编程语言
首页 > 编程语言> > C# 使用Win32API移动光标至指定位置并模拟鼠标点击

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();
        }
    }
}

 

标签:MOUSEEVENTF,鼠标,C#,int,static,WriteLine,Console,光标,Win32API
来源: https://www.cnblogs.com/T-ARF/p/12172997.html