C#鼠标自动点击

实现:鼠标自动点击,设置鼠标点击频率和位置,获取鼠标当前位置的坐标,

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

namespace automatic
{
    public partial class zidong : Form
    {
        public zidong()
        {
            InitializeComponent();
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            LB_Numbers.Text = ClickCount.ToString();
           
        }
        private void btn_exit_Click(object sender, EventArgs e)
        {
            try
            {
                if (controlThread != null)
                    controlThread.Abort();//停止线程
            }
            catch (Exception)
            {
                Application.DoEvents();
            }
            this.Close();
        }
      
        [DllImport("User32")]
        public extern static void mouse_event(int dwFlags, int dx, int dy, int dwData, IntPtr dwExtraInfo);

        [DllImport("User32")]
        public extern static void SetCursorPos(int x, int y);

        [DllImport("User32")]
        public extern static bool GetCursorPos(out POINT p);

        [StructLayout(LayoutKind.Sequential)]
        public struct POINT
        {
            public int X;
            public int Y;
        }
         public enum MouseEventFlags       //鼠标按键的ASCLL码
        {
            Move = 0x0001,
            LeftDown = 0x0002,
            LeftUp = 0x0004,
            RightDown = 0x0008,
            RightUp = 0x0010,
            MiddleDown = 0x0020,
            MiddleUp = 0x0040,
            Wheel = 0x0800,
            Absolute = 0x8000
        }

        private void AutoClick(int x, int y)
        {
            POINT p = new POINT();
            GetCursorPos(out p);
            try
            {
                SetCursorPos(x, y);//设置光标位置               
                mouse_event((int)(MouseEventFlags.LeftDown | MouseEventFlags.Absolute), 0, 0, 0, IntPtr.Zero);//自动按下的按键
                mouse_event((int)(MouseEventFlags.LeftUp | MouseEventFlags.Absolute), 0, 0, 0, IntPtr.Zero);
            }
            finally
            {
                SetCursorPos(p.X, p.Y);
            }
        }       
        Point CursorPosition = new Point(0, 0);//存放着当前鼠标的位置坐标       
        int ClickCount = 0;//存放着鼠标左键点击次数       
        Thread controlThread;//主线控制程对象
        int x=480;
        int y = 483;
        int span = 2000;
        //线程主要处理的函数
        private void ThreadRunMethod()
        {
            while (true)
            {               
                //CursorPosition = Cursor.Position;//自动获取鼠标的位置
                AutoClick(x, y);          //(CursorPosition.X, CursorPosition.Y);设置鼠标点击的位置
                Thread.Sleep(span);
            }
        }
        private void ThreadRunMethod2()
        {
            while (true)
            {
                CursorPosition = Cursor.Position;//自动获取鼠标的位置
                AutoClick(CursorPosition.X, CursorPosition.Y);//设置鼠标点击的位置
                Thread.Sleep(span);
            }
        }
        //点击开始,开始鼠标左键自动点击
        private void BT_Start_Click(object sender, EventArgs e)
        {
            if (txt_span.Text.Length != 0)
                span = Convert.ToInt32(txt_span.Text.Trim());
            try
            {
                if (controlThread == null)
                {
                    controlThread = new Thread(new ThreadStart(ThreadRunMethod2));
                    controlThread.Start();
                }
            }
            catch (Exception)
            {
                Application.DoEvents();
            }
            BT_Stop.Enabled = true;
            BT_Start.Enabled = false;
            button2.Enabled = false;
        }
        private void BT_TestClick_Click(object sender, EventArgs e)
        {
            ClickCount++;
            LB_Numbers.Text = ClickCount.ToString();
        }
        private void BT_Stop_Click(object sender, EventArgs e)
        {
            try
            {
                if (controlThread != null)
                    controlThread.Abort();
                controlThread = null;
            }
            catch (Exception)
            {
                Application.DoEvents();
            }
            BT_Start.Enabled = true;
            button2.Enabled = true;
            BT_Stop.Enabled = false;
        }
        //设置位置
        private void button1_Click(object sender, EventArgs e)
        {
            if (txt_x.Text.Length != 0)
                x = Convert.ToInt32(txt_x.Text.Trim());
            if (txt_y.Text.Length != 0)
                y = Convert.ToInt32(txt_y.Text.Trim());
            if (txt_span.Text.Length != 0)
                span = Convert.ToInt32(txt_span.Text.Trim());
            try
            {
                if (controlThread == null)
                {
                    controlThread = new Thread(new ThreadStart(ThreadRunMethod));
                    controlThread.Start();
                }
            }
            catch (Exception)
            {
                Application.DoEvents();
            }
            BT_Stop.Enabled = true;
            BT_Start.Enabled = false;
        }      
        //开始按钮
        private void button2_Click(object sender, EventArgs e)
        {
            span = 2000;
            try
            {
                if (controlThread == null)
                {
                    controlThread = new Thread(new ThreadStart(ThreadRunMethod));
                    controlThread.Start();
                }
            }
            catch (Exception)
            {
                Application.DoEvents();
            }
            BT_Stop.Enabled = true;
            BT_Start.Enabled = false;
            button1.Enabled = true;
        }
        /// <summary>
        /// 显示当前鼠标位置
        /// </summary>
        private void ShowCursorPosition()
        {
            string str=null ;
            str += "X:" + Cursor.Position.X.ToString() + "  ";
            str += "Y:" + Cursor.Position.Y.ToString() ;
            txt_position.Text = str;          
        }
        // 显示当前鼠标位置的按钮
        private void btn_position_Click(object sender, EventArgs e)
        {
            ShowCursorPosition();
        }
        private void zidong_Load(object sender, EventArgs e)
        {
            ToolTip tooltip = new ToolTip();//鼠标悬浮时显示的提示
            tooltip.ShowAlways = true;
            tooltip.AutoPopDelay = 2000;
            tooltip.InitialDelay = 50;
            tooltip.ReshowDelay = 50;
            tooltip.SetToolTip(this.btn_position, "按Alt+d显示当前鼠标坐标");
            tooltip.SetToolTip(this.button2, "开始");
        }
    }
}

 

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值