Scrolling a Screen Buffer's Contents Example
The ScrollConsoleScreenBuffer() function moves a block of character cells from one part of a screen buffer to another part of the same screen buffer. The function specifies the upper left and lower right cells of the source rectangle to be moved and the destination coordinates of the new location for the upper left cell. The character and color data in the source cells is moved to the new location, and any cells left empty by the move are filled in with a specified character and color. If a clipping rectangle is specified, the cells outside of it are left unchanged. ScrollConsoleScreenBuffer() can be used to delete a line by specifying coordinates of the first cell in the line as the destination coordinates and specifying a scrolling rectangle that includes all the rows below the line. The following example shows the use of a clipping rectangle to scroll only the bottom 15 rows of the console screen buffer. The rows in the specified rectangle are scrolled up one line at a time, and the top row of the block is discarded. The contents of the console screen buffer outside the clipping rectangle are left unchanged.
Create a new empty Win32 console application project. Give a suitable project name and change the project location if needed.
Then, add the source file and give it a suitable name.
Next, add the following source code.
int wmain(int argc, WCHAR **argv)
SMALL_RECT srctScrollRect, srctClipRect;
wprintf(LPrinting 20 lines for reference.\n);
wprintf(LLine 6 should be discarded during scrolling.\n);
for(i=0; i<=20; i++)
// Just for fun
if(i == 4)
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
if(hStdout == INVALID_HANDLE_VALUE)
wprintf(LGetStdHandle() failed with error %d\n, GetLastError());
// Get the screen buffer size.
wprintf(LGetConsoleScreenBufferInfo() failed, error %d\n, GetLastError());
// The scrolling rectangle is the bottom 15 rows of the screen buffer
srctScrollRect.Top = csbiInfo.dwSize.Y - 16;
srctScrollRect.Bottom = csbiInfo.dwSize.Y - 1;
srctScrollRect.Left = 0;
srctScrollRect.Right = csbiInfo.dwSize.X - 1;
// The destination for the scroll rectangle is one row up
coordDest.X = 0;
coordDest.Y = csbiInfo.dwSize.Y - 17;
// The clipping rectangle is the same as the scrolling rectangle.
// The destination row is left unchanged
srctClipRect = srctScrollRect;
// Fill the bottom row with green blanks
chiFill.Attributes = BACKGROUND_GREEN | FOREGROUND_RED;
chiFill.Char.UnicodeChar = ' ';
// Scroll up one line
hStdout, // screen buffer handle
&srctScrollRect, // scrolling rectangle
&srctClipRect, // clipping rectangle
coordDest, // top left destination cell
&chiFill)) // fill character and color
wprintf(LScrollConsoleScreenBuffer() failed, error %d\n, GetLastError());
Build and run the project. The following screenshot is a sample output.