下面程序可指定一連串重復(fù)動(dòng)作,按順序執(zhí)行
?
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);
? ? ? ? //移動(dòng)鼠標(biāo)?
? ? ? ? const int MOUSEEVENTF_MOVE = 0x0001;
? ? ? ? //模擬鼠標(biāo)左鍵按下?
? ? ? ? const int MOUSEEVENTF_LEFTDOWN = 0x0002;
? ? ? ? //模擬鼠標(biāo)左鍵抬起?
? ? ? ? const int MOUSEEVENTF_LEFTUP = 0x0004;
? ? ? ? //模擬鼠標(biāo)右鍵按下?
? ? ? ? const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
? ? ? ? //模擬鼠標(biāo)右鍵抬起?
? ? ? ? const int MOUSEEVENTF_RIGHTUP = 0x0010;
? ? ? ? //模擬鼠標(biāo)中鍵按下?
? ? ? ? const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
? ? ? ? //模擬鼠標(biāo)中鍵抬起?
? ? ? ? const int MOUSEEVENTF_MIDDLEUP = 0x0040;
? ? ? ? //標(biāo)示是否采用絕對(duì)坐標(biāo)?
? ? ? ? const int MOUSEEVENTF_ABSOLUTE = 0x8000;
? ? ? ? //模擬鼠標(biāo)滾輪滾動(dòng)操作,必須配合dwData參數(shù)
? ? ? ? const int MOUSEEVENTF_WHEEL = 0x0800;
? ? ? ??
? ? ? ??
? ? ? ? public Form1()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? ? ??
? ? ? ? }
? ? ? ? List<object> EvenList = new List<object>();
? ? ? ? private void Form1_Load(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? this.timer1.Enabled = true;
? ? ? ? ? ? this.timer1.Interval = 10;
? ? ? ? ? ? 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);//鼠標(biāo)移動(dòng)
? ? ? ? ? ? ? ? ? ? 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);//鼠標(biāo)移動(dòng)
? ? ? ? ? ? ? ? ? ? ? ? //mouse_event(MOUSEEVENTF_ABSOLUTE| MOUSEEVENTF_MOVE, Convert.ToInt32(x) * 65536 / width, Convert.ToInt32(y) * 65536 / height, 0, 0);//鼠標(biāo)移動(dòng)
? ? ? ? ? ? ? ? ? ? ? ? //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);//鼠標(biāo)移動(dòng)
? ? ? ? ? ? ? ? ? ? ? ? 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 + ")";
? ? ? ? }文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-559345.html
? ? ? ? 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:表明發(fā)生移動(dòng)。
//MOUSEEVENTF_LEFTDOWN:表明接按下鼠標(biāo)左鍵。
//MOUSEEVENTF_LEFTUP:表明松開鼠標(biāo)左鍵。
//MOUSEEVENTF_RIGHTDOWN:表明按下鼠標(biāo)右鍵。
//MOUSEEVENTF_RIGHTUP:表明松開鼠標(biāo)右鍵。
//MOUSEEVENTF_MIDDLEDOWN:表明按下鼠標(biāo)中鍵。
//MOUSEEVENTF_MIDDLEUP:表明松開鼠標(biāo)中鍵。
//MOUSEEVENTF_WHEEL:在Windows NT中如果鼠標(biāo)有一個(gè)輪,表明鼠標(biāo)輪被移動(dòng)。移動(dòng)的數(shù)量由dwData給出文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-559345.html
到了這里,關(guān)于C#實(shí)現(xiàn)鍵盤鼠標(biāo)模擬器的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!