C++ 在 Win32 控制台应用程序中设置光标位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2732292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting the Cursor Position in a Win32 Console Application
提问by Jim Fell
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.
如何在 Win32 控制台应用程序中设置光标位置?最好,我想避免制作句柄和使用 Windows 控制台功能。(我整个上午都在这条黑暗的小巷里奔跑;它带来的问题比解决的问题要多。)我似乎记得我在大学使用 stdio 时这样做相对简单,但我现在找不到任何如何去做的例子. 任何想法或建议将不胜感激。谢谢。
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.
文本字符串str
永远不会发送到屏幕。还有什么我应该做的吗?谢谢。
采纳答案by cpx
See SetConsoleCursorPositionAPI
请参阅SetConsoleCursorPositionAPI
Edit:
编辑:
Use WriteConsoleOutputCharacter() which takes the handle to your active buffer in console and also lets you set its position.
使用 WriteConsoleOutputCharacter() 将句柄带到控制台中的活动缓冲区,还可以让您设置其位置。
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);
回答by Jerry Coffin
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 ncurseslibrary.
使用控制台功能,您将使用SetConsoleCursorPosition
. 如果没有他们(或至少不直接使用它们),你可以使用类似gotoxy
的ncurses的库。
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);
}
回答by Hans Passant
Yeah, you forgot to call SetConsoleActiveScreenBuffer
. What exactly was the point of creating your own? Use GetStdHandle(STD_OUTPUT_HANDLE)
to get a handle to the existing console.
是的,你忘记打电话了SetConsoleActiveScreenBuffer
。创建你自己的到底是什么?使用GetStdHandle(STD_OUTPUT_HANDLE)
得到的句柄现有的控制台。
回答by udit043
#include <windows.h>
#include <iostream.h>
using namespace std;
int main(int argc, char *argv[])
{
int x,y;
cin>>x>>y;
SetCursorPos(x,y); //set your co-ordinate
Sleep(500);
mouse_event(MOUSEEVENTF_LEFTDOWN,x,y,0,0); // moving cursor leftdown
mouse_event(MOUSEEVENTF_LEFTUP,x,y,0,0); // moving cursor leftup //for accessing your required co-ordinate
system("pause");
return EXIT_SUCCESS;
}
回答by Mark Ransom
You were probably using ANSI excape code sequences, which do not work with Windows 32-bit console applications.
您可能正在使用ANSI excape 代码序列,它不适用于 Windows 32 位控制台应用程序。