Why does the console window shrink when using GetConsoleScreenBufferInfoEx in Windows?

喜你入骨 提交于 2021-02-06 12:52:14

问题


I'm trying to set the background and foreground colors of the Windows command line console by using GetConsoleScreenBufferInfoEx and SetConsoleScreenBufferInfoEx .

I'm doing it in Python, by using wintypes, and sofar it works.

But there's something odd happening: the window shrinks a little at every call.

This is how the calling code looks like:

def GetConsoleScreenBufferInfoEx(handle):
    csbi = CONSOLE_SCREEN_BUFFER_INFOEX()  # a structure
    csbi.cbSize = 96  # needs to be set
    success = _GetConsoleScreenBufferInfoEx(  # just a wrap
        handle, byref(csbi))
    return csbi

csbi = GetConsoleScreenBufferInfoEx(handle)
csbi.wAttributes = color
SetConsoleScreenBufferInfoEx(csbi)

Now, everytime I change the color, the window also shrinks.

I can fix it by adding

csbi.srWindow.Right += 1  # otherwise the window will shrink
csbi.srWindow.Bottom += 1

before SetConsoleScreenBufferInfoEx(csbi)

The CONSOLE_SCREEN_BUFFER_INFOEX structure is defined as expected, no values are altered there.

It looks like the GetConsoleScreenBufferInfoEx function returns the last index, so when the window width is 80 csbi.srWindow.Right is 79. But when passing the same value to SetConsoleScreenBufferInfoEx it is interpreted as width.

Is this behaviour normal or is there an error?

来源:https://stackoverflow.com/questions/37849801/why-does-the-console-window-shrink-when-using-getconsolescreenbufferinfoex-in-wi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!