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