none
请教关于函数ReadConsoleOutput()问题 RRS feed

  • 问题

  • 查阅了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之后就能完全实现我想要的功能了。。。但是我不懂是是为什么它是右边和下面少啊,根据文档的描述不是应该是跳过前面吗。。。

    2016年5月18日 18:14

全部回复

  • Hi xlymoyixi

    感谢在msdn论坛发帖。

    dwBufferCoord指的是目标缓冲区矩形的左上角坐标,也就是读取的数据从该坐标开始复制。而XY则是该目标缓冲区矩形的行数和列数。ReadConsoleOutput函数的作用就是从控制台屏幕缓冲区中读取字符和颜色数据,并存储到该函数指定位置的目标缓冲区中。

    在函数说明中有以下几段:

    >> If the corresponding cell is outside the boundaries of the destination buffer rectangle (whose dimensions are specified by the dwBufferSize parameter), the data is not copied.

    Cells in the destination buffer corresponding to coordinates that are not within the boundaries of the console screen buffer are left unchanged.

    If the rectangle specified by lpReadRegion lies completely outside the boundaries of the console screen buffer, or if the corresponding rectangle is positioned completely outside the boundaries of the destination buffer, no data is copied. In this case, the function returns with the members of the structure pointed to by the lpReadRegion parameter set such that the Right member is less than the Left, or the Bottom member is less than the Top. To determine the size of the console screen buffer, use the GetConsoleScreenBufferInfo function.

    对应的意思就是目标缓冲区矩阵是以左侧和顶部为优先,与你相对应的结果吻合。

    Best Regards,

    Sera Yu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.

    2016年5月19日 1:30