Windows Character Mode Application 3

 

 

 

 

 

Reading and Writing Blocks of Characters and Attributes Example

 

The ReadConsoleOutput() function copies a rectangular block of character and color attribute data from a console screen buffer into a destination buffer. The function treats the destination buffer as a two-dimensional array of CHAR_INFO structures. Similarly, the WriteConsoleOutput() function copies a rectangular block of character and color attribute data from a source buffer to a console screen buffer. The following example uses the CreateConsoleScreenBuffer() function to create a new screen buffer. After the SetConsoleActiveScreenBuffer() function makes this the active screen buffer, a block of characters and color attributes is copied from the top two rows of the STDOUT screen buffer into a temporary buffer. The data is then copied from the temporary buffer into the new active screen buffer. When the application is finished using the new screen buffer, it calls SetConsoleActiveScreenBuffer() to restore the original STDOUT screen buffer.

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>

 

int wmain(int argc, WCHAR **argv)

{

    HANDLE hStdout, hNewScreenBuffer;

    SMALL_RECT srctReadRect;

    SMALL_RECT srctWriteRect;

    CHAR_INFO chiBuffer[160]; // [2][80];

    COORD coordBufSize;

    COORD coordBufCoord;

    BOOL fSuccess;

 

    // Get a handle to the STDOUT screen buffer to copy from and

    // create a new screen buffer to copy to.

    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);

    hNewScreenBuffer = CreateConsoleScreenBuffer(

       GENERIC_READ | GENERIC_WRITE,       // read/write access        

       0,                       // not shared

       NULL,                    // default security attributes

       CONSOLE_TEXTMODE_BUFFER, // must be TEXTMODE

       NULL);                   // reserved; must be NULL

 

    if (hStdout == INVALID_HANDLE_VALUE || hNewScreenBuffer == INVALID_HANDLE_VALUE)

    {

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

        return 1;

    }

    else

        wprintf(L"CreateConsoleScreenBuffer() should be OK!\n");

 

    // Make the new screen buffer the active screen buffer.

    if (! SetConsoleActiveScreenBuffer(hNewScreenBuffer) )

    {

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

        return 1;

    }

    else

        wprintf(L"SetConsoleActiveScreenBuffer() should be OK!\n");

 

    // Set the source rectangle.

    srctReadRect.Top = 0;    // top left: row 0, col 0

    srctReadRect.Left = 0;

    srctReadRect.Bottom = 1; // bottom right: row 1, col 79

    srctReadRect.Right = 79;

 

    // The temporary buffer size is 2 rows x 80 columns.

    coordBufSize.Y = 2;

    coordBufSize.X = 80;

 

    // The top left destination cell of the temporary buffer is row 0, col 0.

    coordBufCoord.X = 0;

    coordBufCoord.Y = 0;

 

    // Copy the block from the screen buffer to the temp. buffer.

    fSuccess = ReadConsoleOutput(

       hStdout,        // screen buffer to read from

       chiBuffer,      // buffer to copy into

       coordBufSize,   // col-row size of chiBuffer

       coordBufCoord,  // top left dest. cell in chiBuffer

       &srctReadRect); // screen buffer source rectangle

    if (! fSuccess)

    {

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

       return 1;

    }

    else

       wprintf(L"ReadConsoleOutput() should be OK!\n");

 

    // Set the destination rectangle.

    srctWriteRect.Top = 10;    // top lt: row 10, col 0

    srctWriteRect.Left = 0;

    srctWriteRect.Bottom = 11; // bottom rt: row 11, col 79

    srctWriteRect.Right = 79;

 

    // Copy from the temporary buffer to the new screen buffer.

    fSuccess = WriteConsoleOutput(

        hNewScreenBuffer, // screen buffer to write to

        chiBuffer,        // buffer to copy from

        coordBufSize,     // col-row size of chiBuffer

        coordBufCoord,    // top left src cell in chiBuffer

        &srctWriteRect);  // dest. screen buffer rectangle

    if (! fSuccess)

    {

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

        return 1;

    }

    else

        wprintf(L"WriteConsoleOutput() is OK!\n");

 

    wprintf(L"Please wait for a while...\n");

    Sleep(5000);

 

    // Restore the original active screen buffer.

    if (!SetConsoleActiveScreenBuffer(hStdout))

    {

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

        return 1;

    }

   else

        wprintf(L"SetConsoleActiveScreenBuffer() is OK!\n");

}

 

Build and run the project. The following screenshots are sample outputs.

 

Reading and Writing Blocks of Characters and Attributes Example: A sample console program output in action 1

 

Reading and Writing Blocks of Characters and Attributes Example: A sample console program output in action 2

 

 

 

 

< Character Mode App. Win32 Programming 2 | Win32 Programming | Character Mode App. Index | Character Mode App. Win32 Programming 4 >