Simulate Mouse clicking and drag-and-drop operations in Windows

Source: Internet
Author: User

Address: http://blog.csdn.net/ariesjzj/article/details/8526103

You can use the following methods to simulate mouse input with a program:

1. sendmessage and postmessage, by sending a mouse message and simulating a mouse event, the advantage is that the window is still valid after being minimized.

2. mouse_event: Simulate the mouse operation. The window must be in front.

3. sendinput, which can be used as a simulated mouse or keyboard.

 

Here we use the first method to achieve click and drag-and-drop

Click:

void click_point(unsigned long x, unsigned long y){SendMessage(hwnd, WM_LBUTTONDOWN, NULL, MAKELPARAM(x, y));SendMessage(hwnd, WM_LBUTTONUP, NULL, MAKELPARAM(x, y));}

Drag and Drop:

void drag_drop(unsigned long x1, unsigned long y1,    unsigned long x2, unsigned long y2){#define MOVE_STEP50assert(hwnd);POINT point1, point2;point1.x = x1; point1.y = y1;point2.x = x2; point2.y = y2;SendMessage(hwnd, WM_LBUTTONDOWN, NULL, MAKELPARAM(point1.x, point1.y));if (x2 >= x1) {for (unsigned long i = x1; i < x2; i += MOVE_STEP) {Sleep(10);SendMessage(hwnd, WM_MOUSEMOVE, NULL, MAKELPARAM(i, point1.y));}} else {for (unsigned long i = x1; i > x2; i -= MOVE_STEP) {Sleep(10);SendMessage(hwnd, WM_MOUSEMOVE, NULL, MAKELPARAM(i, point1.y));}}SendMessage(hwnd, WM_LBUTTONUP, NULL, MAKELPARAM(point2.x, point2.y));}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.