SetConsoleCursorPosition() 函数的问题

Problem with the SetConsoleCursorPosition() function

我需要帮助,因为我无法让 SetConsoleCursorPosition() 函数工作,我做了一个 dll 项目,然后我分配了控制台及其主要功能(cout 和 cin),但我不知道如何使该功能也能正常工作,因为我没有转到我设置的行,因为代码忽略了该指令

void consolerefresh()
{
    static const HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD coord = { (SHORT)0, (SHORT)10 };
    SetConsoleCursorPosition(hConsole, coord);
    for (int i = 0; i < (sizeof(last) / sizeof(last[0])); i++)
    {
        if (last[i] != 2) {
            cout << last[i] << endl;
        }
            
    }
}

我的 allocConsole 代码:

void createconsole()
{
    AllocConsole();
    FILE* fDummy;
    freopen_s(&fDummy, "CONOUT$", "w", stdout);
    freopen_s(&fDummy, "CONOUT$", "w", stderr);
    freopen_s(&fDummy, "CONIN$", "r", stdin);
    std::cout.clear();
    std::clog.clear();
    std::cerr.clear();
    std::cin.clear();
   
}

编辑:我修复了代码中的错误,但它仍然不起作用。

根据 SetConsoleCursorPosition, the first argument must be "a handle to the console screen buffer". Per GetStdHandle,活动控制台屏幕缓冲区的句柄由 STD_OUTPUT_HANDLEnot STD_INPUT_HANDLE 返回控制台输入缓冲区。

使用正确的句柄将使 SetConsoleCursorPosition 正常工作。

HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);  // instead of STD_INPUT_HANDLE

[ EDIT ] 以下内容经验证可在 VS 2019 中使用默认向导生成的 Win 应用程序在 wWinMain.[=20 周围添加以下代码=]

//... wizard generated code

#include <stdio.h>
#include <iostream>

void consolerefresh()
{
    static const HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD coord = { 5, 5 };
    SetConsoleCursorPosition(hConsole, coord);
    std::cout << "at (5, 5)" << std::endl;
}

void createconsole()
{
    AllocConsole();
    FILE* fDummy;
    freopen_s(&fDummy, "CONOUT$", "w", stdout);
}

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    createconsole();
    consolerefresh();

    UNREFERENCED_PARAMETER(hPrevInstance);

//... rest of wizard generated code

运行 程序创建一个控制台 window 并显示以下内容。