Home   Index   About
Ultimate Pack


Custom Search
Reading and Writing Blocks of Characters and Attributes

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. For more information about reading from or writing to rectangular blocks of screen buffer cells, see Input and Output Methods.

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 SDTOUT 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.

#include <windows.h>

VOID main(void) {

HANDLE hStdout, hNewScreenBuffer;

SMALL_RECT srctReadRect;

SMALL_RECT srctWriteRect;

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

COORD coordBufSize;

COORD coordBufCoord;

BOOL fSuccess;

/*

* Get a handle of 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 | /* read-write access */

GENERIC_WRITE,

0, /* not shared */

NULL, /* no security attributes */

CONSOLE_TEXTMODE_BUFFER, /* must be TEXTMODE */

NULL); /* reserved; must be NULL */

if (hStdout == INVALID_HANDLE_VALUE ||

hNewScreenBuffer == INVALID_HANDLE_VALUE)

MyErrorExit("CreateConsoleScreenBuffer");

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

if (! SetConsoleActiveScreenBuffer(hNewScreenBuffer) )

MyErrorExit("SetConsoleActiveScreenBuffer");

/* Set the source rectangle. */

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

srctReadRect.Left = 0;

srctReadRect.Bottom = 1; /* bot. 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)

MyErrorExit("ReadConsoleOutput");

/* Set the destination rectangle. */

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

srctWriteRect.Left = 0;

srctWriteRect.Bottom = 11; /* bot. 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)

MyErrorExit("WriteConsoleOutput");

Sleep(10000);

/* Restore the original active screen buffer. */

if (! SetConsoleActiveScreenBuffer(hStdout))

MyErrorExit("SetConsoleActiveScreenBuffer");

}


Last news from Greatis Software

Nostalgia .Net     Nostalgia .Net     .Net is powerful, but not all-powerful, so sometimes we need to use Win32 API for our .Net applications. It's simple enough with Platform Invoke if you have Win32 skill, but we do not always have time to dig the ancient documentation, declare the special types that are compatible with Win32, find the values of the Win32's constants and so on. Nostalgia .Net offers several simple-to-use classes, and components that will allow you to forget about the headache of Win32 and just use the power of Win32 in your application the same way as you use the native. Net classes.  More »

Recommended software for developers

Ultimate Pack for Delphi and C++ Builder     Ultimate Pack     Component pack for Delphi and C++ Builder that contains runtime form designer, runtime object inspector, print suite and much more for the very special price.  More »

Form Designer .Net     Form Designer .Net     Unique runtime form design solution that allows to edit any form in .Net WinForms application at runtime with full source codes for only 300 euro!  More »

Print Suite .Net     Print Suite .Net     Print Suite .Net is a set of components for easy printing texts, images and grids from your WinForms applications. Full C# source codes are available  More »

Gradient Controls .Net     Gradient Controls .Net     Gradient Controls .Net offers controls with gradient background feature. Labels, panels and so on... Full C# source codes are available  More »

iGrid     Greatis iGrid     iGrid plots drawing grid right over your desktop, so you can use it everywhere, with any drawing application without any special plugins for different graphic editors.  More »


All the contacts and projects

Dmitry Vasiliev (just.dmitry)

Related Links

Software for Visual Studio .NET developers
Software for Delphi and C++ Builder developers
Software for Visual Basic 6 developers
Delphi Tips&Tricks
MegaDetailed.NET

More Online Helps

Win32 Programmer's Reference
Win32 Multimedia Programmer's Reference
OLE Programmer's Reference
Microsoft Windows Pen API Programmer's Reference
Microsoft Windows Sockets 2 Reference
Microsoft Windows Telephony API (TAPI) Programmer's Reference
Unix Manual Pages

Free Tech Secrets ;) Copyright © 2008-2012 Free Tech Secrets ;) greatis just4fun network just4fun