SRC: changing the console buffer size
Ocassionally I receive mails asking questions about Win32 console functions. With regards to that I plan to start posting SAMPLE functions that cover most of the functions that I use in
KTS for manipulating windows console.
So here is the first sample, a function that changes the console screen buffer.
Please note that error checking is stripped for better readability. Keep in mind that there might be bugs too so use them on your risk.
You might also want to check the MS
console samples.
/**********************************************************
* I take no responsibility for what you or others might do
* with this knowledge.
***********************************************************/
void SetConsoleBuffSize( COORD buff )
{
// please note error checks are strippped !!!
CONSOLE_SCREEN_BUFFER_INFO csbi;
bool fixScreenSize = false;
GetConsoleScreenBufferInfo( GetOutputHandle( ), &csbi );
// buff is ok
if( csbi.dwSize.X == buff.X && csbi.dwSize.Y == buff.Y ) return;
if( buff.Y < csbi.srWindow.Bottom - csbi.srWindow.Top + 1 )
{
COORD tmp;
fixScreenSize = true;
tmp.X = csbi.dwSize.X;
tmp.Y = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
SetConsoleScreenBufferSize( GetOutputHandle( ), tmp );
csbi.srWindow.Top = 0;
csbi.srWindow.Bottom = buff.Y - 1;
}
if( buff.X < csbi.srWindow.Right - csbi.srWindow.Left + 1 )
{
fixScreenSize = true;
csbi.srWindow.Right = buff.X - 1;
csbi.srWindow.Left = 0;
}
if( fixScreenSize ) SetConsoleWindowInfo( GetOutputHandle( ), TRUE, &csbi.srWindow );
SetConsoleScreenBufferSize( GetOutputHandle( ), coBuff );
}
Tuesday, October 25, 2005