C ++ mouse simulation program and mouse simulation program

Source: Internet
Author: User

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





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.