Monday, September 5, 2011

How to move console from one position to another using C

<windows.h> provides a function MoveWindow() to move the console from one position to another. It requires 6 parameters. The source code is given below.

#include<windows.h>
#include<stdio.h>
HWND WINAPI GetConsoleWindowNT()
{
    // 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();
}
int main(){
    CONSOLE_SCREEN_BUFFER_INFO SBInfo;
    HWND hWnd = GetConsoleWindowNT();
    SetConsoleTitle("Programming-technique- Tutorial");
    MoveWindow(hWnd,950,500,400,200,TRUE);
    printf("Hello Every One\nThis is small window ");
    getch();
    return 0;
}

2 comments:

  1. #include conio.h //is missing from the beginning

    ReplyDelete
    Replies
    1. actually conio.h is not needed if you run this program in GCC MinGW compiler

      Delete