如何使用SetConsoleCursorPosition函数

A7A*_*A7A 1 c winapi graphic

我只是用C编写了Hanoi塔的代码,我想使用字符以图形方式显示解决方案。

我想使用windows.h和SetConsoleCursorPosition函数在控制台中移动光标。

您能否通过告诉我此功能是否有效以及如何使用来帮助我,请举一些例子。

rek*_*ire 6

这是一个如何调用SetConsoleCursorPosition函数的示例,取自cplusplus

void GoToXY(int column, int line)
{
    // Create a COORD structure and fill in its members.
    // This specifies the new position of the cursor that we will set.
    COORD coord;
    coord.X = column;
    coord.Y = line;

    // Obtain a handle to the console screen buffer.
    // (You're just using the standard console, so you can use STD_OUTPUT_HANDLE
    // in conjunction with the GetStdHandle() to retrieve the handle.)
    // Note that because it is a standard handle, we don't need to close it.
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    // Finally, call the SetConsoleCursorPosition function.
    if (!SetConsoleCursorPosition(hConsole, coord))
    {
        // Uh-oh! The function call failed, so you need to handle the error.
        // You can call GetLastError() to get a more specific error code.
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

您还可以通过查看SDK文档来了解如何使用Win32函数。谷歌搜索功能名称通常会在第一次点击时打开相应的文档页面。
对于SetConsoleCursorPosition,页面在这里,对于GetStdHandle页面,在这里