c++模拟鼠标事件

原创 2011年01月10日 14:09:00

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中完整解释如下:

dx

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.

dy

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:


  • 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?


    Code:
    LeftClick();
    The left click will be performed on the current position of the mouse cursor.

  • 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?


    Code:

    RightClick();
    The right click will be performed on the current position of the mouse cursor.

  • 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?


    Code:

    MouseMove(100,100);
    This call will move the mouse cursor to the position 100/100 on the screen.



Thanks to cilu for helping writing this FAQ.

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

MFC 得到当前鼠标位置方法

CPoint   point;GetCursorPos(&point);
  • phay
  • phay
  • 2010-06-09 14:28
  • 22636

HDOJ1002 A + B Problem II

原谅我我这个时候才A掉这道经典的大数题目,因为用C++尝试了三四次一直A不掉,最后还是用Java  A掉的。 杭电OJ上用Java A题时要注意到一点的是类名必须是Main ,要不然会编译错误。 ...
  • appte
  • appte
  • 2013-03-02 20:01
  • 2854

SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)

使用SSM(Spring、SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方。之前没有记录SSM整合...

215. Kth Largest Element in an Array

215. Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that ...

Java实现敏感词过滤

package com.iris.nsonline.nsoweb.utils; import java.io.BufferedReader; import java.io.InputStream; ...

程序员8月书讯

7月书讯中奖名单: zejian_《 Android编程权威指南(第3版)》 奋斗的镇屌 《Swift编程权威指南(第2版)》 bit_kaki《 Android编程权威指南...

C++ - string类型转换int类型

string类型转换int类型本文地址: http://blog.csdn.net/caroline_wendyC语言转换形式:... std::string str; int i = atoi(st...

c++学习 - int 和 string 的相互转换

在C++中会碰到int和string类型转换的。string -> int首先我们先看两个函数:atoi这个函数是把char * 转换成int的。应该是属于标准库函数。在想把string 转换成int...

VMware宣布开放大部分虚拟机工具

贴出者为 Anonymous Anonymous写著 VMware宣布,开放“大部分”VMware工具作为开源软件,而且这只是作为VMware公司“Open-VM-Tools”项目的一部分。 VM...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)