Windows Character Mode Application 7

 

 

 

 

 

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.

 

#include <windows.h>

#include <stdio.h>

#include <conio.h>

 

int wmain(int argc, WCHAR **argv)

{

    HANDLE hStdout;

    CONSOLE_SCREEN_BUFFER_INFO csbiInfo;

    SMALL_RECT srctScrollRect, srctClipRect;

    CHAR_INFO chiFill;

    COORD coordDest;

    int i;

 

    wprintf(L"Printing 20 lines for reference.\n");

    wprintf(L"Line 6 should be discarded during scrolling.\n");

 

    for(i=0; i<=20; i++)

      {

        wprintf(L"%d\n", i);

            // Just for fun

            if(i == 4)

                  Sleep(1000);

      }

 

    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);

 

    if(hStdout == INVALID_HANDLE_VALUE)

    {

        wprintf(L"GetStdHandle() failed with error %d\n", GetLastError());

        return 1;

    }

 

    // Get the screen buffer size.

    if(!GetConsoleScreenBufferInfo(hStdout, &csbiInfo))

    {

        wprintf(L"GetConsoleScreenBufferInfo() failed, error %d\n", GetLastError());

        return 1;

    }

 

    // 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

    if(!ScrollConsoleScreenBuffer( 

        hStdout,         // screen buffer handle

        &srctScrollRect, // scrolling rectangle

        &srctClipRect,   // clipping rectangle

        coordDest,       // top left destination cell

        &chiFill))       // fill character and color

    {

        wprintf(L"ScrollConsoleScreenBuffer() failed, error %d\n", GetLastError());

        return 1;

    }

}

 

Build and run the project. The following screenshot is a sample output.

 

Scrolling a Screen Buffer's Contents Example: A sample console program output in action

 

 

 

 

< Character Mode App. Win32 Programming 6 | Win32 Programming | Character Mode App. Index | Character Mode App. Win32 Programming 8 >