111,093
社区成员




class Test:Form
{
public Test()
{
initializeComponent();
button_Stop.Enable=false;
}
Thread controlThread;
//线程处理函数
private void ThreadRunMethod()
{
while(true)
{
//我主要想知道的是:这里写让鼠标自动点击一下左键的处理代码怎么写
//时间间隔1000毫秒
Thread.Sleep(1000);
}
}
private void button_Start_Click(object sender,EventArgs e)
{
//这里写执行让鼠标自动点击左键.点击的时间间隔为1000毫秒
button_Stop.Enable=true;
button_Start.Enable=false;
}
private void button_Stop_Click(object sender,EventArgs e)
{
//这里写让鼠标自动点击左键的线程停止
button_Start.Enable=true;
button_Stop.Enable=false;
}
}
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);
}
}
//构造函数
public AutoClickMouseLeftButton()
{
InitializeComponent();
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
LB_Numbers.Text = ClickCount.ToString();
}
//存放着当前鼠标的位置坐标
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;
}
}
}
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;
namespace test.AutoClickMouseLeftButton
{
public partial class AutoClickMouseLeftButton : Form
{
public AutoClickMouseLeftButton()
{
InitializeComponent();
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
LB_Numbers.Text = ClickCount.ToString();
}
//存放着鼠标左键点击次数
int ClickCount = 0;
Thread controlThread;
private void ThreadRunMethod()
{
while (true)
{
this.BT_Start.PerformClick();
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.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);
//
// 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.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;
}
}
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;
namespace 点击
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[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);
}
}
private void button1_Click(object sender, EventArgs e)
{
AutoClick(20, 40);
}
}
}