如何使用SetConsoleCursorPosition Func

时间:2013-04-02 17:52:17

标签: c winapi graphic

我刚刚在c中编写了hanoi塔的代码,我想用图形模式显示解决方案。

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

你能告诉我这个功能是否有效以及如何使用它可以帮助我吗?请举几个例子。

1 个答案:

答案 0 :(得分:5)

以下是如何调用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.
        // ...
    }
}

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