在 Win32 控制台应用程序中设置光标位置

发布于 2024-08-30 20:15:37 字数 612 浏览 3 评论 0 原文

如何在 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

潜移默化 2024-09-06 20:15:37

使用控制台功能时,您可以使用 SetConsoleCursorPosition。如果没有它们(或者至少不直接使用它们),您可以在 gotoxy 的内容“noreferrer">ncurses 库。

编辑:它的包装非常简单:

// 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);
}

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);
}
简单气质女生网名 2024-09-06 20:15:37

请参阅 SetConsoleCursorPosition API

编辑:

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);

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);
一个人的旅程 2024-09-06 20:15:37

是的,您忘记调用 SetConsoleActiveScreenBuffer。创建您自己的到底有什么意义?使用 GetStdHandle(STD_OUTPUT_HANDLE) 获取现有控制台的句柄。

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.

挽梦忆笙歌 2024-09-06 20:15:37

您可能使用的是 ANSI excape 代码序列,它不适用于 Windows 32 位控制台应用程序。

You were probably using ANSI excape code sequences, which do not work with Windows 32-bit console applications.

岁月流歌 2024-09-06 20:15:37
#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;
}
#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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文