C#鼠标自动点击

C#其他操作 专栏收录该内容
11 篇文章 0 订阅

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

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, "开始");
        }
    }
}

 

展开阅读全文
  • 1
    点赞
  • 0
    评论
  • 10
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值