请教关于函数ReadConsoleOutput()问题
查阅了ReadConsoleOutput()的文档:
https://msdn.microsoft.com/zh-cn/vstudio/ms684965(v=vs.95)
BOOL WINAPI ReadConsoleOutput(
_In_ HANDLE hConsoleOutput,
_Out_ PCHAR_INFO lpBuffer,
_In_ COORD dwBufferSize,
_In_ COORD dwBufferCoord,
_Inout_ PSMALL_RECT lpReadRegion
);
其中的一个参数:
dwBufferCoord [in]
The coordinates of the upper-left cell in the lpBuffer parameter that receives the data read from the console screen buffer. The
X member of the COORD structure is the column, and the
Y member is the row.
意思是不是这个dwBufferCoord指示的是lpBuffer缓冲区的最左上坐标??
但是在用的时候感觉不是这样。。。
我本来是想把一块区域显示的数据保存下来,然后在那块区域打印其他的文字,最后再恢复成原来的数据,测试的代码是:
#include <windows.h>
#include <iostream>
#include <stdio.h>
#include <conio.h>
using namespace std;
int main(){
const int X = 4;
const int Y = 5;
const int MROW = 10;
const int MCOL = 30;
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
SMALL_RECT rect;
CHAR_INFO buffer[MROW * MCOL]; //lpBuffer
COORD coordBufSize; //dwBufferSize
COORD coordBufCoord; // dwBufferCoord
int i, j;
for (i = 0; i < 20; i++) {
cout << i % 10;
for (j = 1; j <= 50; j++)
cout << j % 10;
cout << endl;
}
coordBufSize.Y = MROW;
coordBufSize.X = MCOL;
coordBufCoord.X = 2; //???????
coordBufCoord.Y = 3; //???????
rect.Top = Y;
rect.Left = X;
rect.Bottom = MROW + Y;
rect.Right = MCOL + X;
if (!ReadConsoleOutput(hout, buffer, coordBufSize, coordBufCoord, &rect)) {
printf("ReadConsoleOutput failed - (%d)\n", GetLastError());
return 0;
}
_getch();
for (i = 0; i < MROW; i++) {
gotoxy(hout, X, Y + i);
for (j = 0; j < MCOL; j++)
cout << (char)(i + 97);
}
_getch();
if (!WriteConsoleOutput(hout, buffer, coordBufSize, coordBufCoord, &rect)) {
printf("WriteConsoleOutput failed - (%d)\n", GetLastError());
return 0;
}
SetConsoleCursorPosition(hout, { 0,20 });
return 0;
}
结果是这样:(对不起发不了图片。。。)
012345678901234567890123456789012345678901234567890
112345678901234567890123456789012345678901234567890
212345678901234567890123456789012345678901234567890
312345678901234567890123456789012345678901234567890
412345678901234567890123456789012345678901234567890
51234567890123456789012345678901aa45678901234567890
61234567890123456789012345678901bb45678901234567890
71234567890123456789012345678901cc45678901234567890
81234567890123456789012345678901dd45678901234567890
91234567890123456789012345678901ee45678901234567890
01234567890123456789012345678901ff45678901234567890
11234567890123456789012345678901gg45678901234567890
2123hhhhhhhhhhhhhhhhhhhhhhhhhhhhhh45678901234567890
3123iiiiiiiiiiiiiiiiiiiiiiiiiiiiii45678901234567890
4123jjjjjjjjjjjjjjjjjjjjjjjjjjjjjj45678901234567890
512345678901234567890123456789012345678901234567890
612345678901234567890123456789012345678901234567890
712345678901234567890123456789012345678901234567890
812345678901234567890123456789012345678901234567890
912345678901234567890123456789012345678901234567890感觉就是dwBufferCoord这个参数X/Y设成几,缓冲区就会少保存几列/行??
当它X,Y都设为0之后就能完全实现我想要的功能了。。。但是我不懂是是为什么它是右边和下面少啊,根据文档的描述不是应该是跳过前面吗。。。