下面程序可指定一连串重复动作,按顺序执行
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace Simulator
{
public partial class Form1 : Form
{
[System.Runtime.InteropServices.DllImport("user32")]
private static extern int mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
//移动鼠标
const int MOUSEEVENTF_MOVE = 0x0001;
//模拟鼠标左键按下
const int MOUSEEVENTF_LEFTDOWN = 0x0002;
//模拟鼠标左键抬起
const int MOUSEEVENTF_LEFTUP = 0x0004;
//模拟鼠标右键按下
const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
//模拟鼠标右键抬起
const int MOUSEEVENTF_RIGHTUP = 0x0010;
//模拟鼠标中键按下
const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
//模拟鼠标中键抬起
const int MOUSEEVENTF_MIDDLEUP = 0x0040;
//标示是否采用绝对坐标
const int MOUSEEVENTF_ABSOLUTE = 0x8000;
//模拟鼠标滚轮滚动操作,必须配合dwData参数
const int MOUSEEVENTF_WHEEL = 0x0800;
public Form1()
{
InitializeComponent();
}
List
this.timer2.Enabled = true;
this.timer2.Interval = 20000;
}
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "Start")
{
if (listBox1.Items.Count == 0)
{
MessageBox.Show("No found event");
return;
}
button1.Text = "Stop";
Rectangle ScreenArea = System.Windows.Forms.Screen.GetBounds(this);
int width = ScreenArea.Width;
int height = ScreenArea.Height;
foreach (object obj in EvenList)
{
dynamic x = ((dynamic)obj).x;
dynamic y = ((dynamic)obj).y;
dynamic wheel = ((dynamic)obj).dwData;
dynamic even = ((dynamic)obj).even;
// this.lab_result.Text = x + y + even;
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, Convert.ToInt32(x) * 65536 / width, Convert.ToInt32(y) * 65536 / height, 0, 0);//鼠标移动
System.Threading.Thread.Sleep(1000);
if (even == "LeftClick")
{
// mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,Convert.ToInt32(x)*65536 / 1024, Convert.ToInt32(y)* 65536 / 768, 0, 0);//鼠标移动
//mouse_event(MOUSEEVENTF_ABSOLUTE| MOUSEEVENTF_MOVE, Convert.ToInt32(x) * 65536 / width, Convert.ToInt32(y) * 65536 / height, 0, 0);//鼠标移动
//System.Threading.Thread.Sleep(1000);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
System.Threading.Thread.Sleep(1000);
}
else if (even == "LeftDoubleClick")
{
// mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,Convert.ToInt32(x)*65536 / 1024, Convert.ToInt32(y)* 65536 / 768, 0, 0);//鼠标移动
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
System.Threading.Thread.Sleep(1000);
}
else if (even == "RightClick")
{
mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
System.Threading.Thread.Sleep(1000);
}
else if (even == "RightDoubleClick")
{
mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
System.Threading.Thread.Sleep(1000);
}
else if (even == "LeftDown")
{
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
System.Threading.Thread.Sleep(1000);
}
else if (even == "LeftUp")
{
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
System.Threading.Thread.Sleep(1000);
}
else if (even == "MouseWheel")
{
//
// mouse_event( MOUSEEVENTF_WHEEL, 0, 0,2,0);
mouse_event(MOUSEEVENTF_WHEEL, 0, 0, Convert.ToInt32(wheel) * 65536 / height, 0);
}
}
this.lab_result.Text = "Done";
}
else
{
timer2.Enabled = false;
button1.Text = "Start";
lab_result.Text = "";
}
}
private void button2_Click(object sender, EventArgs e)
{
if (radio_wheel.Checked == false)
{
if (IsInteger(txb_x.Text.Trim()) == false || IsInteger(txb_y.Text.Trim()) == false)
{
MessageBox.Show("Input interger");
return;
}
}
if (txb_x.Text.Trim() == "")
{
txb_x.Text = "0";
txb_y.Text = "0";
}
if (txb_wheel.Text.Trim() == "")
{
txb_wheel.Text = "0";
}
string tmp = "";
if (radio_left1.Checked == true)
{
tmp = "LeftClick";
}
else if (radio_left2.Checked == true)
{
tmp = "LeftDoubleClick";
}
else if (radio_right1.Checked == true)
{
tmp = "RightClick";
}
else if (radio_right2.Checked == true)
{
tmp = "RightDoubleClick";
}
else if (radio_leftdown.Checked == true)
{
tmp = "LeftDown";
}
else if (radio_leftup.Checked == true)
{
tmp = "LeftUp";
}
else if (radio_wheel.Checked == true)
{
tmp = "MouseWheel";
}
else
{
MessageBox.Show("Select a Even");
return;
}
object obj = new { x = txb_x.Text.Trim(), y = txb_y.Text.Trim(), dwData = txb_wheel.Text.Trim(),even = tmp};
EvenList.Add(obj);
this.listBox1.Items.Add(obj);
txb_x.Text = "";
txb_y.Text = "";
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
Point p = new Point(MousePosition.X, MousePosition.Y);
string x = p.X.ToString();
string y = p.Y.ToString();
this.label1.Text = "(" + x + "," + y + ")";
}
private void Form1_Click(object sender, EventArgs e)
{
string x = Cursor.Position.X.ToString();
string y = Cursor.Position.Y.ToString();
this.label1.Text = "(" + x + "," + y + ")";
}
private void timer1_Tick(object sender, EventArgs e)
{
string x = Cursor.Position.X.ToString();
string y = Cursor.Position.Y.ToString();
this.label1.Text = "(" + x + "," + y + ")";
}
private void btn_reset_Click(object sender, EventArgs e)
{
EvenList.Clear();
this.listBox1.Items.Clear();
timer2.Enabled = false;
}
private void timer2_Tick(object sender, EventArgs e)
{
if (this.lab_result.Text == "Done")
{
lab_result.Text = "";
button1_Click(sender, e);
}
}
public bool IsNumber(string value)
{
Regex r = new Regex(@"^/d+(/.)?/d*$");
return r.IsMatch(value);
}
public bool IsInteger(string value)
{
Regex r = new Regex(@"^\d*$");
return r.IsMatch(value);
}
private void btn_saveEven_Click(object sender, EventArgs e)
{
SaveFileDialog savefileddialog1 = new SaveFileDialog();
savefileddialog1.Filter = "文本文档(*.txt)|*.txt|所有文件(*.*)|*.*";
if (savefileddialog1.ShowDialog() == DialogResult.OK)
{
string filepath = savefileddialog1.FileName;
StreamWriter sw = new StreamWriter(filepath);
for (int i = 0; i < listBox1.Items.Count; i++)
{
string str = JsonConvert.SerializeObject(EvenList[i]);
sw.WriteLine(str);
}
sw.Flush();
sw.Close();
MessageBox.Show("Save Success!");
}
}
private void btn_loadEven_Click(object sender, EventArgs e)
{
timer2.Enabled = false;
OpenFileDialog open = new OpenFileDialog();
open.Filter = "txt文件|*.txt";
open.FilterIndex = 1;
if (open.ShowDialog() == DialogResult.OK)
{
this.listBox1.Items.Clear();
string filepath = open.FileName;
StreamReader sr = File.OpenText(filepath);
string nextline;
while ((nextline = sr.ReadLine()) != null)
{
// this.listBox1.Items.Add(nextline);
EvenList.Add(JsonConvert.DeserializeObject(nextline));
this.listBox1.Items.Add(nextline);
}
sr.Dispose();
sr.Close();
}
}
}
}
//MOUSEEVENTF_MOVE:表明发生移动。
//MOUSEEVENTF_LEFTDOWN:表明接按下鼠标左键。
//MOUSEEVENTF_LEFTUP:表明松开鼠标左键。
//MOUSEEVENTF_RIGHTDOWN:表明按下鼠标右键。
//MOUSEEVENTF_RIGHTUP:表明松开鼠标右键。
//MOUSEEVENTF_MIDDLEDOWN:表明按下鼠标中键。
//MOUSEEVENTF_MIDDLEUP:表明松开鼠标中键。
//MOUSEEVENTF_WHEEL:在Windows NT中如果鼠标有一个轮,表明鼠标轮被移动。移动的数量由dwData给出