今天上12306购票,可被气得不轻,首先登陆人数受到了限制,其次买到票还有个排队等待神马的,先不说里头有没有猫腻,电脑直接操作的,需要等待30分钟以上吗??神马查询语言要运行30分钟才能得到个结果出来?赞同网上的言论:“相比较而言,之前的抢票更能接受一些”。
为了查看剩余多少时间,写了个小小的模拟鼠标点击事件,借鉴了网上的一些代码,首先最简单的界面:
上面是TackBar,用于随时调整点击的时间间隔。
按钮则表示开始点击和结束点击。
废话不多上实现点击操作的类代码:
using System;
using System.Runtime.InteropServices;
namespace MouseClick
{
public class MouseFlag
{
[DllImport("user32.dll")]
static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);
[Flags]
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
}
public static void MouseLefDownEvent(int dx, int dy, uint data)
{
mouse_event(MouseEventFlag.LeftDown, dx, dy, data, UIntPtr.Zero);
}
public static void MouseLefupEvent(int dx, int dy, uint data)
{
mouse_event(MouseEventFlag.LeftUp, dx, dy, data, UIntPtr.Zero);
}
}
}
这里仅仅实现了左键按下和左键弹回两个方法,其他的鼠标操作直接照着改就行。
至于调用,我是这样做的:
using System;
using System.Windows.Forms;
namespace MouseClick
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = !timer1.Enabled;
}
private void timer1_Tick(object sender, EventArgs e)
{
MouseFlag.MouseLefDownEvent(this.button1.Location.X, this.button1.Location.Y, 0);
MouseFlag.MouseLefupEvent(this.button1.Location.X, this.button1.Location.Y, 0);
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
timer1.Interval = trackBar1.Value*1000;
label1.Text = trackBar1.Value + "秒";
}
}
}
按钮控制timer控件,timer事件执行鼠标按下和弹起操作。附上源代码:源代码