Tuesday, 13 August 2013

Break Down of myconstdwin.h : Gotoxy, Change text & Background Colour, Move Dos Window.

The file myconstdwin.h, and how to use it can be found here.

It allows you to do three things:
1. Move the cursor to any part of the screen (gotoxy(x,y))
2. Change the text/background colour of the DOS screen (SetColorAndBackground(text,background))
3. Move the DOS window (movewin(Horizontal,Vertical))

1. Move Cursor: When you want to send the cursor to a certain place, use this function. X is for column (horizontal space) and y is for row (vertical space).

#include <myconstdwin.h>

main()
{
    gotoxy(5,15);
    printf("Press any key....");
    getch();

    //go back and erase what's printed
    gotoxy(5,15);
    printf("                    ");

    //print final message at same spot
    gotoxy(5,15);
    printf("Thanks!");
    getch();
}

2. Change the Text Or Background Colour: When you tire of the old Grey and Black, you can specify what colour you want the text or background to be. For a short program that tells you what number is what colour click HERE.

#include <myconstdwin.h>

main()
{
    SetColorAndBackground(12,0); //red
    gotoxy(5,15);
    printf("Press any key....");
    getch();

    //go back and erase what's printed
    gotoxy(5,15);
    printf("                    ");

    //print final message at same spot
    gotoxy(5,15);
    printf("Thanks!");
    getch();
    SetColorAndBackground(8,0); //back to grey
}

You can set the entire background colour to what you want by setting it once and "cls"ing the screen...like so

#include <myconstdwin.h>

main()
{
    SetColorAndBackground(15,2); //15=Bright white text, 2=dull green background
    system("cls");
    printf("Background is set....");
    getch();
}

Sample output:



You can also colour parts of the screen if you use this with the gotoxy function. Here is a sample:



#include <myconstdwin.h>

main()
{
    int loop;

    system("mode 88,30");
    SetColorAndBackground(8,12);
    for(loop=0;loop<5;loop++)
    {
        gotoxy(5,15+loop);
        printf("          ");
    }

    SetColorAndBackground(8,9);
    for(loop=0;loop<5;loop++)
    {
        gotoxy(15,15+loop);
        printf("          ");
    }

    SetColorAndBackground(8,10);
    for(loop=0;loop<5;loop++)
    {
        gotoxy(5,20+loop);
        printf("          ");
    }

    SetColorAndBackground(8,11);
    for(loop=0;loop<5;loop++)
    {
        gotoxy(15,20+loop);
        printf("          ");
    }
    getch();
    SetColorAndBackground(8,0);
}

3. Move the window: You can specify x and y to where you want the window to be. This is in case you use the DOS "Mode x,y" command to resize the screen and want to DOS window to appear at the top left corner of the screen (or anywhere else for that matter).
The following program will slowly move the DOS window across you screen, first horizontally, then vertically...

#include <myconstdwin.h>

main()
{
    int x,loop;

    system("mode 88,30"); //don't worry, i put this here as a demo to re-size the screen, remove if you want
    printf("Changing the value of x moves the window thus\n");
    for(loop=0;loop<300;loop++)
    {
        movewin(loop,1);
        //provide a slight pause
        for(x=0;x<5000000;x++);
    }
    printf("Press any key....\n");
    getch();

    printf("Changing the value of y moves the window thus\n");
    for(loop=0;loop<300;loop++)
    {
        //the window will move back to 1 because it's set back to 1 now.
        movewin(1,loop);
        //provide a slight pause
        for(x=0;x<5000000;x++);
    }
}

//You do not need to include the following lines if you are using C::B 12.11
void movewin(int x, int y)
{
    HWND hWnd=GetConsoleWindowNT();
    MoveWindow(hWnd,x,y,800,400,TRUE);
}

Notice that I had to rewrite these in the program even if it is in the header file:

void movewin(int x, int y)
{
    HWND hWnd=GetConsoleWindowNT();
    MoveWindow(hWnd,x,y,800,400,TRUE);
}

EDIT:This has been fixed with C::B 12.11

If I don't, it gives me an error sometimes. I don't know why. I'll update it here once I figure it out.

No comments:

Post a Comment