4

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?

uzumaki
  • 1,743
  • 17
  • 32
  • I see this too. I haven't been able to find out why it happens so adding 1 seems to be the workaround. The only other reference I've found to this issue is in a post here https://social.msdn.microsoft.com/Forums/vstudio/en-US/0b303fe1-03c7-4c7f-b69b-59cff859b35e/how-do-i-get-my-vbnet-console-application-to-be-able-to-use-the-full-range-of-32bit-rgb-color?forum=vbgeneral – Sam Jul 03 '17 at 00:56
  • Same thing happens if using WinAPI directly with C. – René Nyffenegger Feb 28 '19 at 06:25

0 Answers0