c#如何模拟鼠标事件,完整的封装类

最近的项目需要用到鼠标的模拟事件做自动化处理,于是写了这个模拟鼠标操作的类,可直接使用,代码如下

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
{
    /*
     * 日期:2024-08-19
     * 功能:鼠标及其设置鼠标位置事件类
     * 作者:大师兄
     * 
     */
    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());
            }
        }
        /// <summary>
        /// 模拟鼠标中键抬起事件
        /// </summary>
        /// <param name="dx">移动的x坐标位置</param>
        /// <param name="dy">移动的y坐标位置</param>
        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());
            }
        }

        /// <summary>
        /// 模拟鼠标中键按下事件
        /// </summary>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        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());
            }
        }

        /// <summary>
        /// 模拟鼠标右键抬起
        /// </summary>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        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());
            }
        }
        /// <summary>
        ///鼠标右键按下的时候
        /// </summary>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        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());
            }
        }

        /// <summary>
        /// 模拟鼠标左键按下
        /// </summary>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        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());
            }
        }
        /// <summary>
        /// 模拟鼠标左键抬起
        /// </summary>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        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());
            }
        }
        /// <summary>
        /// 设置鼠标的位置
        /// </summary>
        /// <param name="x">x坐标值</param>
        /// <param name="y">y坐标值</param>
        public static void SetMouseLocation(int x, int y)
        {
            try
            {
                SetCursorPos(x, y);
            }
            catch (Exception ex)
            {
                Console.WriteLine("设置鼠标位置异常:" + ex.Message.ToString() + ex.StackTrace.ToString());
            }

        }

        /// <summary>
        /// 快捷键 Ctrl+v
        /// </summary>
        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);
        }
    }
}

整个类的封装如上图所示,看不懂的可以私信我!
WinAPI-Wrapper 模拟鼠标点击 用于模拟鼠标移动、点击、窗口操作等的Windows API包装器类。 API 下面是一些可用的方法的总结。有更多的方法和类,比下面列出的要多,但目的是要大致了解包装器能做什么。要查看关于特定方法的详细信息和参数的详细信息,请查看代码本身,因为它的注释很好。 Mouse.cs public static void LeftClick(); public static void RightClick(); public static void MiddleClick(); public static void LeftDown(); public static void LeftUp(); public static void RightDown(); public static void RightUp(); public static void MiddleDown(); public static void MiddleUp(); public static void Move(int x, int y); public static void LeftDrag(Point point1, Point point2, int interval, int lag); Window.cs public static bool DoesExist(string windowTitle); public static IntPtr Get(string windowTitle); public static IntPtr GetFocused(); public static void SetFocused(IntPtr hWnd); public static bool IsFocused(IntPtr hWnd); public static void Move(IntPtr hWnd, int x, int y); public static void Resize(IntPtr hWnd, int width, int height); public static void Hide(IntPtr hWnd); public static void Show(IntPtr hWnd); public static Rectangle GetDimensions(IntPtr hWnd); public static Size GetSize(IntPtr hWnd); public static Point GetLocation(IntPtr hWnd); public static string GetTitle(IntPtr hWnd); public static void SetTitle(IntPtr hWnd, string title); public static void Maximize(IntPtr hWnd); public static void Minimize(IntPtr hWnd); public static void Normalize(IntPtr hWnd); public static Bitmap Screenshot(IntPtr hWnd); public static void RemoveMenu(IntPtr hWnd); public static void Close(IntPtr hWnd); public static void DisableCloseButton(IntPtr hWnd); public static void DisableMaximizeButton(IntPtr hWnd); public static void DisableMinimizeButton(IntPtr hWnd); public static void EnableMouseTransparency(IntPtr hWnd); public static Point ConvertToWindowCoordinates(IntPtr hWnd, int x, int y); public static Point GetCoordinateRelativeToWindow(IntPtr hWnd); Desktop.cs public static Bitmap Screenshot(); public static void HideTaskBar(); public static void ShowTaskBar(); public static int GetWidth(); public static int GetHeight(); 使用 在windows api文件夹中编译代码会产生一个.dll文件。任何引用这个.dll的ccode都可以使用包装器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

搬砖大师兄.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值