Scrolling a Screen Buffer's Window Example
The SetConsoleWindowInfo() function can be used to scroll the contents of a screen buffer in the console window. This function can also change the window size. The function can either specify the new upper left and lower right corners of the console screen buffer's window as absolute screen buffer coordinates or specify the changes from the current window coordinates. The function fails if the specified window coordinates are outside the boundaries of the console screen buffer. The following example scrolls the view of the console screen buffer up by modifying the window coordinates returned by the GetConsoleScreenBufferInfo() function. The ScrollByAbsoluteCoord() function demonstrates how to specify absolute coordinates, while the ScrollByRelativeCoord() function demonstrates how to specify relative coordinates.
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.
#include <windows.h>
#include <stdio.h>
// For _getwch()
#include <conio.h>
// Global variable
HANDLE hStdout;
int ScrollByAbsoluteCoord(int iRows)
{
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
SMALL_RECT srctWindow;
// Get the current screen buffer size and window position.
if (!GetConsoleScreenBufferInfo(hStdout, &csbiInfo))
{
wprintf(LGetConsoleScreenBufferInfo() failed, error %d\n, GetLastError());
return 1;
}
// Set srctWindow to the current window size and location.
srctWindow = csbiInfo.srWindow;
// Check whether the window is too close to the screen buffer top
if (srctWindow.Top >= iRows)
{
srctWindow.Top -= iRows; // move top up
srctWindow.Bottom -= iRows; // move bottom up
if (!SetConsoleWindowInfo(
hStdout, // screen buffer handle
TRUE, // absolute coordinates
&srctWindow)) // specifies new location
{
wprintf(LSetConsoleWindowInfo() failed, error %d\n, GetLastError());
return 1;
}
return iRows;
}
else
{
wprintf(L\nCannot scroll; the window is too close to the top.\n);
return 1;
}
}
int ScrollByRelativeCoord(int iRows)
{
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
SMALL_RECT srctWindow;
// Get the current screen buffer window position.
if (!GetConsoleScreenBufferInfo(hStdout, &csbiInfo))
{
wprintf(LGetConsoleScreenBufferInfo(), error %d\n, GetLastError());
return 1;
}
// Check whether the window is too close to the screen buffer top
if (csbiInfo.srWindow.Top >= iRows)
{
srctWindow.Top = -iRows; // move top up
srctWindow.Bottom = -iRows; // move bottom up
srctWindow.Left = 0; // no change
srctWindow.Right = 0; // no change
if (!SetConsoleWindowInfo(
hStdout, // screen buffer handle
FALSE, // relative coordinates
&srctWindow)) // specifies new location
{
wprintf(LSetConsoleWindowInfo(), error %d\n, GetLastError());
return 0;
}
return iRows;
}
else
{
wprintf(L\nCannot scroll; the window is too close to the top.\n);
return 0;
}
}
int wmain(int argc, WCHAR **argv)
{
int i;
wprintf(L\nPrinting sixty lines, then scrolling up five lines.\n);
wprintf(LPress any key to scroll up ten lines...\n);
wprintf(Lthen press another key to stop the demo.\n);
for(i=0; i<=60; i++)
wprintf(L%d\n, i);
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
if(ScrollByAbsoluteCoord(5))
{
wprintf(L Scrolling up by 5...\n);
_getwch();
}
else
return 1;
if(ScrollByRelativeCoord(10))
{
wprintf(L Scrolling up by 10...\n);
_getwch();
}
else
return 1;
return 0;
}
Build and run the project. The following screenshots are sample outputs. Notice the scroll bar movement.