Thursday 6 June 2013

myconstdwin.h

So the new header file contains the following header files windows.h, conio.h, stdio.h, so if you include myconstdwin.h, you dont need to include them again.

Its just an improvement on the previous file. It adds a new ability to move the DOS window.
I have it saved as myconstdwin.h to remind me that the three header files are already included.
If you are not sure what to do with this code:
Copy and paste this code into notepad, save the file as myconstdwin.h on the desktop (not as myconstdwin.txt).
Cut and paste the file into the INCLUDE folder (the default path for the include folder in Code::Blocks is
C:\Program Files\CodeBlocks\MinGW\include) or (C:\Program Files\CodeBlocks\MinGW\x86_64-w64-mingw32\include). Now any program in Code::Blocks that has 
#include <myconstdwin.h> in it will compile and run.

NOTE: Any program that uses the movewin function might require you to  rewrite it again. That means a program that uses movewin will require you to include the following lines in the program:

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

EDIT: To see what this file allows us to do Click Me

Now on to the header file:

#include <windows.h>
#include <conio.h>
#include <stdio.h>  //For move and resize window

COORD coord = {0, 0};

/*Start of move and resize window*/
/*Move Window*/
HWND WINAPI GetConsoleWindowNT(void)
{
    // declare function pointer type

    typedef HWND WINAPI (*GetConsoleWindowT)(void);

    // declare one such function pointer

    GetConsoleWindowT GetConsoleWindow;

    // get a handle on kernel32.dll

    HMODULE hK32Lib = GetModuleHandle(TEXT("KERNEL32.DLL"));

    // assign procedure address to function pointer

    GetConsoleWindow = (GetConsoleWindowT)GetProcAddress(hK32Lib,TEXT("GetConsoleWindow"));

    // check if the function pointer is valid

    // since the function is undocumented

    if ( GetConsoleWindow == NULL ) {
         return NULL;
    }

    // call the undocumented function

    return GetConsoleWindow();
}

void movewin(int x, int y) //Move the DOS Window x=Decrease-Left Increase-Right, y=Decrease-Up Increase-Down
{
    HWND hWnd=GetConsoleWindowNT();
    MoveWindow(hWnd,x,y,800,400,TRUE); //x,y moves screen. 800,400 takes the screen max size cannot make it
                                       //larger using this we have to use the 'mode x,y' DOS command
}
/*End of move and resize window*/


void gotoxy(int x,int y)
{
coord.X=x;coord.Y=y; // X and Y coordinates
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}


void SetColorAndBackground(int ForgC,int BackC)
{
     WORD wColor=((BackC&0x0F)<<4)+(ForgC & 0x0F);;
     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),wColor);
     return;
}


No comments:

Post a Comment