Using Visual C # programming analog mouse operation

xiaoxiao2021-04-07  309

Sometimes we need to simulate the movement, click, etc. of the mouse in our program. - For example, a macro that reproduces the user's operation, or a Demo program that demonstrates the operation method. So, we are

How to achieve in .NET?

.NET does not provide a function of changing the mouse pointer position and simulating click operation; but

The Windows API is available. 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 an 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 also set the absolute position of the mouse pointer but also to be set in relative coordinates. In addition, the function can also simulate the mouse button click, mouse wheel operation, etc. The mouseeventflag is a UINT-based enumeration, 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 details on these two functions, you can view the MSDN Library or Windows Platform SDK documentation.

转载请注明原文地址:https://www.9cbs.com/read-132392.html

New Post(0)