C++缓冲区备份失败

克里斯蒂安

我正在处理一个控制台 C++ 项目,在该项目中我需要备份控制台缓冲区的一个区域,以便我可以编辑它并在完成后恢复更改。我想出了这两个函数,但都存在问题,因为根据MSDN 错误代码列表,第一个输出错误代码1 [ERROR_INVALID_FUNCTION 1 (0x1) Incorrect function],第二个输出错误代码87 我还检查了ReadConsoleOutputWriteConsoleOutput API的文档,但找不到解决方案。[ERROR_INVALID_PARAMETER 87 (0x57) The parameter is incorrect]

void backupBuffer(PCHAR_INFO buffer, COORD pos, COORD size) {
    SMALL_RECT rect = { pos.X,  pos.Y, pos.X + size.X - 1, pos.Y + size.Y - 1 };
    COORD buffer_index = { 0, 0 };
    BOOL success = 
    ReadConsoleOutput(GetStdHandle(STD_OUTPUT_HANDLE),
                      buffer,
                      buffer_index,
                      pos,
                      &rect);
    if (!success) {
        int error = GetLastError();
                    cout << error << endl;
    }
}

void setBuffer(PCHAR_INFO buffer, COORD pos, COORD size) {
    SMALL_RECT rect = { pos.X,  pos.Y, pos.X + size.X - 1, pos.Y + size.Y - 1 };
    COORD buffer_index = { 0, 0 };
    BOOL success =
    WriteConsoleOutput(GetStdHandle(STD_OUTPUT_HANDLE),
                       buffer,
                       buffer_index,
                       pos,
                       &rect);
    if (!success) {
        int error = GetLastError();
        cout << error << endl;
    }
}

这是电话:

// width = 10, height = 10 and the console
// window dimensions are 30 x 120 but the buffer
// height should be bigger since I'm able to scroll
// down for a while before reaching the end. 
PCHAR_INFO buffer = new CHAR_INFO[width * height];
COORD pos, size;
pos.X = 20; pos.Y = 2;
size.X = width; size.Y = height;
backupBuffer(buffer, pos, size);

// Visual Stuff (cout in that area...)

setBuffer(buffer, pos, size);

我该怎么做才能解决这个问题?

索罗内尔·哈提尔

在您的两个功能中,您有:

COORD buffer_index = { 0, 0 };

该参数需要指定字符信息块的大小(在 CHAR_INFO 中)。现在你告诉它副本没有空间。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章