700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Windows下控制鼠标移动和点击的C语言实现

Windows下控制鼠标移动和点击的C语言实现

时间:2019-09-18 08:46:46

相关推荐

Windows下控制鼠标移动和点击的C语言实现

用VC控制台程序来实现。其实这个程序实现的关键就是调用windows api中的user32.dll中的两个函数就搞定了,废话不多说,直接上代码。以下是代码中的两个关键函数封装,完整可运行代码请到/detail/zjuman/9922444下载。

//this macro already defined//const int MOUSEEVENTF_MOVE = 0x0001; //移动鼠标//const int MOUSEEVENTF_LEFTDOWN = 0x0002; //模拟鼠标左键按下//const int MOUSEEVENTF_LEFTUP = 0x0004; //模拟鼠标左键抬起//const int MOUSEEVENTF_RIGHTDOWN = 0x0008; //模拟鼠标右键按下//const int MOUSEEVENTF_RIGHTUP = 0x0010; //模拟鼠标右键抬起//const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;//模拟鼠标中键按下//const int MOUSEEVENTF_MIDDLEUP = 0x0040; //模拟鼠标中键抬起//const int MOUSEEVENTF_ABSOLUTE = 0x8000; //标示是否采用绝对坐标/** mouse move* x -- int, x-coordinate* y -- int, y-coordinate*/int move(int x, int y){HINSTANCE hDll; typedef bool (*Fun1)(int,int);hDll = LoadLibrary("user32.dll");if(NULL == hDll) { fprintf(stderr, "load dll 'user32.dll' fail."); return -1; }Fun1 SetCursorPos = (Fun1)GetProcAddress(hDll, "SetCursorPos"); if(NULL == SetCursorPos) { fprintf(stderr, "call function 'SetCursorPos' fail."); FreeLibrary(hDll); return -1; }SetCursorPos(x,y);FreeLibrary(hDll); return 0;}/** mouse click* type-- int, 0:left click;1:right click* double_click -- bool, true:double click; false: single click*/int click(int type,bool double_click){int left_click = MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP;int right_click = MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP;int clicktype;HINSTANCE hDll; typedef void (*Fun2)(DWORD dwFlags, // motion and click optionsDWORD dx, // horizontal position or changeDWORD dy, // vertical position or changeDWORD dwData,// wheel movementULONG_PTR dwExtraInfo // application-defined information);hDll = LoadLibrary("user32.dll");if(NULL == hDll) { fprintf(stderr, "load dll 'user32.dll' fail."); return -1; }Fun2 mouse_event = (Fun2)GetProcAddress(hDll, "mouse_event"); if(NULL == mouse_event) { fprintf(stderr, "call function 'mouse_event' fail."); FreeLibrary(hDll); return -1; }if(type==0)clicktype = left_click;elseclicktype = right_click;mouse_event (clicktype, 0, 0, 0, 0 );FreeLibrary(hDll);if(double_click)click(type,false);return 0;}

————————————————

原文链接:/zjuman/article/details/76736517

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。