读取和写入字符和属性块
重要
本文档介绍控制台平台功能,该功能已不再是生态系统蓝图的一部分。 我们不建议在新产品中使用此内容,但我们未来将无限期支持现有使用。 我们的首选最新解决方案侧重于虚拟终端序列,以实现跨平台方案中的最大兼容性。 有关此设计决策的详细信息,请参阅经典控制台与虚拟终端文档。
ReadConsoleOutput 函数将字符和颜色属性数据的矩形块从控制台屏幕缓冲区复制到目标缓冲区。 该函数将目标缓冲区视为 CHAR_INFO 结构的二维数组。 同样,WriteConsoleOutput 函数将字符和颜色属性数据的矩形块从源缓冲区复制到控制台屏幕缓冲区。 有关在屏幕缓冲区单元的矩形块读取或写入内容的详细信息,请参阅输入和输出方法。
下面的示例使用 CreateConsoleScreenBuffer 函数创建新屏幕缓冲区。 在 SetConsoleActiveScreenBuffer 函数使其成为活动屏幕缓冲区后,字符和颜色属性块将从 STDOUT 屏幕缓冲区的前两行复制到临时缓冲区。 然后,将数据从临时缓冲区复制到新的活动屏幕缓冲区。 当应用程序使用完新屏幕缓冲区后,将调用 SetConsoleActiveScreenBuffer 来还原原始 STDOUT 屏幕缓冲区。
#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;
}