在处理界面问题的时候,有时需要模拟鼠标移动或者点击来快速解决问题。比如界面某个区域或者某个按钮没有重画,把鼠标移上去或者点击一下后就正常了,而有时候又一时找不到原因,可以模拟鼠标移动点击来实现你想要的效果。
处理过程就是先记住原始鼠标坐标,再移动到你想要的坐标或者点击,之后再移到原始坐标点。
代码如下:
//模拟鼠标点击,双击
void ClickPoint(int x,int y,BOOL isDbClick) { //点击第一步 CPoint pointOld; GetCursorPos(&pointOld);//保存鼠标初始位置 SetCursorPos(x,y); mouse_event( MOUSEEVENTF_LEFTDOWN, 0,0,0,NULL);//鼠标down事件 mouse_event( MOUSEEVENTF_LEFTUP, 0,0,0,NULL);//鼠标up事件 if (isDbClick) { Sleep(200); mouse_event( MOUSEEVENTF_LEFTDOWN, 0,0,0,NULL);//鼠标down事件 mouse_event( MOUSEEVENTF_LEFTUP, 0,0,0,NULL);//鼠标up事件 } SetCursorPos(pointOld.x,pointOld.y);//还原鼠标初始位置 Sleep(300); }模拟键盘输入
void inputMobilephone(CString mobilephone) { for (int i=0,nCount = mobilephone.GetLength();i<nCount;i++) { keybd_event(mobilephone.GetAt(i),0,0,0); Sleep(200); keybd_event(mobilephone.GetAt(i),0,KEYEVENTF_KEYUP,0); } }模拟鼠标滚轮前滚
void upWheel(int x,int y) { CPoint pointOld; GetCursorPos(&pointOld);//保存鼠标初始位置 SetCursorPos(x,y); for (int i=0;i<60;i++) { mouse_event( MOUSEEVENTF_WHEEL, 0,0,WHEEL_DELTA*3,NULL);//滚轮向上滚动 Sleep(35); } SetCursorPos(pointOld.x,pointOld.y);//还原鼠标初始位置 }