繁体   English   中英

'具体'使用此代码进行双缓冲?

[英]'Specific' Double buffering with this code?

对于这段代码,我试图实现双缓冲,以便在Windows 10中我的控制台窗口上更新std::cout时不会闪烁。在我当前的代码中实现它的最佳方法是什么? 我正在查看一些Microsoft文档,但我无法找到合并它的方法可以这么说?

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来创建一个屏幕外缓冲区。 然后在调用FillConsoleOutputCharacterFillConsoleOutputAttribute等时将句柄传递给该屏幕缓冲区,根据需要清除/填充它。当它准备好供用户查看时,调用SetConsoleActiveScreenBuffer使其成为控制台的活动缓冲区。

请注意,在大多数情况下,您不希望每次清除屏幕时都创建新的屏幕缓冲区 - 而是您可能希望在启动程序时创建两个屏幕缓冲区,并在写入时在两者之间切换。显示输出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM