c++模拟鼠标事件
来源:互联网 发布:网络借贷管理办法 限额 编辑:程序博客网 时间:2024/05/18 11:17
c++模拟鼠标事件,一般可以通过mouse_event()和SendInPut()两种方法。mouse_event()在windows后期版本中逐渐被SendInPut()取代。SendInPut()模拟鼠标移动的事件中,标志位取值不同,输入坐标的意义也不同。简单来说就是,添加MOUSEEVENTF_ABSOLUTE标志位表示鼠标移动是通过绝对坐标定位,此时的坐标要通过转换。光标在屏幕中被分成65535个小块,可以通过如下转换:
double fx = x*(65535.0f/fScreenWidth);
double fy = y*(65535.0f/fScreenHeight);
若不使用MOUSEEVENTF_ABSOLUTE标志位,则坐标是相对前一坐标的位移。
SendInPut()鼠标事件使用如下结构:
typedef struct tagMOUSEINPUT { LONG dx; LONG dy; DWORD mouseData; DWORD dwFlags; DWORD time; ULONG_PTR dwExtraInfo;} MOUSEINPUT, *PMOUSEINPUT;
msdn中完整解释如下:
Type: LONG
The absolute position of the mouse, or the amount of motion since the last mouse event was generated, depending on the value of the dwFlags member. Absolute data is specified as the x coordinate of the mouse; relative data is specified as the number of pixels moved.
Type: LONG
The absolute position of the mouse, or the amount of motion since the last mouse event was generated, depending on the value of the dwFlags member. Absolute data is specified as the y coordinate of the mouse; relative data is specified as the number of pixels moved.
以下是摘自网络上的一段FAQ,代码演示了鼠标的几个具体操作。
Q: How can I emulate mouse events in an application?
A:
- There are two API fucntions that you can use:
- 'mouse_event()'.
- 'SendInput()'.
- Which of the two API functions should I use?
The 'mouse_event()' function has been superseded by 'SendInput()' on Window NT/2000/XP. Thus, on these operating systems you should use 'SendInput()' (unless you need to provide backward compatibility with Windows 98 etc.). This FAQ is based on 'SendInput()'. - Can I see some example on how to use 'SendInput()' to emulate a click with the left mouse button?Code:
void LeftClick ( )
{
INPUT Input={0};
// left down
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
::SendInput(1,&Input,sizeof(INPUT));
// left up
::ZeroMemory(&Input,sizeof(INPUT));
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
::SendInput(1,&Input,sizeof(INPUT));
} - How to use the function? The left click will be performed on the current position of the mouse cursor.Code:
LeftClick(); - Can I see some example on how to use 'SendInput()'
to emulate a click with the right mouse button?Code:
void RightClick ( )
{
INPUT Input={0};
// right down
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_RIGHTDOWN;
::SendInput(1,&Input,sizeof(INPUT));
// right up
::ZeroMemory(&Input,sizeof(INPUT));
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_RIGHTUP;
::SendInput(1,&Input,sizeof(INPUT));
} - How to use the function? The right click will be performed on the current position of the mouse cursor.Code:
RightClick(); - Can I see some example on how to use 'SendInput()' for emulating mouse movement?Code:
void MouseMove (int x, int y )
{
double fScreenWidth = ::GetSystemMetrics( SM_CXSCREEN )-1;
double fScreenHeight = ::GetSystemMetrics( SM_CYSCREEN )-1;
double fx = x*(65535.0f/fScreenWidth);
double fy = y*(65535.0f/fScreenHeight);
INPUT Input={0};
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_MOVE|MOUSEEVENTF_ABSOLUTE;
Input.mi.dx = fx;
Input.mi.dy = fy;
::SendInput(1,&Input,sizeof(INPUT));
} - How to use the function? This call will move the mouse cursor to the position 100/100 on the screen.Code:
MouseMove(100,100);
Thanks to cilu for helping writing this FAQ.
- c++--模拟鼠标事件
- C语言模拟鼠标事件
- 模拟鼠标点击事件
- 模拟鼠标键盘事件
- C# 模拟鼠标事件
- QT 模拟鼠标事件
- c++模拟鼠标事件
- 鼠标键盘事件模拟
- C# 模拟鼠标事件
- QT 模拟鼠标事件
- 模拟鼠标、键盘事件
- JavaScript模拟鼠标事件
- web-模拟鼠标事件
- QT 模拟鼠标事件
- Python 模拟鼠标事件
- QT模拟鼠标事件
- Javascript事件模拟(鼠标事件、键盘事件)
- VB模拟一次鼠标事件
- Linux0.11中系统内存的使用
- How to handle crash problem?(4.VS 2005使用map文件查找程序崩溃原因)
- jacorb 的使用(二) 启动命名服务
- How to handle crash problem?(3.仅通过崩溃地址找出源代码的出错行)
- jacorb 的使用(三)启动通知服务
- c++模拟鼠标事件
- jacorb 的使用(四) idl 编译
- 传智播客-Java基础加强-day1
- How to handle crash problem?(2.Finding crash information using the MAP file 2)
- linux cmd
- 解决访问TFS时“文档”和“报告”打叉的问题
- How to handle crash problem? (1.结合Dr.Watson系统日志和Vc6来定位多线程环境下程序异常退出的错误)
- api file operation
- vs2008 试用版评估期结束的解决方法