C # simulate the mouse (mouse_event ),

Source: Internet
Author: User

C # simulate the mouse (mouse_event ),

Many people may encounter the small function of simulating mouse clicks during project development.

Baidu used the mouse_event function later, but I don't want to discuss how to use the mouse_event function.

How to use a function is meaningless.

        static void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo)        {            int x = dx, y = dy;            edit_position(dwFlags, dx, dy, ref x, ref y);             IntPtr hWndFromPoint = WindowFromPoint(x, y);            screen_to_client(hWndFromPoint, ref x, ref y);             send_message(hWndFromPoint, dwFlags, cButtons, x, y);        }

What did you find in the above Code? If you find that you know what this article is about, maybe you

I will be interested in reading it, but I think it's okay that I am working on the construction site.

When the mouse clicks the target, the message will be delivered to the target window that the mouse clicks.

Different messages are delivered. The complete "left-click mouse" event process is "WM_LBUTTONDOWN +

WM_LBUTTONUP "indicates that the mouse is" left-click to press + and then left-click to lift up ", because mouse_event can simulate

The mouse clicking process is not directly a complete Mouse clicking process, so there is also a "Press, lift"

mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP | MOUSEEVENTF_MOVE, -450, 0, 1, 0);
The mouse_event will not be moved to the relative position when the MOUSEEVENTF_MOVE volume is not provided,

"Cursor relative position = current cursor position + new cursor position" if the cursor volume is "MOUSEEVENTF_ABSOLUTE"

Absolute position, the cursor position will be taken as the standard instead of the current position of the cursor"

        static void edit_position(int dwFlags, int dx, int dy, ref int x, ref int y)        {            Point pos = MousePosition;            x = x + pos.X;            y = y + pos.Y;            if ((dwFlags | MOUSEEVENTF_ABSOLUTE) == dwFlags)                SetCursorPos(dx, dy);            if ((dwFlags | MOUSEEVENTF_MOVE) == dwFlags)                SetCursorPos(x, y);        }

The edit_position function is mainly used to move MOUSEEVENTF_MOVE to MOUSEEVENTF_ABSOLUTE.

Support for modification of relative/absolute cursor positions

        static void send_message(IntPtr hWnd, int dwFlags, int cButtons, int x, int y)        {            if ((dwFlags | MOUSEEVENTF_LEFTDOWN) == dwFlags)                SendMessage(hWnd, WM_LBUTTONDOWN, cButtons, MakeDWord(x, y));            if ((dwFlags | MOUSEEVENTF_LEFTUP) == dwFlags)                SendMessage(hWnd, WM_LBUTTONUP, cButtons, MakeDWord(x, y));            if ((dwFlags | MOUSEEVENTF_RIGHTDOWN) == dwFlags)                SendMessage(hWnd, WM_RBUTTONDOWN, cButtons, MakeDWord(x, y));            if ((dwFlags | MOUSEEVENTF_RIGHTUP) == dwFlags)                SendMessage(hWnd, WM_RBUTTONUP, cButtons, MakeDWord(x, y));            if ((dwFlags | MOUSEEVENTF_MIDDLEDOWN) == dwFlags)                SendMessage(hWnd, WM_MBUTTONDOWN, cButtons, MakeDWord(x, y));            if ((dwFlags | MOUSEEVENTF_MIDDLEUP) == dwFlags)                SendMessage(hWnd, WM_MBUTTONUP, cButtons, MakeDWord(x, y));        }

The send_message function is mainly used to simulate the mouse clicking process. As I mentioned above, "click + and then lift"

In the above code, you can see clearly. If you can try it on the contrary, what are the consequences?

It's better to do it yourself.

        static int MakeDWord(int low, int high)        {            return low + (high * Abs(~ushort.MaxValue));        }        static int Abs(int value)        {            return ((value >> 31) ^ value) - (value >> 31);        }
MakeDWord/merge integers. The function combines two short values into an int, which is divided into two parts: low and high.

        static bool screen_to_client(IntPtr hwnd, ref int x, ref int y)        {            bool bRetVal = false;            Point lpptPos = new Point(x, y);            if ((bRetVal = ScreenToClient(hwnd, ref lpptPos)))            {                x = lpptPos.X;                y = lpptPos.Y;            }            return bRetVal;        }
The screen_to_client function is used to convert the coordinates on the screen to the corresponding coordinates on the window client.

Public const int WM_LBUTTONDOWN = 513; // press the left mouse button to press public const int WM_LBUTTONUP = 514; // the left mouse button raises public const int WM_RBUTTONDOWN = 516; // right-click public const int WM_RBUTTONUP = 517; // right-click to raise public const int WM_MBUTTONDOWN = 519; // press public const int WM_MBUTTONUP = 520 in the middle mouse corner; // hold the public const int MOUSEEVENTF_MOVE = 0x0001 in the middle mouse corner; // move the public const int MOUSEEVENTF_LEFTDOWN = 0x0002 in the middle mouse corner; // press the left mouse button to press public const int MOUSEEVENTF_LEFTUP = 0x0004; // press the left mouse button to raise public const int MOUSEEVENTF_RIGHTDOWN = 0x0008; // right-click public const int MOUSEEVENTF_RIGHTUP = 0x0010; // right-click and choose public const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; // press public const int MOUSEEVENTF_MIDDLEUP = 0x0040 in the middle mouse corner; // hold public const int MOUSEEVENTF_ABSOLUTE = 0x8000 in the middle mouse corner; // absolute Coordinate

        [DllImport("user32.dll", SetLastError = true)]        public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, int lParam);        [DllImport("user32.dll", SetLastError = true)]        public static extern IntPtr WindowFromPoint(int xPoint, int yPoint);        [DllImport("user32.dll", SetLastError = true)]        public static extern int SetCursorPos(int x, int y);        [DllImport("user32.dll", SetLastError = true)]        public static extern bool ScreenToClient(IntPtr hWnd, ref Point lppt);        // [DllImport("user32", SetLastError = true)]        // public static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
Right-click (silent ):

mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 1, 0);

Double-click with the left mouse button (silent ):

mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 2, 0);
Move the mouse (relative position ):

mouse_event(MOUSEEVENTF_MOVE, 100, 50, 0, 0);
Move the mouse (absolute position ):

mouse_event(MOUSEEVENTF_ABSOLUTE, 100, 50, 0, 0);
Well, that's all. It's hard to understand the truth from the Code if there are too many dizzy words.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.