Setting the Cursor Position in a Win32 Console Application

前端 未结 5 1417
走了就别回头了
走了就别回头了 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:34

    See SetConsoleCursorPosition API

    Edit:

    Use WriteConsoleOutputCharacter() which takes the handle to your active buffer in console and also lets you set its position.

    int x = 5; int y = 6;
    COORD pos = {x, y};
    HANDLE hConsole_c = CreateConsoleScreenBuffer( GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
    SetConsoleActiveScreenBuffer(hConsole_c);
    char *str = "Some Text\r\n";
    DWORD len = strlen(str);
    DWORD dwBytesWritten = 0;
    WriteConsoleOutputCharacter(hConsole_c, str, len, pos, &dwBytesWritten);
    CloseHandle(hConsole_c);
    

提交回复
热议问题