光标位置 c++获取光标位置方法
在下面的代码中,我想要鼠标光标在屏幕上的位置,但是无论我移动光标,我都会从下面第二个加星号或粗体(不确定)部分(无论光标在哪里)得到相同的输出:-1957298293 343277548。如果有人有更好的方法来获取光标位置或修复我的代码,请提供帮助。 (顺便说一句,“HANDLE csbiHandle; CONSOLE_SCREEN_BUFFER_INFO csbi;”不是必需的。它们在我之前的方法中使用过,但也失败了)
#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <cstdio>
#include <stdio.h>
#ifndef MOUSE_HWHEELED
#define MOUSE_HWHEELED 0x0008
#endif
using namespace std;
int main()
{
LPPOINT point;
HANDLE csbiHandle;
CONSOLE_SCREEN_BUFFER_INFO csbi;
int counter = 0;
DWORD cNumRead, i,fdwMode, fdwSaveOldMode;
INPUT_RECORD irInputBuffer[128];
HANDLE stdHandle;
stdHandle = GetStdHandle(STD_INPUT_HANDLE);
MOUSE_EVENT_RECORD mer;
cout << "|-------------|" << endl
<< "| A |" << endl
<< "|-------------|" << endl;
while(counter++<1000)
{
buttonpress:
ReadConsoleInput(stdHandle, irInputBuffer,128, &cNumRead);
**GetCursorPos(point);**
for(i=0; i<cNumRead; i++)
{
switch(irInputBuffer[i].EventType)
{
case MOUSE_EVENT:
{
mer = irInputBuffer[i].Event.MouseEvent;
if(mer.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
{
cout << "left button press" << endl;
**cout << point->x << " " << point->y << endl;**
}
else
{
goto buttonpress;
}
break;
}
default:{
printf("unknown\n");
break;}
}
}
}
return 0;
}
In the following code, I want the position of the mouse cursor on the screen, but wherever I move the cursor, I get the same output from the second starred or bolded(not sure) part below(wherever the cursor is): -1957298293 343277548. If anyone has a better method of getting the cursor position or a fix for my code, please help. (Just by the way, the "HANDLE csbiHandle; CONSOLE_SCREEN_BUFFER_INFO csbi;" aren't necessary. They were used in my previous method which also failed)
#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <cstdio>
#include <stdio.h>
#ifndef MOUSE_HWHEELED
#define MOUSE_HWHEELED 0x0008
#endif
using namespace std;
int main()
{
LPPOINT point;
HANDLE csbiHandle;
CONSOLE_SCREEN_BUFFER_INFO csbi;
int counter = 0;
DWORD cNumRead, i,fdwMode, fdwSaveOldMode;
INPUT_RECORD irInputBuffer[128];
HANDLE stdHandle;
stdHandle = GetStdHandle(STD_INPUT_HANDLE);
MOUSE_EVENT_RECORD mer;
cout << "|-------------|" << endl
<< "| A |" << endl
<< "|-------------|" << endl;
while(counter++<1000)
{
buttonpress:
ReadConsoleInput(stdHandle, irInputBuffer,128, &cNumRead);
**GetCursorPos(point);**
for(i=0; i<cNumRead; i++)
{
switch(irInputBuffer[i].EventType)
{
case MOUSE_EVENT:
{
mer = irInputBuffer[i].Event.MouseEvent;
if(mer.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
{
cout << "left button press" << endl;
**cout << point->x << " " << point->y << endl;**
}
else
{
goto buttonpress;
}
break;
}
default:{
printf("unknown\n");
break;}
}
}
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能很幸运,您的程序没有立即崩溃。
GetCursorPos
函数获取LPPOINT
作为参数,但这并不意味着您应该声明该类型的变量。相反,你应该这样做:原因是你的 LPPOINT 在调用时是一个指向“某处”的指针,没有人能说它指向哪里。因此,它很可能指向只读内存,而 Windows 很好地检查了这一点,并且不写入该内存,而是返回
FALSE
。您没有检查函数调用的返回值,因此您无法知道它是否成功。You can be lucky that your program didn't crash right away. The
GetCursorPos
function gets anLPPOINT
as parameter, but that doesn't mean you should declare a variable of that type. Instead, you should do this:The reason is that your LPPOINT, at the time of the call, is a pointer that points "somewhere", and nobody can say where it points. So it could very well be that it points to read-only memory, and Windows is so nice to check this and don't write to that memory, but instead returns
FALSE
. You didn't check the return value of the function call, so you cannot know whether it was successful or not.您正在将未初始化的指针传递给 GetCursorPos() 方法。这是未定义行为的典型案例。这通常会导致崩溃,但在你的情况下情况有所不同。不知道为什么你总是在那里读取相同的值:最重要的是不应该这样做。
正确的方法是将有效的指针传递给该函数。它可以通过将指针传递给本地或全局 POINT 变量或通过在堆中分配 POINT 变量来完成。
这是正确的局部变量的情况:
这是堆分配变量的情况:
you're passing an uninitialized pointer to the GetCursorPos() method. This is the classic case of undefined behaviour. This usually causes crash, but in your case it's something different. There's no telling why you're reading the same value there all the time: the most important thing is that it should not be done this way.
The correct way is to pass a valid pointer into this function. It can be done either by passing pointer to a local or global POINT variable or by allocating POINT variable in heap.
Here's the case of proper local variable:
and here's the case of the heap-allocated variable: