17.3.13 Window and Screen Buffer Sizes

Console window and screen buffer sizes are expressed in terms of a coordinate grid based on character cells. The width is the number of characters in each row, and the height is the number of rows. The screen buffer may be of any size, limited only by available memory. The dimensions of a console window may not exceed the corresponding dimensions of either the screen buffer or the maximum window that could fit on the display using the current font.

Call GetConsoleScreenBufferInfo to determine the current size of the screen buffer or the console window associated with it. The dwSize field contains the screen buffer size. The srWindow field contains the coordinates of the topleft and bottomright corners of the window in terms of the screen buffer's coordinate system. The srWindow rectangle can be modified and then passed in to SetConsoleWindowInfo to scroll the screen buffer in the window and/or to change the size of the window. The dwMaximumWindowSize field returned by GetConsoleScreenBufferInfo is the maximum size of the console window given the current screen buffer size, the current font, and the display size. The size returned by GetLargestConsoleWindowSize is based only on the the current font and display, and does not consider the size of the screen buffer.

If the mode of the screen buffer is windows aware, the screen buffer size can be changed by the user as discussed above in the section on buffer size events. In any mode, the application can change a screen buffer's size by calling SetConsoleScreenBufferSize; and it can change the size of the console window by calling SetConsoleWindowInfo. If you change the size of the screen buffer such that it is smaller than the window, the window size will be automatically reduced. If you attempt to change the window size so that it exceeds the limits of the screen buffer or the display, SetConsoleWindowInfo will fail and return FALSE.

Typically, applications that are not in windows aware mode will either use the inherited window and screen buffer sizes, or set them to the desired size at initialization. Window aware applications will probably want to use GetConsoleScreenBufferInfo to query window and screen buffer size at start up. This information can then be used to determine the way data is displayed in the window. If the user changes the screen buffer size, the event will be reported and the application may want to respond by changing the way data is displayed. For example, you could adjust the way text wraps at the end of the line if the number of characters per row changes. Applications that change screen buffer attributes should either create their own screen buffer or save console state during startup and restore it at exit.