# c++ - How resize window Console?

## Recommended Posts

cambalinho    230

how can i resize window Console?

i did these code:

void SetWindowSize(COORD newsize)
{
PCONSOLE_SCREEN_BUFFER_INFOEX consolesize;
GetConsoleScreenBufferInfoEx(hConsole,consolesize);

COORD c;
consolesize->srWindow.Right  = newsize.X+ consolesize->srWindow.Left;
consolesize->srWindow.Bottom = newsize.Y+ consolesize->srWindow.Top;
SetConsoleWindowInfo(hConsole, TRUE, &consolesize->srWindow);
PCONSOLE_FONT_INFO consolefont;
GetCurrentConsoleFont(hConsole, FALSE,consolefont);
GetConsoleFontSize(hConsole,consolefont->nFont);
c.X =(consolesize->srWindow.Right-consolesize->srWindow.Left)/consolefont->dwFontSize.X;
c.Y =(consolesize->srWindow.Bottom-consolesize->srWindow.Top)/consolefont->dwFontSize.Y;
DebugText("hello size " + to_string(c.X) + "  " + to_string(c.Y));
SetConsoleScreenBufferSize( hConsole, c );
}


but i get a memory leak, or i see the scrollbars.

what i'm doing wrong?

##### Share on other sites
MarkS    3502
According to the MSDN documentation for SetConsoleWindowInfo:

"The function fails if the specified window rectangle extends beyond the boundaries of the console screen buffer. This means that the Top and Left members of the lpConsoleWindow rectangle (or the calculated top and left coordinates, if bAbsolute is FALSE) cannot be less than zero. Similarly, the Bottom and Right members (or the calculated bottom and right coordinates) cannot be greater than (screen buffer height – 1) and (screen buffer width – 1), respectively. The function also fails if the Right member (or calculated right coordinate) is less than or equal to the Left member (or calculated left coordinate) or if the Bottom member (or calculated bottom coordinate) is less than or equal to the Top member (or calculated top coordinate)."

This, to me, seems to state that you must first increase the screen buffer size and then set the window size. Are you checking the return values? Which function is failing, if any? Edited by MarkS

##### Share on other sites
cambalinho    230

the SetConsoleWindowInfo() is give me a memory leak

##### Share on other sites
cambalinho    230

but seems that depends on position

##### Share on other sites
cambalinho    230

ok.. now no memory leak:

void SetWindowSize(COORD newsize)
{
CONSOLE_SCREEN_BUFFER_INFOEX consolesize;//see these type name
consolesize.cbSize=sizeof(consolesize);//i forgot these
GetConsoleScreenBufferInfoEx(hConsole,&consolesize);
COORD c;
consolesize.srWindow.Right  = newsize.X-1;
consolesize.srWindow.Bottom = newsize.Y-1;
CONSOLE_FONT_INFO consolefont={0} ;//see these type name
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);
}


i have 1 problem on c.X and c.Y calculations, because the vertical scrollbar continues visible :(