Reducing console size

后端 未结 4 1967
孤街浪徒
孤街浪徒 2021-01-06 16:27

I got a problem with changing console size. This is my code:

BOOL setConsole(int x, int y)
{
hStdin = GetStdHandle(STD_INPUT_HANDLE); 
hStdout = GetStdHandle         


        
4条回答
  •  失恋的感觉
    2021-01-06 16:55

    Late to the party ...

    As far as can be devised from MSDN and a few tests, the screen buffer can't be set smaller than the window's extent or the window's extent made bigger than the screen buffer.

    One hack is to shrink the window to a minimal before changing the buffer size :

    static void
    set_console_size(HANDLE screen_buffer, SHORT width, SHORT height)
    {
        assert(screen_buffer != NULL);
        assert(width > 0);
        assert(height > 0);
    
        COORD const size = { width, height };
        BOOL success;
    
        SMALL_RECT const minimal_window = { 0, 0, 1, 1 };
        success = SetConsoleWindowInfo(screen_buffer, TRUE, &minimal_window);
        CHECK(success);
    
        success = SetConsoleScreenBufferSize(screen_buffer, size);
        CHECK(success);
    
        SMALL_RECT const window = { 0, 0, size.X - 1, size.Y - 1 };
        success = SetConsoleWindowInfo(screen_buffer, TRUE, &window);
        CHECK(success);
    }
    

提交回复
热议问题