Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ ';具体';用这个代码进行双缓冲?_C++_Winapi - Fatal编程技术网

C++ ';具体';用这个代码进行双缓冲?

C++ ';具体';用这个代码进行双缓冲?,c++,winapi,C++,Winapi,对于这段代码,我试图实现双缓冲,以便在windows 10中更新我的控制台窗口上的std::cout时它不会闪烁。在我当前的代码中实现这一点的最佳方法是什么?我正在看,但我想不出一种方法来合并它,可以这么说吗 void ClearScreen() { HANDLE hStdOut; CONSOLE_SCREEN_BUFFER_INFO csbi; DWORD count; DWORD

对于这段代码,我试图实现双缓冲,以便在windows 10中更新我的控制台窗口上的
std::cout
时它不会闪烁。在我当前的代码中实现这一点的最佳方法是什么?我正在看,但我想不出一种方法来合并它,可以这么说吗

void ClearScreen()
{
    HANDLE                     hStdOut;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    DWORD                      count;
    DWORD                      cellCount;
    COORD                      homeCoords = { 0, 0 };

    homeCoords.X = 0;
    homeCoords.Y = 0;

    hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    if (hStdOut == INVALID_HANDLE_VALUE) return;

    /* Get the number of cells in the current buffer */
    if (!GetConsoleScreenBufferInfo(hStdOut, &csbi)) return;
    cellCount = csbi.dwSize.X * csbi.dwSize.Y;

    /* Fill the entire buffer with spaces */
    if (!FillConsoleOutputCharacter(
        hStdOut,
        (TCHAR) ' ',
        cellCount,
        homeCoords,
        &count
    )) return;

    /* Fill the entire buffer with the current colors and attributes */
    if (!FillConsoleOutputAttribute(
        hStdOut,
        csbi.wAttributes,
        cellCount,
        homeCoords,
        &count
    )) return;

    /* Move the cursor home */
    SetConsoleCursorPosition(hStdOut, homeCoords);
}

基本思想是调用
CreateConsoleScreenBuffer
创建屏幕外缓冲区。然后,在调用
FillConsoleOutputCharacter
FillConsoleOutputAttribute
等时,通过将句柄传递到该屏幕缓冲区来根据需要清除/填充它。当它准备好供用户查看时,调用
SetConsoleActivesScreenbuffer
使其成为控制台的活动缓冲区


请注意,在大多数情况下,您不希望每次清除屏幕时都创建一个新的屏幕缓冲区,相反,您可能希望在启动程序时创建两个屏幕缓冲区,并在写入和显示输出时在这两个屏幕缓冲区之间进行交替。

此代码中没有
cout
。此外,双缓冲是一种图形输出技术,而不是文本。如果控制台闪烁,这是控制台的一个功能。我认为下面的内容很明显,因为cout是一个很常见的东西,这是隐含的,如果你是初学者,我编写了示例代码来说明这一点:
std::cout