Quote Originally Posted by 2kaud View Post
It occured to me later that others might find this a useful routine. So I've slightly enhanced the function so that it can now take either pixels or char numbers for width and height. It also will now allow the console window to be down-sized as well as up-sized.

Here's the revised code
Code:
#define _WIN32_WINNT 0x0500 
#include <windows.h>

int SetConsoleSize(int width, int height, bool pixel = true);

int main()
{
        //Tests for function
    SetConsoleSize(1000, 500);
    system("pause");
    SetConsoleSize(200, 200);
    system("pause");
    SetConsoleSize(600, 400);
    system("pause");
    return (0);
}

//Set console size in pixels and centres console
int SetConsoleSize(int width, int height, bool pixel)
{
HANDLE    hcon = GetStdHandle(STD_OUTPUT_HANDLE);

    if (hcon == INVALID_HANDLE_VALUE) {
        return ((int)hcon);
    }

COORD    co;

    //Convert pixel to chars if specified
    if (pixel == true) {
        CONSOLE_FONT_INFO    finfo;
        COORD            fsize;

            if (!GetCurrentConsoleFont(hcon, FALSE, &finfo)) return (GetLastError());

            fsize = GetConsoleFontSize(hcon, finfo.nFont);
            if (fsize.X == 0 || fsize.Y == 0) return (GetLastError());

            co.X = width / fsize.X;
            co.Y = height / fsize.Y;
    } else {
        co.X = width;
        co.Y = height;
    }

    //Can not have a console window larger than the max allowed
COORD    max = GetLargestConsoleWindowSize(hcon);

    if (max.X == 0 || max.Y == 0) return (GetLastError());

    if (co.X > max.X) co.X = max.X;
    if (co.Y > max.Y) co.Y = max.Y;

CONSOLE_SCREEN_BUFFER_INFO    bufInfo;

    //Console window must be no larger than the screen buffer and screen buffer no smaller than console window
    if (!GetConsoleScreenBufferInfo(hcon, &bufInfo)) return (GetLastError());

bool    change = false;

    if (co.X > bufInfo.dwSize.X) {
        bufInfo.dwSize.X = co.X;
        change = true;
    }

    if (co.Y > bufInfo.dwSize.Y) {
        bufInfo.dwSize.Y = co.Y;
        change = true;
    }

    if ((change == true) && !SetConsoleScreenBufferSize(hcon, bufInfo.dwSize)) return (GetLastError());
    
SMALL_RECT rect;

    //Change console window size
    rect.Top = 0;
    rect.Bottom = co.Y - 1;
    rect.Left = 0;
    rect.Right = co.X - 1;

    if (!SetConsoleWindowInfo(hcon, TRUE, &rect)) return (GetLastError());

    Sleep(5);    //THIS IS NOT GOOD BUT NEED TO GIVE THE SYSTEM TIME TO CHANGE CONSOLE SIZE
            //BEFORE GET WINDOW RECT FOR CONSOLE. IF DO WITHOUT DELAY CAN GET WRONG SIZE

HWND    hwndScreen = GetDesktopWindow(),
    conwd = GetConsoleWindow();

RECT    rectScreen,
    rectcon;

int    ConsolePosX,
    ConsolePosY;

    if (!GetWindowRect(hwndScreen, &rectScreen)) return (GetLastError());
    if (!GetWindowRect(conwd, &rectcon)) return (GetLastError());

    //Position resized console window in middle of desktop
    ConsolePosX = (rectScreen.right - rectScreen.left - (rectcon.right - rectcon.left)) >> 1;
    ConsolePosY = (rectScreen.bottom - rectScreen.top - (rectcon.bottom - rectcon.top)) >> 1;
    if (!SetWindowPos(conwd, HWND_NOTOPMOST, ConsolePosX, ConsolePosY, 0, 0, SWP_NOOWNERZORDER | SWP_NOSIZE)) return (GetLastError());

    return (0);
}
sorry the errors continue
seems that i don't have the GetConsoleFontSize() and GetConsoleWindow() and CONSOLE_FONT_INFO type