首页 > 编程语言 >C# 模拟鼠标操作类

C# 模拟鼠标操作类

时间:2023-01-25 20:22:17浏览次数:36  
标签:鼠标 point C# System static location using Mouse 模拟

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Windows.Forms;

namespace Vicky.Helpers
{
    /// <summary>
    /// 模拟鼠标点击
    /// </summary>
    [HostProtection(SecurityAction.LinkDemand, Resources = HostProtectionResource.ExternalProcessMgmt)]
    public static class Mouse
    {
        #region 鼠标操作函数

        [DllImport("user32.dll")]
        private static extern void mouse_event(MouseEvent flags, int dx, int dy, uint data, UIntPtr extraInfo);

        /// <summary>
        /// 连续两次鼠标单击之间会被处理成双击事件的间隔时间。
        /// </summary>
        /// <returns>以毫秒表示的双击时间</returns>
        [DllImport("user32.dll", EntryPoint = "GetDoubleClickTime")]
        public static extern int GetDoubleClickTime();/// <summary>
        /// 把光标移到屏幕的指定位置。如果新位置不在由 ClipCursor函数设置的屏幕矩形区域之内,则系统自动调整坐标,使得光标在矩形之内。
        /// </summary>
        /// <param name="x">指定光标的新的X坐标,以屏幕坐标表示。</param>
        /// <param name="y">指定光标的新的Y坐标,以屏幕坐标表示。</param>
        /// <returns>如果成功,返回非零值;如果失败,返回值是零</returns>
        [DllImport("user32.dll")]
        public static extern int SetCursorPos(int x, int y);

        [Flags]
        private enum MouseEvent : 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
        }
        #endregion

        #region 鼠标相关属性

        /// <summary>
        /// 检查鼠标是否已经安装.
        /// </summary>
        public static bool Present
        {
            get 
            {
                return SystemInformation.MousePresent;
            }
        }

        /// <summary>
        /// 检查鼠标是否存在滚轮
        /// </summary>
        public static bool WheelPresent
        {
            get
            {
                if (!Present)
                {
                    throw new InvalidOperationException("没有找到鼠标.");
                }

                return SystemInformation.MouseWheelPresent;
            }
        }

        /// <summary>
        /// 获取鼠标滚轮每次滚动的行数
        /// </summary>
        public static int WheelScrollLines
        {
            get
            {
                if (!WheelPresent)
                {
                    throw new InvalidOperationException("没有找到鼠标滑轮.");
                }

                return SystemInformation.MouseWheelScrollLines;
            }
        }

        #endregion

        #region 封装函数

        /// <summary>
        /// 在当前鼠标的位置左键点击一下
        /// </summary>
        public static void MouseClick()
        {
            mouse_event(MouseEvent.LeftDown, 0, 0, 0, UIntPtr.Zero);
            mouse_event(MouseEvent.LeftUp, 0, 0, 0, UIntPtr.Zero);
        }

        /// <summary>
        /// 移动到坐标位置点击
        /// </summary>
        /// <param name="location">要点击的坐标位置,屏幕绝对值</param>
        public static void MouseClick(Point location)
        {
            MouseMove(location);
            mouse_event(MouseEvent.LeftDown, 0, 0, 0, UIntPtr.Zero);
            mouse_event(MouseEvent.LeftUp, 0, 0, 0, UIntPtr.Zero);
        }

        /// <summary>
        /// 移动到坐标位置点击
        /// </summary>
        /// <param name="location">要点击的坐标位置,屏幕绝对值</param>
        public static void MouseRightClick(Point location)
        {
            MouseMove(location);
            mouse_event(MouseEvent.RightDown, 0, 0, 0, UIntPtr.Zero);
            mouse_event(MouseEvent.RightUp, 0, 0, 0, UIntPtr.Zero);
        }

        /// <summary>
        /// 移动到坐标位置
        /// </summary>
        /// <param name="location">要移动到的坐标位置,屏幕绝对值</param>
        public static void MouseMove(Point location)
        {
            SetCursorPos(location.X, location.Y);
        }

        #endregion
    }
}

