zoukankan      html  css  js  c++  java
  • C# 使用Win32API移动光标至指定位置并模拟鼠标点击

    东西不难。

    使用的函数那么几个。

    本例是我删除淘宝购物记录时写的,所以是两个坐标点来回移动并点击鼠标左键。

    using System;
    using System.Runtime.InteropServices;
    using System.Threading;
    
    namespace 鼠标移动且点击
    {
        public enum MouseType
        {
            //移动鼠标 
            MOUSEEVENTF_MOVE = 0x0001,
            //模拟鼠标左键按下 
            MOUSEEVENTF_LEFTDOWN = 0x0002,
            //模拟鼠标左键抬起 
            MOUSEEVENTF_LEFTUP = 0x0004,
            //模拟鼠标右键按下 
            MOUSEEVENTF_RIGHTDOWN = 0x0008,
            //模拟鼠标右键抬起 
            MOUSEEVENTF_RIGHTUP = 0x0010,
            //模拟鼠标中键按下 
            MOUSEEVENTF_MIDDLEDOWN = 0x0020,
            //模拟鼠标中键抬起 
            MOUSEEVENTF_MIDDLEUP = 0x0040,
            //标示是否采用绝对坐标 
            MOUSEEVENTF_ABSOLUTE = 0x8000,
    
        }
        class Program
        {
    
            [System.Runtime.InteropServices.DllImport("user32")]
            public static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
    
            [DllImport("User32.dll")]
            public static extern bool SetCursorPos(int X, int Y);
            static void Main(string[] args)
            {
    
                Init();
                Console.ReadKey();
            }
            static int x1, y1, x2, y2;
            private static void DoClick()
            {
    
                SetCursorPos(x1, y1);
                 mouse_event((int)MouseType.MOUSEEVENTF_LEFTDOWN | (int)MouseType.MOUSEEVENTF_LEFTUP, 0,0, 0, 0);
                //mouse_event((int)MouseType.MOUSEEVENTF_RIGHTDOWN | (int)MouseType.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
    
               
    
                string n = $"x1:{x1},y1:{y1}";
                Console.WriteLine(n);
                Thread.Sleep(300);
            }
    
            private static void DoClickXY2()
            {
                SetCursorPos(x2, y2);
              //  mouse_event((int)MouseType.MOUSEEVENTF_RIGHTDOWN | (int)MouseType.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
                   mouse_event((int)MouseType.MOUSEEVENTF_LEFTDOWN | (int)MouseType.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
    
                string n = $"x2:{x2},y2:{y2}";
                Console.WriteLine(n);
                Thread.Sleep(300);
            }
    
            private static void InputData()
            {
                Console.WriteLine("x1坐标");
                x1 = int.Parse(Console.ReadLine());
                Console.WriteLine("y1坐标");
                y1 = int.Parse(Console.ReadLine());
                Console.WriteLine("x2坐标");
                x2 = int.Parse(Console.ReadLine());
                Console.WriteLine("y2坐标");
                y2 = int.Parse(Console.ReadLine());
                Console.WriteLine();
                string n = $"x1:{x1},y1:{y1},x2:{x2},y2:{y2}";
                Console.WriteLine(n);
            }
    
            static  void Init()
            {
                while (true)
                {
                    pool();
                    Console.WriteLine("是否继续? Y/N");
                    var b = Console.ReadKey().KeyChar;
                    if (b != 'Y' && b != 'y')
                        break;
                }
    
            }
    
            static  void pool()
            {
                Console.WriteLine("执行次数:");
    
                int num = int.Parse(Console.ReadLine());
    
                InputData();
    
                while (num >= 0)
                {
                    DoClick();
                    Thread.Sleep(100);
                    DoClickXY2();
                    num--;
                }
                Console.WriteLine();
            }
        }
    }
  • 相关阅读:
    Python单例模式中的4种方式
    Python list,tuple,dict,set高级变量常用方法
    python如何获取多个excel单元格的值
    两种方法实现python操作日志的封装
    numpy中函数shape的用法
    python中timer定时器常用的两种实现方法
    详解Python中argpasrse模块的基本使用
    在python中列表删除和多重循环退出
    Python的驻留机制(仅对数字,字母,下划线有效)
    python实现tail -f 功能
  • 原文地址:https://www.cnblogs.com/T-ARF/p/12172997.html
Copyright © 2011-2022 走看看