Use mouse_event () to control mouse operations
In the development of automated testing, it is difficult to find the IDs of some controls. In some cases, we directly set the mouse position and then use the click event, will receive good results. In Windows API, there is a mouse_event function ready for us.
This function is in the library file user32.dll. We can find this file in the directory c:/Windows/system32 (XP System), which comes with the system. We use C # To directly call the API in this file as an example to illustrate how to perform mouse operations. First, we declare the reference in C #. If it is a from-based program, this declaration can be written in your from class.
[System. runtime. interopservices. dllimport ("USER32")]
Private Static extern int mouse_event (INT dwflags, int dx, int dy, int cbuttons, int dwextrainfo );
Parameter meaning
Dwflags long, one of the flags in the following table or their combination
DX, Dy long, specifies the absolute or relative position of the X and Y directions based on the mouseeventf_absolute mark.
Cbuttons long, not used
Dwextrainfo long, not used
Meaning of the dwflags constant
Const int mouseeventf_move = 0x0001; move the mouse
Const int mouseeventf_leftdown = 0x0002; simulate left mouse button Press
Const int mouseeventf_leftup = 0x0004; simulate left mouse button lifting
Const int mouseeventf_rightdown = 0x0008; simulate right-clicking
Const int mouseeventf_rightup = 0x0010; simulate right-clicking and raising
Const int mouseeventf_middledown = 0x0020; simulate the mouse key and press
Const int mouseeventf_middleup = 0x0040; simulate middle mouse button lifting
Const int mouseeventf_absolute = 0x8000; indicates whether absolute coordinates are used
In the program, we can directly call the mouse_event function.
Mouse_event (INT) (mouseeventf_absolute | mouseeventf_move), 500,500, 0/* useless parameter */, 0 );
1. Click the combination of the two events by clicking and releasing the left mouse button:
Mouse_event (mouseeventf_leftdown | mouseeventf_leftup, 0, 0, 0)
2. Simulate the right-click event:
Mouse_event (mouseeventf_rightdown | mouseeventf_rightup, 0, 0, 0)
3. Two consecutive left-click events constitute a double-click event:
Mouse_event (mouseeventf_leftdown | mouseeventf_leftup, 0, 0, 0)
Mouse_event (mouseeventf_leftdown | mouseeventf_leftup, 0, 0, 0)
4. Use absolute coordinates
Mouseeventf_absolute | mouseeventf_move, 500,500, 0, 0
It should be noted that if mouseeventf_absolute is not used, the default point of the function is relative to the current position of the mouse. If dx, and Dy are expressed as 0, this function is considered to be the point where the current mouse is located. 5. directly set the absolute coordinates and click
Mouse_event (mouseeventf_leftdown, x * 65536/1024, y * 65536/768, 0, 0 );
Mouse_event (mouseeventf_leftup, x 65536/1024, y * 65536/768, 0, 0 );
X and Y are the abscissa and ordinate of the point you want to click.
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ///////////////////////////
Demo:
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. runtime. interopservices;
Namespace click
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}
[Dllimport ("USER32")]
Public extern static void mouse_event (INT dwflags, int dx, int dy, int dwdata, intptr dwextrainfo );
[Dllimport ("USER32")]
Public extern static void setcursorpos (int x, int y );
[Dllimport ("USER32")]
Public extern static bool getcursorpos (out point P );
[Structlayout (layoutkind. Sequential)]
Public struct point
{
Public int X;
Public int y;
}
Public Enum mouseeventflags
{
Move = 0x0001,
Leftdown = 0x0002,
Leftup-to-date = 0x0004,
Rightdown = 0x0008,
Rightup = 0x0010,
Middledown = 0x0020,
Middleup = 0x0040,
Wheel = 0x0800,
Absolute = 0x8000
}
Private void autoclick (int x, int y)
{
Point P = new point ();
Getcursorpos (Out P );
Try
{
Setcursorpos (x, y );
Mouse_event (INT) (mouseeventflags. leftdown | mouseeventflags. Absolute), 0, 0, 0, intptr. Zero );
Mouse_event (INT) (mouseeventflags. leftup | mouseeventflags. Absolute), 0, 0, 0, intptr. Zero );
}
Finally
{
Setcursorpos (P. X, p. y );
}
}
Private void button#click (Object sender, eventargs E)
{
Autoclick (20, 40 );
}
}
}