首页 > 解决方案 > 如何在换行后将 std::cout 输出带回顶部

问题描述

我有以下菜单,它应该根据用户是否键入了键F1或进行更新F2

int main()
{
    bool f1 = false;
    bool f2 = false;
 
    while (true)    
    {
        std::cout << "[F1]:  " << (f1 ? "ON" : "OFF") << std::endl;
        std::cout << "[F2]:  " << (f2 ? "ON" : "OFF") << std::endl;
        std::cout << "[INS] to quit" << std::endl;

        if (GetAsyncKeyState(VK_INSERT) & 0x1)
            break;

        if (GetAsyncKeyState(VK_F1) & 0x1)
            f1 = !f1;
        
        if (GetAsyncKeyState(VK_F2) & 0x1)
            f2 = !f2;
        
        Sleep(100);
        cleanWindow();
    }

    return 0;
}

现在,我以前使用system("cls")过,它工作“很好”,但有人告诉我,我宁愿使用 Win32 API 来清理控制台,所以我按照这篇 MSVC 文章cleanWindow()的描述创建了。

DWORD cleanWindow()
{
    HANDLE hStdOut;

    hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

    // Fetch existing console mode so we correctly add a flag and not turn off others
    DWORD mode = 0;
    if (!GetConsoleMode(hStdOut, &mode))
    {
        return ::GetLastError();
    }

    // Hold original mode to restore on exit to be cooperative with other command-line apps.
    const DWORD originalMode = mode;
    mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;

    // Try to set the mode.
    if (!SetConsoleMode(hStdOut, mode))
    {
        return ::GetLastError();
    }

    // Write the sequence for clearing the display.
    // \x1b[2J is the code for clearing the screen and set cursor to home
    DWORD written = 0;
    PCWSTR sequence = L"\x1b[2J";
    if (!WriteConsoleW(hStdOut, sequence, (DWORD)wcslen(sequence), &written, NULL))
    {
        // If we fail, try to restore the mode on the way out.
        SetConsoleMode(hStdOut, originalMode);
        return ::GetLastError();
    }

    // To also clear the scroll back, emit L"\x1b[3J" as well.
    // 2J only clears the visible window and 3J only clears the scroll back.

    // Restore the mode on the way out to be nice to other command-line applications.
    SetConsoleMode(hStdOut, originalMode);
}

现在,问题是“菜单”位于命令提示符的末尾而不是开头,就像它一样system("cls")

输出

我的问题是,我该如何解决这个问题?如何将输出带回外壳的顶部?

编辑:

我还编辑了cleanWindow()编写序列的函数:\033[2JL"\033[H"with WriteConsoleW(),它可以工作,但我仍然会得到与 with 一样的“闪烁”效果system("cls"),这是我试图避免的。

标签: c++winapi

解决方案


清除后,您可以使用SetConsoleCursorPosition将光标位置设置回左上角。

还有ANSI 转义码(类似于用于清除屏幕的转义码)可以让您重新定位光标。

"\033[r;cH"

替换为要移动到r的行和c列。它们是基于 1 的,默认位于左上角,因此您可以使用"\033[1;1H"或仅使用"\033[H"


推荐阅读