SetConsoleScreenBufferInfoEx … bug?

2019-05-04 17:51发布

问题:

Each time I run this code ( on Win7) the console gets 1 character smaller in both directions.

int wmain( INT argc, WCHAR **argv )  
{  
    CONSOLE_SCREEN_BUFFER_INFOEX csbi;  
    csbi.cbSize = sizeof(CONSOLE_SCREEN_BUFFER_INFOEX);  
    GetConsoleScreenBufferInfoEx(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);  
    wprintf(L"Window: %u x %u\n", csbi.srWindow.Right - csbi.srWindow.Left + 1, csbi.srWindow.Bottom - csbi.srWindow.Top + 1);  
    SetConsoleScreenBufferInfoEx(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);  
    return 0;  
}

I doubt that's expected behavior. Is it documented? Is it any better in newer versions of Windows? Here's a clip of running it a few times.

p:\test\release> test.exe
Window: 99 x 41

p:\test\release> test.exe
Window: 98 x 40

p:\test\release> test.exe
Window: 97 x 39

回答1:

Long standing "won't fix" bug in the Windows console API. You just need to do what everyone else does, and increment the window.Bottom and window.Right after reading it.

GetConsoleScreenBufferInfoEx(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)
++csbi.srWindowBottom;
++csbi.srWindowRight;