测试类

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Vicky.Helpers.TestBenches
{
    internal class MouseTestBench : TestBench
    {
        Point point = new Point(0, 0);

        internal override void Test()
        {
            Console.WriteLine(Mouse.Present ? "Mouse presents" : "Mouse does not present.");
            Console.WriteLine(Mouse.WheelPresent ? "Mouse wheel presents" : "Mouse wheel does not present.");
            Console.WriteLine($"Mouse wheel scrollLines: {Mouse.WheelScrollLines}.");
            Console.WriteLine($"Mouse double-click time: {Mouse.GetDoubleClickTime()}.");

            Mouse.MouseClick();
            Console.WriteLine($"Mouse left-clicked at location: ({point.X}, {point.Y})");

            point = new Point(10, 10);
            Mouse.MouseClick(point);
            Console.WriteLine($"Mouse left-clicked at location: ({point.X}, {point.Y})");

            point = new Point(400, 300);
            Mouse.MouseRightClick(point);
            Console.WriteLine($"Mouse right-clicked at the location: ({point.X}, {point.Y})");

            point = new Point(200, 200);
            Mouse.MouseMove(point);
            Console.WriteLine($"Mouse moved to the location: ({point.X}, {point.Y})");
        }
    }
}

 

标签:鼠标,point,C#,System,static,location,using,Mouse,模拟
From: https://www.cnblogs.com/vicky2021/p/17067237.html

相关文章

  • JavaScript学习笔记—对象的序列化
    JS中的对象使用时都是存在于计算机的内存中序列化指将对象转换为一个可以存储的格式,在JS中对象的序列化通常是将一个对象转换为字符串(JSON字符串)序列化的用途,对象转换为......
  • WMI and ACPI 问题
    微软的资料如下:​​http://msdn.microsoft.com/en-us/library/windows/hardware/Dn614028(v=vs.85).aspx​​Code:​​http://code.msdn.microsoft.com/windowshardware/WMI-......
  • 说一说JavaScript有几种方法判断变量的类型?
    typeof、instanceof、Object.prototype.toString.call()(对象原型链判断方法)、constructor(用于引用数据类型)标准回答JavaScript有4种方法判断变量的类型......
  • 题解 CF1773H【Hot and Cold】
    赛时跟队友一起摆烂了,于是没做出来,回来补题。如果询问到了答案,可以直接判断并退出,因此下文的询问过程并没有考虑这一点。显然“\((1,1)\)比\((0,0)\)离所求位置更近”......
  • JavaScript学习笔记—对象的解构
    constobj={name:"孙悟空",age:18,gender:"男"};let{name,age,gender}=obj;//声明变量同时解构变量console.log(name,age,gender);//孙悟空18男let......
  • call apply bind的作用和区别?
    bind改变this指向不直接调用、call和apply改变this指向直接调用、apply接收第二个参数为数组、call用于对象的继承、伪数组转换成真数组、apply用于找出数组中的最大值和......
  • CSS尺寸设置的单位
    解题思路得分点px、rem、em、vw、vh标准回答px:pixel像素的缩写,绝对长度单位,它的大小取决于屏幕的分辨率,是开发网页中常常使用的单位。em:相对长度单位,在......
  • CSRF攻击是什么
    CSRF攻击的过程原理是:-用户打开浏览器,访问目标网站A,输入用户名和密码请求登录-用户信息在通过认证后,网站A产生一个cookie信息返回给浏览器,这个时候用户以可正常发送请......
  • Xbox One 手柄在 Steam 上截图 - Xbox One Controller Gamepad Screenshot on Steam
    Steam手柄如何截图/XboxOne手柄怎么截图/Xbox旧手柄怎么截图?答案如下:via:Howtotakescreenshotsusingxboxonecontroller?::HelpandTipsGuide+R......
  • 用python求三个数中最大的奇数,主要用branching语法
    #Findthelargestoddnumberamong3numbersnum1=int(input("请输入第一个数:"))num2=int(input("请输入第二个数:"))num3=int(input("请输入第三个数:"))ifnum1%2==0......