C # simulate the mouse operation in the program [simulate mouse movement and click programmatically]

Source: Internet
Author: User
Sometimes, we need Program To simulate the movement and clicking of the mouse. -- For example, a macro that recreates user operations or a demo program that demonstrates operation methods. So how can we implement it in. Net?

. Net does not provide a function to change the mouse pointer position and simulate click operations. However, Windows API provides. One of them is:[Dllimport ("User32.dll")]
Static Extern BoolSetcursorpos (IntX,IntY );

This function can change the cursor position. X and Y are absolute 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 be set with relative coordinates. In addition, this function can simulate left and right mouse clicks, scroll wheel operations, and so on. The mouseeventflag is a uint-type enumeration, which is defined as follows:

[Flags]
Enum Mouseeventflag: Uint
{
Move =   Zero X 0001 ,
Leftdown =   Zero X 0002 ,
Leftup =   Zero X 0004 ,
Rightdown =   Zero X 0008 ,
Rightup =   Zero X 0010 ,
Middledown =   Zero X 0020 ,
Middleup =   Zero X 0040 ,
Xdown =   Zero X 0080 ,
Xup =   Zero X 0100 ,
Wheel =   Zero X 0800 ,
Virtualdesk =   Zero X 4000 ,
Absolute =   Zero x 8000
}

For more information about the two functions, see the msdn library or Windows Platform SDK documentation.
The following demo program (full versionSource code, Vs. NET 2005/C #) demonstrate how to use the above function to control the mouse to move to the taskbar and click the "Start" button.
(This program uses findjavaswex and other API functions 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.