C # how to simulate mouse operations using SendInput

Source: Internet
Author: User

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.

To put it bluntly, go to the Code:

First, let's take a look at the SendInput function description in the Win API:

Copy codeThe Code is as follows: uint winapi SendInput (
_ In UINT nInputs,
_ In LPINPUT pInputs,
_ In int cbSize
);

Corresponding C # code:Copy codeThe Code is as follows: [DllImport ("user32.dll")]
Public static extern int32 usendinput (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:

Copy codeThe Code is as follows: typedef struct tagINPUT {
DWORD type;
Union {
MOUSEINPUT mi;
KEYBDINPUT ki;
HARDWAREINPUT hi;
};
} INPUT, * PINPUT;

Corresponding C # codeCopy codeThe Code is as follows: [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:

Copy codeThe Code is as follows: 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:Copy codeThe Code is as follows: [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:

Copy codeThe Code is as follows: const int MouseEvent_Absolute = 0x8000;
Const int MouserEvent_Hwheel = 0x01000;
Const int MouseEvent_Move = 0x0001;
Const int MouseEvent_Move_noCoalesce = 0x2000;
Const int MouseEvent_LeftDown = 0x0002;
Const int MouseEvent_LeftUp = 0x0004;
Const int MouseEvent_MiddleDown = 0x0020;
Constint MouseEvent_MiddleUp = 0x0040;
Const int MouseEvent_RightDown = 0x0008;
Constint MouseEvent_RightUp = 0x0010;
Const int MouseEvent_Wheel = 0x0800;
Const int MousseEvent_XUp = 0x0100;
Constint MousseEvent_XDown = 0x0080;

Code for simulating mouse operations in c:

Copy codeThe Code is as follows: 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 = http://www.jb51.net/cx361/archive/2011/12/11/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:

Copy codeThe Code is as follows: dx = x * (65335/ScreenWidth) // x, and 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;

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.