首页 > 编程语言 >C# 实现模拟控制鼠标点击事件

C# 实现模拟控制鼠标点击事件

时间:2023-02-22 10:45:13浏览次数:31  
标签:loginy 鼠标 C# void int 点击 static public

        /// <summary>
        /// 引用user32.dll动态链接库(windows api),
        /// 使用库中定义 API:SetCursorPos 
        /// </summary>
        [DllImport("user32.dll")]
        private static extern int SetCursorPos(int x, int y);
        /// <summary>
        /// 移动鼠标到指定的坐标点
        /// </summary>
        public void MoveMouseToPoint(Point p)
        {
            SetCursorPos(p.X, p.Y);
        }
        /// <summary>
        /// 设置鼠标的移动范围
        /// </summary>
        public void SetMouseRectangle(Rectangle rectangle)
        {
            System.Windows.Forms.Cursor.Clip = rectangle;
        }
        /// <summary>
        /// 设置鼠标位于屏幕中心
        /// </summary>
        public void SetMouseAtCenterScreen()
        {
            //当前屏幕的宽高
            int winHeight = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
            int winWidth = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
            //设置鼠标的x,y位置
            loginx = winWidth / 2;
            loginy = winHeight / 2;
            Point centerP = new Point(loginx, loginy);
            //移动鼠标
            MoveMouseToPoint(centerP);
        }
        //点击事件
        [DllImport("User32")]
        //下面这一行对应着下面的点击事件
        //    public extern static void mouse_event(int dwFlags, int dx, int dy, int dwData, IntPtr dwExtraInfo);
        public extern static void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
        public const int MOUSEEVENTF_LEFTDOWN = 0x2;
        public const int MOUSEEVENTF_LEFTUP = 0x4;
        public enum MouseEventFlags
        {
            Move = 0x0001, //移动鼠标
            LeftDown = 0x0002,//模拟鼠标左键按下
            LeftUp = 0x0004,//模拟鼠标左键抬起
            RightDown = 0x0008,//鼠标右键按下
            RightUp = 0x0010,//鼠标右键抬起
            MiddleDown = 0x0020,//鼠标中键按下 
            MiddleUp = 0x0040,//中键抬起
            Wheel = 0x0800,
            Absolute = 0x8000//标示是否采用绝对坐标
        }
        //鼠标将要到的x,y位置
        public static int loginx, loginy;

        private void button1_Click(object sender, EventArgs e)
        {
            SetMouseAtCenterScreen();//模拟鼠标移动到指定坐标
            mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, loginx, loginy, 0, 0);//模拟鼠标点击
        }

 

标签:loginy,鼠标,C#,void,int,点击,static,public
From: https://www.cnblogs.com/blossomwei/p/17143525.html

相关文章

  • const char* 与string
    string是c++标准库里面其中一个,封装了对字符串的操作,实际操作过程我们可以用constchar*给string类初始化a)string转constchar*strings=“abc”;constcha......
  • 时间比较方法DateTime.Compare
    格式:DateTime.Compare(datetime1,datetime2)参数为时间格式,为第一个参数比较第二个参数,返回小于0的值,等于0或大于0的值。stringst1="12:13";stringst2="14:13";DateT......
  • RecyclerView添加分割线
    RecyclerView并没有divider属性,但是我们可以通过RecyclerView的addItemDecoration()来添加分割线,该方法参数为RecyclerView.ItemDecoration。介绍当RecyclerView......
  • Android-ColorMatrixColorFilter设置图片灰色
       我们在编程时有时需要对图片进行处理,比如将图片做成灰色的效果。那么就要用到android为我们提供的颜色矩阵类ColorMatrix。  先介绍一下ColorMatrix这个类,这是一......
  • Linux - scp 使用方法
    scp是securecopy的缩写,scp是linux系统下基于ssh登陆进行安全的远程文件拷贝命令。linux的scp命令可以在linux服务器之间复制文件和目录。命令格式:scp参数原路径......
  • ios - 如何使用 CAShaperLayer 设置圆角半径值?
    https://www.coder.work/article/785950 我用UIBeizerPath画了一条垂直线,我需要这条线的角半径值为5。我试图记忆起 [pathLayersetCornerRadius:5]; 但我没有得......
  • lenova think centre安装 双系统
    配置256的固态盘,1t的机械盘分区为gpt引导应该是uefi(msinfo32->biosM****)计划:windows10必装到256的固态盘只给它200Glinuxmint安装到机械盘各个区中共分......
  • Switch
    Switchpackagecom.andy.base.Andy.operator.structure;publicclassSwitchDemo01{publicstaticvoidmain(String[]args){//case穿透//swit......
  • python+playwright 学习-8.如何在控制台调试定位(Inspect selectors)
    前言在运行selenium脚本的时候,我们通常习惯用sleep去让页面暂停,打开console输入$(selector)去调试定位页面的元素。有时候明明页面能找到元素,代码运行却找不到,很是郁......
  • @Async 结合CompletableFuture使用
    @Async含义:在方法上使用此注解,申明该方法是一个异步任务在类上使用此注解,申明该类中的方法都是异步任务在使用此注解的类对象,必须为spring管理下的bean想要使用此异步......