如何在 Win32 控制台应用程序中设置光标位置?最好,我想避免制作句柄并使用 Windows 控制台功能。 (我花了整个早上沿着那条黑暗的小巷跑;它产生的问题比它解决的问题还要多。)我似乎记得当我在大学时使用 stdio 做这件事相对简单,但我现在找不到任何如何做的例子。任何想法或建议将不胜感激。谢谢。
其他详细信息
这是我现在正在尝试做的事情:
COORD pos = {x, y};
HANDLE hConsole_c = CreateConsoleScreenBuffer( GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL );
char * str = "Some Text\r\n";
DWDORD len = strlen(str);
SetConsoleCursorPosition(hConsole_c, pos);
WriteConsole(hConsole_c, str, len, &dwBytesWritten, NULL);
CloseHandle(hConsole_c)
文本字符串str
永远不会发送到屏幕。还有什么我应该做的吗?谢谢。
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 morning running down that dark alley; it creates more problems than it solves.) I seem to recall doing this relatively simply when I was in college using stdio, but I can't find any examples of how to do it now. Any thoughts or suggestions would be greatly appreciated. Thanks.
Additional Details
Here is what I am now trying to do:
COORD pos = {x, y};
HANDLE hConsole_c = CreateConsoleScreenBuffer( GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL );
char * str = "Some Text\r\n";
DWDORD len = strlen(str);
SetConsoleCursorPosition(hConsole_c, pos);
WriteConsole(hConsole_c, str, len, &dwBytesWritten, NULL);
CloseHandle(hConsole_c)
The text string str
is never sent to the screen. Is there something else that I should be doing? Thanks.
发布评论
评论(5)
使用控制台功能时,您可以使用
SetConsoleCursorPosition
。如果没有它们(或者至少不直接使用它们),您可以在 gotoxy 的内容“noreferrer">ncurses 库。编辑:它的包装非常简单:
Using the console functions, you'd use
SetConsoleCursorPosition
. Without them (or at least not using them directly), you could use something likegotoxy
in the ncurses library.Edit: a wrapper for it is pretty trivial:
请参阅 SetConsoleCursorPosition API
编辑:
WriteConsoleOutputCharacter() 获取控制台中活动缓冲区的句柄,并允许您设置其位置。
See SetConsoleCursorPosition API
Edit:
Use WriteConsoleOutputCharacter() which takes the handle to your active buffer in console and also lets you set its position.
是的,您忘记调用
SetConsoleActiveScreenBuffer
。创建您自己的到底有什么意义?使用 GetStdHandle(STD_OUTPUT_HANDLE) 获取现有控制台的句柄。Yeah, you forgot to call
SetConsoleActiveScreenBuffer
. What exactly was the point of creating your own? UseGetStdHandle(STD_OUTPUT_HANDLE)
to get a handle to the existing console.您可能使用的是 ANSI excape 代码序列,它不适用于 Windows 32 位控制台应用程序。
You were probably using ANSI excape code sequences, which do not work with Windows 32-bit console applications.