How can I resize the Windows console window in C?
1 Answer
Alright, after much deliberation, I got the code working.
Using this include:
#include <windows.h>
This struct:
struct SMALL_RECT {
SHORT Left;
SHORT Top;
SHORT Right;
SHORT Bottom;
};
And this function:
void adjustWindowSize()
{
struct SMALL_RECT test;
HANDLE hStdout;
COORD coord;
BOOL ok;
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
coord.X = 100;
coord.Y = 50;
ok = SetConsoleScreenBufferSize(hStdout, coord);
test.Left = 0;
test.Top = 0;
test.Right = coord.X-1;
test.Bottom = coord.Y-1;
SetConsoleWindowInfo(hStdout, ok, &test);
} //end adjustWindowSize
I successfully adjusted the size of the console window to the values in coord.X and coord.Y
2 Comments
Tamás Szelei
While this probably works, it makes no sense to pass the return value of
SetConsoleScreenBufferSize as the bAbsolute parameter of SetConsoleWindowInfo.I'm unable to get this to work.. I'm on Window 11, using VS 2019 in C mode. SetConsoleScreenBufferSize always returns false. SetConsoleWindowInfo only returns true if test.Left and test.Top = 0. However, it never resizes the console window, no matter what I put in Right and Bottom.
SetConsoleScreenBufferSizeand thenSetConsoleWindowInfo. If you can't get this to work, post the code.