0

How can I resize the Windows console window in C?

22
  • 1
    C just specifies file i/o. Consoles are OS-specific. Commented Jun 18, 2013 at 4:34
  • You need to use SetConsoleScreenBufferSize and then SetConsoleWindowInfo. If you can't get this to work, post the code. Commented Jun 18, 2013 at 4:45
  • Comment formatting is weird, so I'm just going to edit my post. New stuff in response to @HarryJohnston is up there. Commented Jun 18, 2013 at 5:06
  • @MichaelAnthonyLeber: generally speaking, anything the original poster wants to add to the question in response to a comment should indeed be made by editing the original post. :-) Commented Jun 18, 2013 at 5:10
  • @MichaelAnthonyLeber on Windows to change console window we were using registry entry HKCU\Console Commented Jun 18, 2013 at 5:11

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.