how can i resize a window console buffer?
i know that i can use the SetConsoleScreenBufferSize(), but i'm getting problems convert from pixels to characters, because the vertical scrollbar is showed and the size isn't changed correctly, because it's more big than the buffer size:

CONSOLE_SCREEN_BUFFER_INFOEX consolesize;
        consolesize.cbSize=sizeof(consolesize);
        GetConsoleScreenBufferInfoEx(hConsole,&consolesize);
        COORD c;
        consolesize.srWindow.Right  = newsize.X+consolesize.srWindow.Right;
        consolesize.srWindow.Bottom = newsize.Y + consolesize.srWindow.Left;
        CONSOLE_FONT_INFO consolefont={0} ;
        GetCurrentConsoleFont(hConsole, FALSE,&consolefont);
        GetConsoleFontSize(hConsole,consolefont.nFont);
        c.X =(consolesize.srWindow.Right)/consolefont.dwFontSize.X;
        c.Y =(consolesize.srWindow.Bottom)/consolefont.dwFontSize.Y;
        SetConsoleScreenBufferSize( hConsole, c );
        SetConsoleWindowInfo(hConsole, FALSE, &consolesize.srWindow);

how can i fix these problem?

is SetConsoleScreenBufferSize() limitted?
if i use it on constructor, works. affter that don't... confused :(
the GetLastError() is 87 - 'The parameter is incorrect.'

nullptr: that thread is limited.
heres my code:

void SetWindowSize(COORD newsize)//newaize recive the number of characters been showed on window
    {
        CONSOLE_SCREEN_BUFFER_INFOEX consolesize;

        consolesize.cbSize=sizeof(consolesize);

        HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

        GetConsoleScreenBufferInfoEx(hConsole,&consolesize);

        COORD c;
        c.X = newsize.X;
        c.Y = newsize.Y;
        consolesize.dwSize = c;
        consolesize.dwMaximumWindowSize = c; //change the window max size

        consolesize.srWindow.Left = 0;
        consolesize.srWindow.Right = c.X;
        consolesize.srWindow.Top = 0;
        consolesize.srWindow.Bottom = c.Y;
        //srWindow, c and dwMaximumWindowSize are the same
        //here i don't use pixels. for that i must get the current font size and then calculate the number of characters

        SetConsoleScreenBufferInfoEx(hConsole, &consolesize);
        SetConsoleWindowInfo(hConsole, FALSE, &consolesize.srWindow);
    }

these code works fine. but for we use pixels, instead characters number, we must get the current font size and calculate the number of characters:
horizontalnumberofcharacters=(pixelswidth-bordersize)/currentfontsize.x;
verticalnumberofcharacters=(pixelsheight-bordersize)/currentfontsize.y;
- i don't have sure about border size, we can get it. but will be (-bordersize*2)?
int border_thickness = GetSystemMetrics(SM_CXSIZEFRAME);
(that function isn't completed for these and see if the border is showed and the SM_CYCAPTION is for caption border size)
thanks to all

Remember I am not running your code but I do see an issue. The variables to the API call look to be integers so a rounding error could have your size off by 1 character. That is if you did get your pixel count per fixed spaced font in question, it may work out to 39.5 and lop it off at 39. So you may have to fudge this aby adding 1 to your calculation to avoid the scrollbar.

Also, you would have single stepped your code to see all the variables to check your math.

This article has been dead for over six months. Start a new discussion instead.