Just Let It Flow

August 8, 2009

Maximizing the Console Window (Completely)

Filed under: Code,Windows — adeyblue @ 12:35 am

It seems that when it comes to the web, most people are under the impression that the maximum size for a Windows console is what you get when you “maximize” one. That is, about half screen, like the following picture:

Free Image Hosting at www.ImageShack.us

In Code

The first few hits that google returns which contain code, back up the impression while displaying code that is invariably a variation on this:

GetConsoleScreenBufferInfo(hStdOut, &info);
rc.Left = rc.Top = 0;
rc.Right = (short)(min(info.dwMaximumWindowSize.X, info.dwSize.X) - 1);
rc.Bottom = (short)(min(info.dwMaximumWindowSize.Y, info.dwSize.Y) - 1);
SetConsoleWindowInfo(hStdOut, TRUE, &rc);

OK, so this will actually create a full screen console, but only on one condition. The default screen buffer width and height must already be set large enough to cover the screen. This default is rarely set to anything other than the stock Windows setting of 80 characters wide and 300 characters tall, which is only enough to cover about half the screen.

Making the console cover the whole screen requires a few pre-steps before the code above. The character dimensions of the current font (GetCurrentConsoleFont()), the size of the screen (GetDeviceCaps()), a division to see how many horizontal and vertical characters we can fit on the screen and then finally a change in the size of the screen buffer (SetConsoleScreenBufferSize()) are required to achieve it. In short…

BOOL MaximizeConsole()
    BOOL bRet = FALSE;
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    if(hStdOut != INVALID_HANDLE_VALUE && hStdOut != NULL)
        CONSOLE_FONT_INFO cfi = {0};
        CONSOLE_SCREEN_BUFFER_INFO csbi = {0};
        if(GetCurrentConsoleFont(hStdOut, FALSE, &cfi) && GetConsoleScreenBufferInfo(hStdOut, &csbi))
            HDC hdcScreen = GetDC(NULL);
                int xScreen = GetDeviceCaps(hdcScreen, HORZRES);
                // get number of characters that would fit across
                SHORT newConsoleX = xScreen / cfi.dwFontSize.X;
                int yScreen = GetDeviceCaps(hdcScreen, VERTRES);
                // get number of characters that would fit down
                SHORT newConsoleY = yScreen / cfi.dwFontSize.Y;
                // ensure we don't make any dimensions smaller
                csbi.dwSize.X = max(csbi.dwSize.X, newConsoleX);
                csbi.dwSize.Y = max(csbi.dwSize.Y, newConsoleY);
                if(SetConsoleScreenBufferSize(hStdOut, csbi.dwSize))
                    bRet = ShowWindow(GetConsoleWindow(), SW_MAXIMIZE);
                ReleaseDC(NULL, hdcScreen);
    return bRet;

Hey, we didn’t use the original code at all. There’s a simple reason for that. Using ShowWindow rather than SetConsoleWindowInfo will change the maximize button on the window to its restore appearance and behaviour, meaning a click will give consistent behaviour with all the other maximized windows. If you don’t want nor care for that, replace it as necessary.

From the Console Window

For those people who think programmers are people who do weights, or more generally, the way to achieve a maximal console via the user interface is as follows.

Right-click on the console’s title bar
Select properties
Choose the Layout tab
Change the value in the Screen Buffer size Width box to a bigger number (125 is sufficient for me, others will probably differ)
Click OK and maximize the window.

If it doesn’t quite cover, repeat using a higher number; if you get a horizontal scrollbar at the bottom, you can make the number smaller to get rid of it, if you so desire. Et voila, as the French would say, you now have a fancy maximized console window just like below.

Free Image Hosting at www.ImageShack.us

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress