The realization idea and code _c language of simulating mouse event

Source: Internet
Author: User

Simulating mouse events can generally be done by mouse_event () and SendInput () methods. Mouse_event () is gradually replaced by SendInput () in later versions of Windows. SendInput () analog mouse movement in the event, the flag bit value is different, the meaning of the input coordinates are also different. Simply put, adding a Mouseeventf_absolute flag indicates that the mouse movement is positioned through absolute coordinates, at which point the coordinates are converted. The cursor is divided into 65,535 small pieces on the screen, which can be transformed by the following:

Copy Code code as follows:

Double FX = x * (65535.0f/fscreenwidth);
Double fy = y * (65535.0f/fscreenheight);

If the Mouseeventf_absolute flag bit is not used, the coordinate is a displacement relative to the previous coordinate.

The SendInput () mouse time usage structure is as follows:

Copy Code code as follows:

typedef struct TAGMOUSEINPUT {
LONG DX;
LONG dy;
DWORD Mousedata;
DWORD dwflags;
DWORD time;
ULONG_PTR dwExtraInfo;
Mouseinput, *pmouseinput, far* lpmouseinput;

The full explanation in MSDN is as follows:
The absolute position of the DX mouse, or the value of the dwflags member resulting from the last mouse event's activity. The x coordinates of the mouse are specified as absolute data, relative to the number of moving pixels that are specified as data.
Dy the absolute position of the mouse or the value of the dwflags member resulting from the last mouse event's exercise. The y-coordinate of the mouse is specified as absolute data, relative to the number of moving pixels that are specified as data.
Mousedata If the dwflags contains mouseeventf_wheel,mousedata specifies the amount of wheel movement. A positive value indicates that the wheel rotates forward, away from the user, and a negative value indicates that the wheel rotates backwards, which is toward the user. The scroll wheel is defined as Wheel_delta, which is 120.

The following code demonstrates several specific actions of the mouse.
1. How can I simulate mouse events in my application?

There are two API functions that can be used: mouse_event () and SendInput ();

2, which API function should be used?

In Windows NT/2000/XP, the Mouse_event () function has been superseded by the SendInput () function. Therefore, on these operating systems, you should use the SendInput () function. (unless you need to provide backward compatibility with Windows98 etc.).

3, how to use the SendInput () function to simulate the click of the left mouse button?

Copy Code code as follows:

void Leftclick ()
{
INPUT input={0};
Left click
Input.type = Input_mouse;
Input.mi.dwFlags = Mouseeventf_leftdown;
:: SendInput (1,&input,sizeof (Input));

Left lift
:: ZeroMemory (&input,sizeof (Input));
Input.type = Input_mouse;
Input.mi.dwFlags = Mouseeventf_leftup;
:: SendInput (1,&input,sizeof (Input));
}

4, how to use the SendInput () function to simulate the right mouse button click?

Copy Code code as follows:

void RightClick ()
{
INPUT input={0};
Right-click the
Input.type = Input_mouse;
Input.mi.dwFlags = Mouseeventf_rightdown;
:: SendInput (1,&input,sizeof (Input));

Right-lift up
:: ZeroMemory (&input,sizeof (Input));
Input.type = Input_mouse;
Input.mi.dwFlags = Mouseeventf_rightup;
:: SendInput (1,&input,sizeof (Input));
}

5, how to use the SendInput () function to simulate the movement of the mouse?

Copy Code code as follows:

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));
}

6, how to use the SendInput () function to simulate the click of the mouse button?

Copy Code code as follows:

void Middleclick ()
{
INPUT input={0};
Set Wheel Volume
Input.type = Input_mouse;
Input.mi.dwFlags = Mouseeventf_wheel;
Input.mi.mouseData = 500;
:: SendInput (1,&input,sizeof (Input));
}

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.