windwos获取鼠标坐标
时间: 2023-07-28 15:07:55 浏览: 47
在 Windows 中,可以使用以下两个 API 获取鼠标位置:
1. `GetCursorPos`:获取当前鼠标位置,返回一个 POINT 结构体,其中 x 和 y 分别表示横纵坐标。
2. `GetMessagePos`:获取最近一次鼠标消息的位置,返回一个 DWORD 类型的值,其中低 16 位表示横坐标,高 16 位表示纵坐标。
下面是一个简单的示例代码,演示如何获取鼠标位置:
```c++
#include <Windows.h>
#include <iostream>
int main()
{
POINT pt;
GetCursorPos(&pt); // 获取当前鼠标位置
std::cout << "Current position: (" << pt.x << ", " << pt.y << ")" << std::endl;
DWORD dwPos = GetMessagePos(); // 获取最近一次鼠标消息的位置
int x = LOWORD(dwPos); // 获取横坐标
int y = HIWORD(dwPos); // 获取纵坐标
std::cout << "Last message position: (" << x << ", " << y << ")" << std::endl;
return 0;
}
```
需要注意的是,鼠标位置是相对于屏幕左上角的坐标,而非当前窗口的坐标。如果需要将屏幕坐标转换为窗口坐标,可以使用 `ScreenToClient` 函数。