Training
Module
Control variable scope and logic using code blocks in C# - Training
Use code blocks with more confidence, understanding how they impact the visibility and accessibility of both higher and lower-level constructs in your code.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Important
This document describes console platform functionality that is no longer a part of our ecosystem roadmap. We do not recommend using this content in new products, but we will continue to support existing usages for the indefinite future. Our preferred modern solution focuses on virtual terminal sequences for maximum compatibility in cross-platform scenarios. You can find more information about this design decision in our classic console vs. virtual terminal document.
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 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.
#include <windows.h>
#include <stdio.h>
int 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 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 | // read/write access
GENERIC_WRITE,
FILE_SHARE_READ |
FILE_SHARE_WRITE, // 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)
{
printf("CreateConsoleScreenBuffer failed - (%d)\n", GetLastError());
return 1;
}
// Make the new screen buffer the active screen buffer.
if (! SetConsoleActiveScreenBuffer(hNewScreenBuffer) )
{
printf("SetConsoleActiveScreenBuffer failed - (%d)\n", GetLastError());
return 1;
}
// 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)
{
printf("ReadConsoleOutput failed - (%d)\n", GetLastError());
return 1;
}
// 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)
{
printf("WriteConsoleOutput failed - (%d)\n", GetLastError());
return 1;
}
Sleep(5000);
// Restore the original active screen buffer.
if (! SetConsoleActiveScreenBuffer(hStdout))
{
printf("SetConsoleActiveScreenBuffer failed - (%d)\n", GetLastError());
return 1;
}
return 0;
}
Training
Module
Control variable scope and logic using code blocks in C# - Training
Use code blocks with more confidence, understanding how they impact the visibility and accessibility of both higher and lower-level constructs in your code.
Documentation
CHAR_INFO structure - Windows Console
Specifies a Unicode or ANSI character and its attributes. This structure is used by console functions to read from and write to a console screen buffer.
ReadConsoleOutput function - Windows Console
Reads character and color attribute data from a rectangular block of character cells in a console screen buffer and writes data in the destination buffer.
Console Buffer Security and Access Rights - Windows Console
The Windows security model enables you to control access to console input buffers and console screen buffers. For more information about security, see Access-Control Model.