I just started to learn C # And wanted to create a web game myself. There is money collection in the game. The point of a building and a building is very tired. So I want to use C # to simulate the mouse operation to collect money for me, and I want to learn this will be helpful for future testing, so I have the motivation. The learning process is also twists and turns, because there is very little information on the Internet for half a day. The game is flash, but sendmessage is not used because the objects in the game cannot be obtained. I found some articles about mouse_event, but it is outdated on msdn. The sendinput function is found.
First, the first level is that you do not know how to call the window API in C. Fortunately, I found an article on the Internet and helped me get in. Thank you. If you do not know, visit http://www.linuxdiyf.com/1/article/2006/0702/article_796.html. Haowen.
To put it bluntly, go to the Code:
First, let's take a look at the sendinput function description in the win api:
Uint winapi sendinput (_ in uint ninputs, _ in lpinput pinputs, _ in int cbsize );
Corresponding C # code:
[DllImport("user32.dll")]
public static extern UInt32 SendInput(UInt32 nInputs,Input[] pInputs, int cbSize);
The pinputs parameter is an array type and the input structure of the array element. Therefore, we need to define the corresponding input structure or object in C. The input structure mainly defines the required mouse or keyboard operations. Ninputs indicates the length of the pinputs array. Cbsize indicates the size of the input structure.
Define the input structure. The following describes the Input Structure in win api:
Typedef struct taginput {DWORD type; Union {mouseinput Mi; keybdinput Ki; hardwareinput Hi ;};} input, * pinput;
Corresponding C # code
[StructLayout(LayoutKind.Explicit)]
public struct Input
{
[FieldOffset(0)]public Int32 type;
[FieldOffset(4)]public MouseInput mi;
[FieldOffset(4)]public tagKEYBDINPUT ki;
[FieldOffset(4)]public tagHARDWAREINPUT hi;
}
As shown above, the mouseinput, keybdinput, and hardwareinput structures need to be defined. The code is directly pasted below.
Description in win api:
typedef struct tagMOUSEINPUT { LONG dx; LONG dy; DWORD mouseData; DWORD dwFlags; DWORD time; ULONG_PTR dwExtraInfo;} MOUSEINPUT, *PMOUSEINPUT;
typedef struct tagKEYBDINPUT { WORD wVk; WORD wScan; DWORD dwFlags; DWORD time; ULONG_PTR dwExtraInfo;} KEYBDINPUT, *PKEYBDINPUT;
typedef struct tagHARDWAREINPUT { DWORD uMsg; WORD wParamL; WORD wParamH;} HARDWAREINPUT, *PHARDWAREINPUT;
Corresponding code in C:
[StructLayout(LayoutKind.Sequential)]
public struct MouseInput
{
public Int32 dx;
public Int32 dy;
public Int32 Mousedata;
public Int32 dwFlag;
public Int32 time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
public struct tagKEYBDINPUT
{
Int16 wVk;
Int16 wScan;
Int32 dwFlags;
Int32 time;
IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
public struct tagHARDWAREINPUT
{
Int32 uMsg;
Int16 wParamL;
Int16 wParamH;
}
I mainly simulate the mouse, so I only need to define the flag value of the mouse:
Const int mouseevent_absolute = 0x8000; const int mouserevent_hwheel = 0x01000; const int mouseevent_move = 0x0001; const int rows = 0x2000; const int mouseevent_leftdown = 0x0002; const int mouseevent_leftup = 0x0004; const int mouseevent_middledown = 0x0020; const int down = 0x0040; const int mouseevent_rightdown = 0x0008; const int mouseevent_rightup = 0x0010; const int mouseevent_wheel = 0x0800; const int mousseevent_xup = 0x0100; const int mousseevent_xdown = 0x0080;
Code for simulating mouse operations in C:
For (I = x; I <= x + width; I ++ = 450)
// X is the absolute coordinate value of the X axis in the upper left corner of the flash window. The coordinates in the upper left corner of the screen are (0, 0 ). Width indicates the flash window width.
{
For (j = y; j <= Y + height; j + = 150) // y indicates the absolute coordinate value of the Y axis in the upper left corner of the flash window. Height indicates the flash window height.
{
Mouseinput myminput = new mouseinput ();
Myminput. dx = I;
Myminput. DY = J;
Myminput. mousedata = 0;
Myminput. dwflag = mouseevent_absolute | mouseevent_move | mouseevent_leftdown | mouseevent_leftup;
Myminput. Time = 0;
Input [] myinput = new input [1];
Myinput [0] = new input ();
Myinput [0]. type = 0;
Myinput [0]. MI = myminput;
Uint32 result = sendinput (uint) myinput. length, myinput, Marshal. sizeof (myinput [0]. GetType ()));
If (result = 0)
{
MessageBox. Show ("fail ");
}
}
}
Knowledge Point: Convert pixel coordinates to absolute coordinates:
Dx in the mouseinput structure in the API. Dy indicates absolute coordinates, which are relative to the screen. The coordinates in the upper left corner of the screen are (), and those in the lower right corner are ). The coordinates of the objects (frame, button, Flash, etc.) obtained in C # Are pixel coordinates related to the resolution of the current screen. If your display resolution is 1024*768, the pixel coordinates in the upper-left corner of the screen are (1024,768), and the lower-right coordinates are ). The conversion function is as follows:
DX = x * (65335/screenwidth) // X, Y is the pixel coordinate.
DY = y * (65335/screenheight) // screenwidth and screenheight are the resolutions of the current monitor. The obtained method is screenwidth = screen. primaryscreen. workingarea. width;
Screenheight = screen. primaryscreen. workingarea. height;
OK. This is what we have learned today. Through self-learning, we can simulate the mouse action. Unfortunately, clicking the mouse will not only collect the money, but a small window will pop up when the point is to the building. If you want to upgrade the system, you will be given an upgrade, upgrade takes a long time. I don't want this. I have no idea today. I will continue tomorrow.