I wrote a code to change window buffer and size in a console app with C.

Background: I had trouble with changing window buffer and size in C with WinAPI. Simply calling system("mode con cols=XX lines=YY"); worked but I didn't want to do that like that.

First I've tried to change window buffer and size in C with WinAPI. I had a problem with the code:

SetConsoleScreenBufferSize(GetStdHandle( STD_OUTPUT_HANDLE ), newbuffersize); SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), TRUE, &newwindowsize);

This worked in some cases, but failed other times. Than I changed order:

SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), TRUE, &newwindowsize); SetConsoleScreenBufferSize(GetStdHandle( STD_OUTPUT_HANDLE ), newbuffersize);

This worked in some cases, but failed other times. When first order worked, second second one failed. When second one worked, first one failed. Interesting case isn't it? Here I've posted it: https://stackoverflow.com/questions/47270329/fail-to-change-windows-size-and-buffer-under-c-with-winapi

Than I figured out the problem: Buffer cannot be smaller than window size. If you change buffer first, you cant go smaller than old window size. If you change window first, you cant go larger than old buffer size.

This is my solution, please review and share your ideas and comments:

void newbufwinxy(int x, int y)
SMALL_RECT console_size2;
console_size2.Right = x;
console_size2.Bottom = y;
console_size2.Left = 1;
console_size2.Top = 1;
COORD console_size;
console_size.X = x;
console_size.Y = y;
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &screencsbi);
//if new buffer is larger than old buffer size or at least window, go ahead
if (console_size.X >= screencsbi.dwSize.X && (console_size.Y >= screencsbi.dwSize.Y||console_size.Y >= screencsbi.srWindow.Bottom-1)) 
    SetConsoleScreenBufferSize(hStdout, console_size);
    SetConsoleWindowInfo(hStdout, TRUE, &console_size2);
//if new window is larger in widght but smaller in height, do a temporary shrink 
else if (console_size2.Right >= screencsbi.srWindow.Right-1 && console_size2.Bottom <= screencsbi.srWindow.Bottom-1)
    console_size2.Right = 20; //temporarily shrink window width...
    SetConsoleWindowInfo(hStdout, TRUE, &console_size2);
    SetConsoleScreenBufferSize(hStdout, console_size);
    console_size2.Right = x;
    SetConsoleWindowInfo(hStdout, TRUE, &console_size2);
//if new buffer is smaller than old window, change window first
else if (console_size.X <= screencsbi.dwSize.X && !(console_size2.Bottom >= screencsbi.dwSize.Y))
    SetConsoleWindowInfo(hStdout, TRUE, &console_size2);
    SetConsoleScreenBufferSize(hStdout, console_size);
    printf("\nold _values_ >> new _values_ \n");
    printf("dwSize.X = %i >> ",screencsbi.dwSize.X);
    printf("dwSize.X = %i\n",console_size.X);
    printf("dwSize.Y = %i >> ",screencsbi.dwSize.Y);
    printf("dwSize.Y = %i\n",console_size.Y);
    printf("srWindow.Bottom = %i >> ", screencsbi.srWindow.Bottom);
    printf("srWindow.Bottom = %i\n", console_size2.Bottom);
    printf("srWindow.Right = %i >> ", screencsbi.srWindow.Right);
    printf("srWindow.Right = %i\n", console_size2.Right);
    printf("SetConsoleWindowInfo, SetConsoleScreenBufferSize\n");
    printf("Error. Cannot recognise console windows or buffer size!\n");
    printf("\nProgram encountered errors and shall exit now.");
    printf("\nPress any key to continue...");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.