Win32API using C # Move the cursor to the specified location and simulating a mouse click

Things difficult.

Function uses so few.

This example was written when I removed Taobao record, so the two coordinate points move around and click the left mouse button.

using System;
using System.Runtime.InteropServices;
using System.Threading;

namespace mouse movements and clicks
{
    public enum MouseType
    {
        // Move the mouse 
        MOUSEEVENTF_MOVE = 0x0001 ,
         // simulate the left mouse button pressed 
        MOUSEEVENTF_LEFTDOWN = 0x0002 ,
         // simulate the left mouse button to lift 
        MOUSEEVENTF_LEFTUP = 0x0004 ,
         // simulate a right mouse down 
        MOUSEEVENTF_RIGHTDOWN = 0x0008 ,
         // simulate the right mouse button lift 
        = MOUSEEVENTF_RIGHTUP 0x0010 ,
         // simulated mouse button pressed 
        MOUSEEVENTF_MIDDLEDOWN = 0x0020 ,
         // simulated mouse button lift 
        MOUSEEVENTF_MIDDLEUP = 0x0040 ,
        // indicate whether the absolute coordinate 
        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 ( " the number of executions: " );

            int num = int.Parse(Console.ReadLine());

            InputData();

            while (num >= 0)
            {
                DoClick();
                Thread.Sleep(100);
                DoClickXY2();
                a - ;
            }
            Console.WriteLine();
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/T-ARF/p/12172997.html