Search code examples
cwindowswinapiconsoleheader-files

Console Screen Hides after implementing a API function from (windows.h)in C


I am new to C language but having experience in higher level language such as java, python.....etc.

I am trying to resize the console window in Windows OS. Using the "SetConsoleWindowInfo and SetConsoleScreenBufferSize" function from "windows.h" header file.

But while I try to run that code the console window hides in the taskbar and nothing is happening. While I try to run the code in vsCode the integrated terminal prints nothing.

Here is my code

void setConsole(short consolePos_X, short consolePos_Y, short consoleWidth, short consoleHeight){

HANDLE wConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);

SMALL_RECT consoleWinSize = {consolePos_X, consolePos_Y, consolePos_X+consoleWidth, consolePos_Y+consoleHeight};
SetConsoleWindowInfo(wConsoleHandle, TRUE, &consoleWinSize);

COORD buffSize = {100, 50};
SetConsoleScreenBufferSize(wConsoleHandle, buffSize);
SetConsoleTitle("Snake Game");

I think the problem is with setting buffersize but I am not sure.

Hope I will get Solution. Thanks in Advance...!


Solution

  • Hey I Found The Solution.

    The problem is that the "ConsoleScreenBufferSize" should not be less than the "ConsoleWindowSize" (More info here). But I have used a "BufferSize" which is less than the "ConsoleSize". So the problem aries.

    Solution for this problem is just to use the "BufferSize" Greater than or equal to the "ConsoleSize". As follows:

     COORD coord = {consoleWidth, consoleHeight};
         SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), &coord);
    

    And this just works fine. If you have anydoubts fell free to comment here. I will try to answer in my free time

    Hope this is useful..!