C # program Simulation mouse operation [simulate Mouse Movement and Click programmatically]

Source: Internet
Author: User
Tags functions relative
Program | mouse

Sometimes, we need to simulate the movement of the mouse, click and so on in our program. -for example, a macro that reproduces the user's actions, or a demo program that demonstrates how to do it. So, we're in. NET, how to implement it?

. NET does not provide a function to change the mouse pointer position and simulate the click Action, but the Windows API provides. One of them is:
[DllImport ("user32.dll")]
static extern bool Setcursorpos (int X, int Y);
This function can change the position of the mouse pointer. Where X,y is the absolute position relative to the upper-left corner of the screen.
Another function is:

[DllImport ("user32.dll")]
static extern void Mouse_event (mouseeventflag flags, int dx, int dy, uint data, uintptr extraInfo);
This function can not only set the absolute position of the mouse pointer, but also can be set in relative coordinates. In addition, the function can also simulate the mouse around key click, mouse wheel operation. The Mouseeventflag is an enumeration based on the uint type, defined as follows:

[Flags]
Enum Mouseeventflag:uint
{
move = 0x0001,
Leftdown = 0x0002,
Leftup = 0x0004,
Rightdown = 0x0008,
Rightup = 0x0010,
Middledown = 0x0020,
Middleup = 0x0040,
Xdown = 0x0080,
Xup = 0x0100,
Wheel = 0x0800,
Virtualdesk = 0x4000,
Absolute = 0x8000
}
For a detailed description of these two functions, you can view the MSDN Library or the Windows Platform SDK documentation.
The following demo (full source code, Vs.net 2005/c#) demonstrates how to use the above function to control the mouse movement to the taskbar and click the "Start" button.
(This program uses API functions such as FindWindowEx to find the taskbar and Start menu)
Click here to download



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.