c++vc获得鼠标坐标

来源:百度知道 编辑:UC知道 时间:2024/06/24 12:42:47
#include <iostream>
using namespace std;
int main()
{
long x,long y;
{~~~~~}
cout>>x>>y;
return 0;
}

这是普通的cpp文件 vc6环境windows 环境
在大括号里加点东西
随便加什么 只要 使得最后 x y 为鼠标点击时的坐标就行
要开什么库也一并在前面写上
谢谢

如果作为一个命令行的程序,要获取鼠标单击事件比较麻烦。比较简单的就是用WINAPI:GetCursorPos函数直接获取鼠标的当前位置,这样可以先把鼠标移动到想知道位置的地方,然后调用那个API就可以了,例如下面这个程序每隔1秒就打印鼠标的位置:

#include <iostream>
#include <windows.h>

using namespace std;

int main(void)
{
POINT p;
while (1)
{
if (GetCursorPos(&p))
{
cout<<p.x<<"\t"<<p.y<<endl;
}
Sleep(1000);
}

return 0;
}

头文件就是多加个Windows.h就行了。

// 下面是完整程序 prog.cpp
// 编译时要链接 User32.lib
// 例如,在DOS窗打命令 带此库:
// CL prog.cpp User32.lib
// 得到 prog.exe

#include <windows.h>
#include <winuser.h>
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
POINT p;
GetCursorPos(&p);
cout<< "The cursor x is: " << p.x << " and y is: " << p.y <<endl;