自动点击鼠标

C#+Win32 API控制鼠标的事件:左击,右击,双击,移动,滚动,等等.(部分参考)主要以线程控制

C#如何实现鼠标左键自动点击并自己设定点击间隔

按一件键(例如home,再按一次停止;)使鼠标在设定的时间间隔开始左键单击(例如一秒点一次那种的,时间单位最好是毫秒)并可以移动到窗口以外点击桌面或者其他东西。

 

图:

 

 

源码:

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

namespace test.AutoClickMouseLeftButton
{
    public partial class AutoClickMouseLeftButton : Form
    {
        [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
        {
            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);
            }
        }

        bool isBoot = false;

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            const int WM_SYSTEMDOWN = 0x104;//系统功能按键
            const int WM_KEYDOWN = 0x100;//普通按键
            if ((msg.Msg == WM_SYSTEMDOWN)||(msg.Msg==WM_KEYDOWN))
            {
                switch (keyData)
                {
                    //因为我的笔记本的Home与方向键集成一起块.
                    //不知道怎么按出HOME键了,所以用普按键:方向键上:UP来代替了.
                    //你运行的时候你可以自己修改.
                    //把case Keys.Up隐掉,把case Keys.Home还原就是了。

                    //case Keys.Home:
                    case Keys.Up:
                        {
                            //
                            if (isBoot)
                            {
                                isBoot = false;
                                this.LB_RootState.Text = "未启动";
                            }
                            else
                            {
                                isBoot = true;
                                this.LB_RootState.Text = "已启动";
                            }
                            if (isBoot)
                                this.BT_Start_Click(null, null);
                            else
                                this.BT_Stop_Click(null, null);
                            return true;
                        }
                }
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }


        //构造函数
        public AutoClickMouseLeftButton()
        {
            InitializeComponent();
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            LB_Numbers.Text = ClickCount.ToString();
            LB_RootState.Text = "未启动";
        }
        //存放着当前鼠标的位置坐标
        Point CursorPosition = new Point(0, 0);

        //存放着鼠标左键点击次数
        int ClickCount = 0;
        //主线控制程对象
        Thread controlThread;
        //线程主要处理的函数
        private void ThreadRunMethod()
        {
            while (true)
            {
                //this.BT_Start.PerformClick();
                CursorPosition = Cursor.Position;
                AutoClick(CursorPosition.X, CursorPosition.Y);
                Thread.Sleep(1000);
            }
        }

        private void BT_TestClick_Click(object sender, EventArgs e)
        {
            ClickCount++;
            LB_Numbers.Text = ClickCount.ToString();
        }

        private void BT_Start_Click(object sender, EventArgs e)
        {
            try
            {
                controlThread = new Thread(new ThreadStart(ThreadRunMethod));
                controlThread.Start();
            }
            catch (Exception)
            {
                Application.DoEvents();
            }
            BT_Stop.Enabled = true;
            BT_Start.Enabled = false;
        }

        private void BT_Stop_Click(object sender, EventArgs e)
        {
            try
            {
                if (controlThread != null)
                    controlThread.Abort();
            }
            catch (Exception)
            {
                Application.DoEvents();
            }
            BT_Start.Enabled = true;
            BT_Stop.Enabled = false;
        }
    }
}

完整初始化代码:

namespace test.AutoClickMouseLeftButton
{
    partial class AutoClickMouseLeftButton
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.BT_TestClick = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.LB_Numbers = new System.Windows.Forms.Label();
            this.BT_Start = new System.Windows.Forms.Button();
            this.BT_Stop = new System.Windows.Forms.Button();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.LB_RootState = new System.Windows.Forms.Label();
            this.SuspendLayout();
            //
            // BT_TestClick
            //
            this.BT_TestClick.Location = new System.Drawing.Point(14, 51);
            this.BT_TestClick.Name = "BT_TestClick";
            this.BT_TestClick.Size = new System.Drawing.Size(115, 23);
            this.BT_TestClick.TabIndex = 0;
            this.BT_TestClick.Text = "TestClickHere";
            this.BT_TestClick.UseVisualStyleBackColor = true;
            this.BT_TestClick.Click += new System.EventHandler(this.BT_TestClick_Click);
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(12, 9);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(137, 12);
            this.label1.TabIndex = 1;
            this.label1.Text = "当前鼠标左键点击次数:";
            //
            // LB_Numbers
            //
            this.LB_Numbers.AutoSize = true;
            this.LB_Numbers.Location = new System.Drawing.Point(156, 8);
            this.LB_Numbers.Name = "LB_Numbers";
            this.LB_Numbers.Size = new System.Drawing.Size(47, 12);
            this.LB_Numbers.TabIndex = 2;
            this.LB_Numbers.Text = "Numbers";
            //
            // BT_Start
            //
            this.BT_Start.Location = new System.Drawing.Point(14, 189);
            this.BT_Start.Name = "BT_Start";
            this.BT_Start.Size = new System.Drawing.Size(200, 23);
            this.BT_Start.TabIndex = 3;
            this.BT_Start.Text = "StartAutoClickMouseLeftButton";
            this.BT_Start.UseVisualStyleBackColor = true;
            this.BT_Start.Click += new System.EventHandler(this.BT_Start_Click);
            //
            // BT_Stop
            //
            this.BT_Stop.Location = new System.Drawing.Point(14, 218);
            this.BT_Stop.Name = "BT_Stop";
            this.BT_Stop.Size = new System.Drawing.Size(200, 23);
            this.BT_Stop.TabIndex = 4;
            this.BT_Stop.Text = "StopAutoClickMouseLeftButton";
            this.BT_Stop.UseVisualStyleBackColor = true;
            this.BT_Stop.Click += new System.EventHandler(this.BT_Stop_Click);
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label2.Location = new System.Drawing.Point(155, 76);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(170, 16);
            this.label2.TabIndex = 5;
            this.label2.Text = "JaveLin BaiDu Blog";
            //
            // label3
            //
            this.label3.AutoSize = true;
            this.label3.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label3.Location = new System.Drawing.Point(94, 105);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(317, 16);
            this.label3.TabIndex = 5;
            this.label3.Text = "部分参考:CSDN技术论坛,源至本人发的贴";
            //
            // label4
            //
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(188, 242);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(137, 12);
            this.label4.TabIndex = 6;
            this.label4.Text = "按HOME键自动点击状态:";
            //
            // LB_RootState
            //
            this.LB_RootState.AutoSize = true;
            this.LB_RootState.Location = new System.Drawing.Point(331, 242);
            this.LB_RootState.Name = "LB_RootState";
            this.LB_RootState.Size = new System.Drawing.Size(59, 12);
            this.LB_RootState.TabIndex = 6;
            this.LB_RootState.Text = "RootState";
            //
            // AutoClickMouseLeftButton
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(423, 266);
            this.Controls.Add(this.LB_RootState);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.BT_Stop);
            this.Controls.Add(this.BT_Start);
            this.Controls.Add(this.LB_Numbers);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.BT_TestClick);
            this.Name = "AutoClickMouseLeftButton";
            this.Text = "AutoClickMouseLeftButton";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button BT_TestClick;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label LB_Numbers;
        private System.Windows.Forms.Button BT_Start;
        private System.Windows.Forms.Button BT_Stop;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label LB_RootState;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值