C#实现键盘鼠标模拟器

下面程序可指定一连串重复动作,按顺序执行

 

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<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);//鼠标移动

                    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给出

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WinAPI-Wrapper 模拟鼠标点击 用于模拟鼠标移动、点击、窗口操作等的Windows API包装器类。 API 下面是一些可用的方法的总结。有更多的方法和类,比下面列出的要多,但目的是要大致了解包装器能做什么。要查看关于特定方法的详细信息和参数的详细信息,请查看代码本身,因为它的注释很好。 Mouse.cs public static void LeftClick(); public static void RightClick(); public static void MiddleClick(); public static void LeftDown(); public static void LeftUp(); public static void RightDown(); public static void RightUp(); public static void MiddleDown(); public static void MiddleUp(); public static void Move(int x, int y); public static void LeftDrag(Point point1, Point point2, int interval, int lag); Window.cs public static bool DoesExist(string windowTitle); public static IntPtr Get(string windowTitle); public static IntPtr GetFocused(); public static void SetFocused(IntPtr hWnd); public static bool IsFocused(IntPtr hWnd); public static void Move(IntPtr hWnd, int x, int y); public static void Resize(IntPtr hWnd, int width, int height); public static void Hide(IntPtr hWnd); public static void Show(IntPtr hWnd); public static Rectangle GetDimensions(IntPtr hWnd); public static Size GetSize(IntPtr hWnd); public static Point GetLocation(IntPtr hWnd); public static string GetTitle(IntPtr hWnd); public static void SetTitle(IntPtr hWnd, string title); public static void Maximize(IntPtr hWnd); public static void Minimize(IntPtr hWnd); public static void Normalize(IntPtr hWnd); public static Bitmap Screenshot(IntPtr hWnd); public static void RemoveMenu(IntPtr hWnd); public static void Close(IntPtr hWnd); public static void DisableCloseButton(IntPtr hWnd); public static void DisableMaximizeButton(IntPtr hWnd); public static void DisableMinimizeButton(IntPtr hWnd); public static void EnableMouseTransparency(IntPtr hWnd); public static Point ConvertToWindowCoordinates(IntPtr hWnd, int x, int y); public static Point GetCoordinateRelativeToWindow(IntPtr hWnd); Desktop.cs public static Bitmap Screenshot(); public static void HideTaskBar(); public static void ShowTaskBar(); public static int GetWidth(); public static int GetHeight(); 使用 在windows api文件夹中编译代码会产生一个.dll文件。任何引用这个.dll的ccode都可以使用包装器。
C#提供了一个`System.Windows.Forms`命名空间,其中包含了许多与用户界面相关的类及方法,可以用来模拟键盘鼠标操作。 下面是一些常用的模拟键盘鼠标操作的方法: 1. 模拟按键操作 可以使用`SendKeys`类中的`Send`方法来模拟按键操作。例如,要模拟按下“a”键,可以使用以下代码: ``` SendKeys.Send("a"); ``` 2. 模拟组合键操作 如果要模拟组合键操作,例如Ctrl+C复制操作,可以将组合键的按键值用“+”连接起来,如下所示: ``` SendKeys.Send("^c"); ``` 其中,“^”表示Ctrl键,“+”表示Shift键,“%”表示Alt键,“{F1}”表示F1键,等等。 3. 模拟鼠标点击操作 可以使用`System.Windows.Forms.Cursor`类中的`Position`属性来获取当前鼠标的位置,然后使用`System.Windows.Forms.Mouse`类中的`LeftClick`或`RightClick`方法来模拟鼠标左键或右键点击操作。例如,要模拟在屏幕上(100,100)的位置进行左键点击操作,可以使用以下代码: ``` Cursor.Position = new Point(100, 100); Mouse.LeftClick(); ``` 4. 模拟鼠标移动操作 可以使用`System.Windows.Forms.Cursor`类中的`Position`属性来设置鼠标的位置,从而模拟鼠标移动操作。例如,要将鼠标移到屏幕上(200,200)的位置,可以使用以下代码: ``` Cursor.Position = new Point(200, 200); ``` 需要注意的是,模拟键盘鼠标操作可能会对系统产生影响,因此在使用时要谨慎,并且尽量避免在用户不知情的情况下进行操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值