net framework如何模擬鼠標(biāo)?net framework通過(guò)下載6.0版本以及.NET Core 3.1進(jìn)行文檔的代碼修改來(lái)模擬鼠標(biāo),下面一起看看net framework模擬鼠標(biāo)教程吧!
net framework如何模擬鼠標(biāo):
方法一:
1、通過(guò)use32的SendInput,use32的SendInput模擬鼠標(biāo)。
2、定義代碼:
[DllImport("user32.dll", SetLastError = true)]
internal static extern int SendInput(int nInputs, ref INPUT mi, int cbSize);
其中的INPUT結(jié)構(gòu)表示一個(gè)鍵盤(pán)或鼠標(biāo)操作:
[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模擬鼠標(biāo)需要安裝.NET 6/.NET 5(包括.NET Core 3.1)的桌面指南文檔進(jìn)行改寫(xiě)。
2、以O(shè)n后跟EventName的模擬命名,這種方法只適合自定義控件或窗體內(nèi)。
3、使用方法:
想模擬鼠標(biāo)懸停在ListBox中的某一項(xiàng),OnMouseMove和ListBox不會(huì)以光標(biāo)下的顯示項(xiàng)作為方式反應(yīng)。
PS:受保護(hù)的方法可用于模擬鼠標(biāo)
OnMouseDown
OnMouseEnter
OnMouseHover
OnMouseLeave
OnMouseMove
OnMouseUp
OnMouseWheel
OnMouseClick
OnMouseDoubleClick