当前位置:首页 > 文章教程 > 软件教程 > net framework如何模拟鼠标

net framework如何模拟鼠标

2023-10-16 12:34:18 作者:不眠鸟 文章来源:互联网 阅读次数:185

net framework如何模拟鼠标:

方法一

1、通过use32的SendInput,use32的SendInput模拟鼠标。

2、定义代码

[DllImport("user32.dll", SetLastError = true)]

internal static extern int SendInput(int nInputs, ref INPUT mi, int cbSize);

其中的INPUT结构表示一个键盘或鼠标操作:

[StructLayout(LayoutKind.Sequential)]

internal struct INPUT

{

internal int type;

internal INPUTUNION union;

};

[StructLayout(LayoutKind.Explicit)]

internal struct INPUTUNION

{

[FieldOffset(0)]

internal MOUSEINPUT mouseInput;

[FieldOffset(0)]

internal KEYBDINPUT keyboardInput;

};

[StructLayout(LayoutKind.Sequential)]

internal struct MOUSEINPUT

{

internal int dx;

internal int dy;

internal int mouseData;

internal int dwFlags;

internal int time;

internal IntPtr dwExtraInfo;

};

[StructLayout(LayoutKind.Sequential)]

internal struct KEYBDINPUT

{

internal short wVk;

internal short wScan;

internal int dwFlags;

internal int time;

internal IntPtr dwExtraInfo;

};

[Flags]

internal enum SendMouseInputFlags

{

Move = 0x0001,

LeftDown = 0x0002,

LeftUp = 0x0004,

RightDown = 0x0008,

RightUp = 0x0010,

MiddleDown = 0x0020,

MiddleUp = 0x0040,

XDown = 0x0080,

XUp = 0x0100,

Wheel = 0x0800,

Absolute = 0x8000,

};

方法二

1、net framework模拟鼠标需要安装.NET 6/.NET 5(包括.NET Core 3.1)的桌面指南文档进行改写。

2、以On后跟EventName的模拟命名,这种方法只适合自定义控件或窗体内。

3、使用方法

想模拟鼠标悬停在ListBox中的某一项,OnMouseMove和ListBox不会以光标下的显示项作为方式反应。

PS:受保护的方法可用于模拟鼠标

OnMouseDown

OnMouseEnter

OnMouseHover

OnMouseLeave

OnMouseMove

OnMouseUp

OnMouseWheel

OnMouseClick

OnMouseDoubleClick

返回顶部