c++類比滑鼠事件

來源:互聯網
上載者:User

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:

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

    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.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.