Setting the Cursor Position in a Win32 Console Application

前端 未结 5 1420
走了就别回头了
走了就别回头了 2020-11-30 08:42

How can I set the cursor position in a Win32 Console application? Preferably, I would like to avoid making a handle and using the Windows Console Functions. (I spent all m

5条回答
  •  执念已碎
    2020-11-30 09:41

    Using the console functions, you'd use SetConsoleCursorPosition. Without them (or at least not using them directly), you could use something like gotoxy in the ncurses library.

    Edit: a wrapper for it is pretty trivial:

    // Untested, but simple enough it should at least be close to reality...
    void gotoxy(int x, int y) { 
        COORD pos = {x, y};
        HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
        SetConsoleCursorPosition(output, pos);
    }
    

提交回复
热议问题