最近的项目需要用到鼠标的模拟事件做自动化处理,于是写了这个模拟鼠标操作的类,可直接使用,代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace VideoGengXia.Untils
{
public class UmouseUntils
{
[DllImport("User32.dll")]
public static extern void keybd_event(byte bVk, byte Scan, int dwFlags, int dwExtraInfo);
[DllImport("user32.dll")]
private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
[DllImport("User32")]
static extern bool SetCursorPos(int x, int y);
const int MOUSEEVENTF_MOVE = 0x0001;
const int MOUSEEVENTF_LEFTDOWN = 0x0002;
const int MOUSEEVENTF_LEFTUP = 0x0004;
const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
const int MOUSEEVENTF_RIGHTUP = 0x0010;
const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
const int MOUSEEVENTF_MIDDLEUP = 0x0040;
const int MOUSEEVENTF_ABSOLUTE = 0x8000;
const int MOUSEEVENTF_WHEEL = 0x800;
public static void Simuate_Mouse_GunDong(int dx)
{
try
{
mouse_event(MOUSEEVENTF_WHEEL, 0, 0, dx, 0);
}
catch (Exception ex)
{
Console.WriteLine("模拟鼠标混动异常:" + ex.Message.ToString());
}
}
public static void Simuate_Mouse_Center_Up(int dx, int dy)
{
try
{
mouse_event(MOUSEEVENTF_MIDDLEUP, dx, dy, 0, 0);
}
catch (Exception ex)
{
Console.WriteLine("模拟鼠标中建抬起异常:" + ex.Message.ToString());
}
}
public static void Simuate_Mouse_Center_Down(int dx, int dy)
{
try
{
mouse_event(MOUSEEVENTF_MIDDLEDOWN, dx, dy, 0, 0);
}
catch (Exception ex)
{
Console.WriteLine("模拟鼠标中键按下异常:" + ex.Message.ToString());
}
}
public static void Simulate_Mouse_Right_Up(int dx, int dy)
{
try
{
mouse_event(MOUSEEVENTF_RIGHTUP, dx, dy, 0, 0);
}
catch (Exception ex)
{
Console.WriteLine("模拟鼠标右键抬起异常:" + ex.Message.ToString());
}
}
public static void Simulate_Mouse_Right_Down(int dx, int dy)
{
try
{
mouse_event(MOUSEEVENTF_RIGHTDOWN, dx, dy, 0, 0);
}
catch (Exception ex)
{
Console.WriteLine("模拟鼠标右键按下异常:" + ex.Message.ToString());
}
}
public static void Simulate_Mouse_Left_Down(int dx, int dy)
{
try
{
mouse_event(MOUSEEVENTF_LEFTDOWN, dx, dy, 0, 0);
}
catch (Exception ex)
{
Console.WriteLine("模拟鼠标左键按下异常:" + ex.Message.ToString() + ex.StackTrace.ToString());
}
}
public static void Simulate_Mouse_Left_Up(int dx, int dy)
{
try
{
mouse_event(MOUSEEVENTF_LEFTUP, dx, dy, 0, 0);
}
catch (Exception ex)
{
Console.WriteLine("模拟鼠标左键抬起异常:" + ex.Message.ToString() + ex.StackTrace.ToString());
}
}
public static void SetMouseLocation(int x, int y)
{
try
{
SetCursorPos(x, y);
}
catch (Exception ex)
{
Console.WriteLine("设置鼠标位置异常:" + ex.Message.ToString() + ex.StackTrace.ToString());
}
}
public static void SetClipborad()
{
string hotkey_value = "V";
Keys getType = (Keys)Enum.Parse(typeof(Keys), hotkey_value);
keybd_event(17, 0, 0, 0);
keybd_event((byte)getType, 0, 0, 0);
keybd_event((byte)getType, 0, 2, 0);
keybd_event(17, 0, 2, 0);
}
public static void SendHotKey(string value)
{
Keys getType = (Keys)Enum.Parse(typeof(Keys), value);
keybd_event((byte)getType, 0, 0, 0);
keybd_event((byte)getType, 0, 2, 0);
}
}
}
整个类的封装如上图所示,看不懂的可以私信我!