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