[英]Setting the Cursor Position in a Win32 Console Application
How can I set the cursor position in a Win32 Console application? 如何在Win32控制台应用程序中设置光标位置? Preferably, I would like to avoid making a handle and using the Windows Console Functions. 最好,我想避免使用Windows控制台功能。 (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. (我整个上午都在那条黑暗的小巷里奔跑;它造成的问题多于解决的问题。)我似乎回忆起当我在大学时使用stdio相对简单,但我现在找不到任何如何做的例子。 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. 文本字符串str
永远不会发送到屏幕。 Is there something else that I should be doing? 我还应该做些什么吗? Thanks. 谢谢。
Using the console functions, you'd use SetConsoleCursorPosition
. 使用控制台功能,您将使用SetConsoleCursorPosition
。 Without them (or at least not using them directly), you could use something like gotoxy
in the ncurses library. 没有它们(或者至少不直接使用它们),你可以在ncurses库中使用类似gotoxy
东西。
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);
}
See SetConsoleCursorPosition API 请参阅SetConsoleCursorPosition API
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);
Yeah, you forgot to call SetConsoleActiveScreenBuffer
. 是的,你忘了调用SetConsoleActiveScreenBuffer
。 What exactly was the point of creating your own? 创建自己的究竟是什么意思? Use GetStdHandle(STD_OUTPUT_HANDLE)
to get a handle to the existing console. 使用GetStdHandle(STD_OUTPUT_HANDLE)
获取现有控制台的句柄。
#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;
}
您可能正在使用ANSI excape代码序列 ,它不适用于Windows 32位控制台应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.