C ++ mouse simulation program and mouse simulation program
Mouse simulation applications are not uncommon. Applications often have miraculous effects on game plug-ins or programs with frequent operations.
The old API is mouse_event, which I used at the beginning, but later I saw that the new API is more unified in operation, A slight modification can also simulate keyboard input (the two are often applied together), so we use a new API. By the way, the new API name is SendInput.
Next let's not talk nonsense. Let's directly run the Code. This code runs on the MFC project. To run it on the console or other projects, you must include the necessary header files. In addition, this program can only simulate General mouse operations, for some anti-plug-in program click need driver-level simulation.
Simulate various Mouse Action Functions
Void MouseMove (int x, int y) // move the cursor to the specified position {double fScreenWidth =: GetSystemMetrics (SM_CXSCREEN)-1; // obtain the screen resolution width double fScreenHeight = :: getSystemMetrics (SM_CYSCREEN)-1; // obtain the screen resolution height double fx = x * (6553520.f/fScreenWidth); double fy = y * (6553520.f/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);} void MouseLeftDown () // press the left mouse button {INPUT Input = {0}; Input. type = INPUT_MOUSE; Input. mi. dwFlags = MOUSEEVENTF_LEFTDOWN; SendInput (1, & Input, sizeof (INPUT);} void MouseLeftUp () // left click to open {INPUT Input = {0}; Input. type = INPUT_MOUSE; Input. mi. dwFlags = MOUSEEVENTF_LEFTUP; SendInput (1, & Input, sizeof (INPUT);} void MouseRightDown () // right-click and press {INPUT Input = {0}; Input. type = INPUT_MOUSE; Input. mi. dwFlags = MOUSEEVENTF_RIGHTDOWN; SendInput (1, & Input, sizeof (INPUT);} void MouseRightUp () // right-click and open {INPUT Input = {0}; Input. type = INPUT_MOUSE; Input. mi. dwFlags = MOUSEEVENTF_RIGHTUP; SendInput (1, & Input, sizeof (INPUT ));}
Various action functions work together to complete various mouse operations
// Select ShowWindow (SW_SHOWMINIMIZED) as the simulated mouse drag box; // minimize the POINT mypoint of the form; GetCursorPos (& mypoint); // obtain the current position of the mouse: MouseMove (800,100 0 ); // move the mouse to the specified position: MouseLeftDown (); // move the mouse to the left-click MouseMove (10, 10); // drag the mouse to the specified position: Sleep (10 ); // wait for a moment. Otherwise, the dragging will not work. MouseLeftUp (); // release the MouseMove (mypoint. x, mypoint. y); // place the cursor back
// Simulate right-clicking and releasing ShowWindow (SW_SHOWMINIMIZED); // minimize MouseRightDown (); Sleep (10); MouseRightUp ();