C# 模拟鼠标单击 键盘按键

鼠标单击案例

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace AutoClick
{
    
    public partial class Form1 : Form
    {
        enum MouseEventFlag : 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
        }

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

        [DllImport("user32.dll")]
        public static extern int SetCursorPos(int x, int y);

        public static void MouseLeftClickEvent(int dx, int dy, uint data)
        {
            SetCursorPos(dx, dy);
            //System.Threading.Thread.Sleep(2 * 1000);
            mouse_event(MouseEventFlag.LeftDown, dx, dy, data, UIntPtr.Zero);
            mouse_event(MouseEventFlag.LeftUp, dx, dy, data, UIntPtr.Zero);
        }
        //public static void MouseLeftClickEvent(uint data)
        //{
        //    SetCursorPos(Convert.ToInt32(Cursor.Position.X), Convert.ToInt32(Cursor.Position.Y));
        //    System.Threading.Thread.Sleep(2 * 1000);
        //    mouse_event(MouseEventFlag.LeftDown, Convert.ToInt32(Cursor.Position.X), Convert.ToInt32(Cursor.Position.Y), data, UIntPtr.Zero);
        //    mouse_event(MouseEventFlag.LeftUp, Convert.ToInt32(Cursor.Position.X), Convert.ToInt32(Cursor.Position.Y), data, UIntPtr.Zero);
        //}

        //public static void MouseLeftClickEvent(int dx, int dy, uint data)
        //{
        //    SetCursorPos(dx, dy);
        //    System.Threading.Thread.Sleep(2 * 1000);
        //    mouse_event(MouseEventFlag.LeftDown, 2905, Convert.ToInt32(Cursor.Position.Y), data, UIntPtr.Zero);
        //    mouse_event(MouseEventFlag.LeftUp, Convert.ToInt32(Cursor.Position.X), Convert.ToInt32(Cursor.Position.Y), data, UIntPtr.Zero);
        //}
        public static void MouseRightClickEvent(int dx, int dy, uint data)
        {
            SetCursorPos(dx, dy);
            System.Threading.Thread.Sleep(2 * 1000);
            mouse_event(MouseEventFlag.RightDown, dx, dy, data, UIntPtr.Zero);
            mouse_event(MouseEventFlag.RightUp, dx, dy, data, UIntPtr.Zero);
        }

        //[System.Runtime.InteropServices.DllImport("user32")]
        //private static extern int mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
        ////移动鼠标 
        //const int MOUSEEVENTF_MOVE = 0x0001;
        ////模拟鼠标左键按下 
        //const int MOUSEEVENTF_LEFTDOWN = 0x0002;
        ////模拟鼠标左键抬起 
        //const int MOUSEEVENTF_LEFTUP = 0x0004;
        ////模拟鼠标右键按下 
        //const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
        ////模拟鼠标右键抬起 
        //const int MOUSEEVENTF_RIGHTUP = 0x0010;
        ////模拟鼠标中键按下 
        //const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
        ////模拟鼠标中键抬起 
        //const int MOUSEEVENTF_MIDDLEUP = 0x0040;
        ////标示是否采用绝对坐标 
        //const int MOUSEEVENTF_ABSOLUTE = 0x8000;
        ////模拟鼠标滚轮滚动操作,必须配合dwData参数
        //const int MOUSEEVENTF_WHEEL = 0x0800;

        public Form1()
        {
            InitializeComponent();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
           pointX.Text = Cursor.Position.X.ToString();
           pointY.Text = Cursor.Position.Y.ToString();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btn_Start_Click(object sender, EventArgs e)
        {
            timer1.Interval = Convert.ToInt32(textBox1.Text.ToString()) * 1000;
            timer1.Enabled = true;
            richTextBox1.AppendText("功能开启" + DateTime.Now.ToString("HH:mm:ss") + System.Environment.NewLine);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Form1.MouseLeftClickEvent(Convert.ToInt32(txtPointX.Text.Trim()), Convert.ToInt32(txtPointY.Text.Trim()), 1);
            //Form1.MouseLeftClickEvent(2905,630,1);
            richTextBox1.AppendText("单击了一次" + DateTime.Now.ToString("HH:mm:ss") + System.Environment.NewLine);
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            richTextBox1.AppendText("功能关闭" + DateTime.Now.ToString("HH:mm:ss") + System.Environment.NewLine);
        }
    }
}

界面图

源码下载:
https://pan.baidu.com/s/19wAZgIzIz6Cz1ybVuKxOrw
提取码:9999

赞(0)
未经允许不得转载:TaKaSa » C# 模拟鼠标单击 键盘按键