作者: 乔克斯
查看: 12312|回复: 23

主题标签Tag

打印 上一主题 下一主题

[源码] 【记录鼠标轨迹】鼠标记录器,可播放轨迹

[复制链接]
跳转到指定楼层
楼主
乔克斯 发表于 2014-8-10 01:24:15 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
查看: 12312|回复: 23
C#鼠标记录器
一款可以录制鼠标轨迹并且播放鼠标轨迹的Winform源码案例。
效果截图:


部分窗体源码展示:
[C#] 纯文本查看 复制代码
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.IO;

namespace AutoClick
{
        /// <summary>
        /// Form1 的摘要说明。
        /// </summary>
        public class MainForm : System.Windows.Forms.Form
        {
                private System.ComponentModel.IContainer components;
                private System.Windows.Forms.Timer RecordOp;
                private System.Windows.Forms.Timer startOp;
                private System.Windows.Forms.Panel panel1;
                private System.Windows.Forms.GroupBox groupBox1;
                private System.Windows.Forms.GroupBox groupBox2;
                private System.Windows.Forms.Label label1;
                private System.Windows.Forms.Label label2;
                private System.Windows.Forms.Label label3;
                private System.Windows.Forms.Label label4;
                private System.Windows.Forms.Button btnSave;
                private System.Windows.Forms.Button btnRead;
                private System.Windows.Forms.NumericUpDown nudPlayNum;
                private System.Windows.Forms.TrackBar tkbSpeed;
                private System.Windows.Forms.CheckBox chkIsL;
                
                ArrayList currentOp=new ArrayList();

                [DllImport("user32.dll")]
                static extern bool SetCursorPos(int X, int Y); 
                [DllImport("user32.dll", EntryPoint="mouse_event")]
                public static extern void mouse_event (
                        int dwFlags,
                        int dx,
                        int dy,
                        int cButtons,
                        int dwExtraInfo
                        );

                [DllImport("user32.dll", SetLastError = true)]
                public static extern bool RegisterHotKey(
                        IntPtr hWnd, // handle to window 
                        int id, // hot key identifier 
                        KeyModifiers fsModifiers, // key-modifier options 
                        Keys vk // virtual-key code 
                        );

                [DllImport("user32.dll", SetLastError = true)]
                public static extern bool UnregisterHotKey(
                        IntPtr hWnd, // handle to window 
                        int id // hot key identifier 
                        );


                [Flags()]
                        public enum KeyModifiers
                {
                        None = 0,
                        Alt = 1,
                        Control = 2,
                        Shift = 4,
                        Windows = 8
                } 

                [DllImport("user32.dll", EntryPoint="GetCursorPos")]
                public static extern int GetCursorPos (
                        ref Point lpPoint
                        );

                public const int MOUSEEVENTF_LEFTDOWN = 0x2;
                public const int MOUSEEVENTF_LEFTUP = 0x4;
                public const int MOUSEEVENTF_RIGHTDOWN = 0x8;
                public const int MOUSEEVENTF_RIGHTUP = 0x10;
                public MainForm()
                {
                        //
                        // Windows 窗体设计器支持所必需的
                        //
                        InitializeComponent();
                        Application.EnableVisualStyles();
                        //
                        // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
                        //
                }

                /// <summary>
                /// 清理所有正在使用的资源。
                /// </summary>
                protected override void Dispose( bool disposing )
                {
                        if( disposing )
                        {
                                if (components != null) 
                                {
                                        components.Dispose();
                                }
                        }
                        base.Dispose( disposing );
                }

                #region Windows 窗体设计器生成的代码
                /// <summary>
                /// 设计器支持所需的方法 - 不要使用代码编辑器修改
                /// 此方法的内容。
                /// </summary>
                private void InitializeComponent()
                {
                        this.components = new System.ComponentModel.Container();
                        System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
                        this.RecordOp = new System.Windows.Forms.Timer(this.components);
                        this.startOp = new System.Windows.Forms.Timer(this.components);
                        this.panel1 = new System.Windows.Forms.Panel();
                        this.chkIsL = new System.Windows.Forms.CheckBox();
                        this.label4 = new System.Windows.Forms.Label();
                        this.groupBox2 = new System.Windows.Forms.GroupBox();
                        this.label3 = new System.Windows.Forms.Label();
                        this.label2 = new System.Windows.Forms.Label();
                        this.tkbSpeed = new System.Windows.Forms.TrackBar();
                        this.nudPlayNum = new System.Windows.Forms.NumericUpDown();
                        this.label1 = new System.Windows.Forms.Label();
                        this.groupBox1 = new System.Windows.Forms.GroupBox();
                        this.btnSave = new System.Windows.Forms.Button();
                        this.btnRead = new System.Windows.Forms.Button();
                        this.panel1.SuspendLayout();
                        this.groupBox2.SuspendLayout();
                        ((System.ComponentModel.ISupportInitialize)(this.tkbSpeed)).BeginInit();
                        ((System.ComponentModel.ISupportInitialize)(this.nudPlayNum)).BeginInit();
                        this.groupBox1.SuspendLayout();
                        this.SuspendLayout();
                        // 
                        // RecordOp
                        // 
                        this.RecordOp.Interval = 30;
                        this.RecordOp.Tick += new System.EventHandler(this.RecordOp_Tick);
                        // 
                        // startOp
                        // 
                        this.startOp.Interval = 20;
                        this.startOp.Tick += new System.EventHandler(this.startOp_Tick);
                        // 
                        // panel1
                        // 
                        this.panel1.BackColor = System.Drawing.Color.Azure;
                        this.panel1.Controls.Add(this.chkIsL);
                        this.panel1.Controls.Add(this.label4);
                        this.panel1.Controls.Add(this.groupBox2);
                        this.panel1.Controls.Add(this.groupBox1);
                        this.panel1.Dock = System.Windows.Forms.DockStyle.Right;
                        this.panel1.Location = new System.Drawing.Point(296, 0);
                        this.panel1.Name = "panel1";
                        this.panel1.Size = new System.Drawing.Size(160, 266);
                        this.panel1.TabIndex = 0;
                        // 
                        // chkIsL
                        // 
                        this.chkIsL.Location = new System.Drawing.Point(10, 240);
                        this.chkIsL.Name = "chkIsL";
                        this.chkIsL.Size = new System.Drawing.Size(76, 24);
                        this.chkIsL.TabIndex = 6;
                        this.chkIsL.Text = "开启延迟";
                        this.chkIsL.CheckedChanged += new System.EventHandler(this.chkIsL_CheckedChanged);
                        // 
                        // label4
                        // 
                        this.label4.ForeColor = System.Drawing.SystemColors.AppWorkspace;
                        this.label4.Location = new System.Drawing.Point(106, 244);
                        this.label4.Name = "label4";
                        this.label4.Size = new System.Drawing.Size(48, 14);
                        this.label4.TabIndex = 5;
                        this.label4.Text = "-Blake-";
                        // 
                        // groupBox2
                        // 
                        this.groupBox2.Controls.Add(this.label3);
                        this.groupBox2.Controls.Add(this.label2);
                        this.groupBox2.Controls.Add(this.tkbSpeed);
                        this.groupBox2.Controls.Add(this.nudPlayNum);
                        this.groupBox2.Controls.Add(this.label1);
                        this.groupBox2.Location = new System.Drawing.Point(6, 100);
                        this.groupBox2.Name = "groupBox2";
                        this.groupBox2.Size = new System.Drawing.Size(150, 138);
                        this.groupBox2.TabIndex = 3;
                        this.groupBox2.TabStop = false;
                        this.groupBox2.Text = "播放设置";
                        // 
                        // label3
                        // 
                        this.label3.Location = new System.Drawing.Point(4, 64);
                        this.label3.Name = "label3";
                        this.label3.Size = new System.Drawing.Size(78, 14);
                        this.label3.TabIndex = 4;
                        this.label3.Text = "播放速度:";
                        // 
                        // label2
                        // 
                        this.label2.AutoSize = true;
                        this.label2.Location = new System.Drawing.Point(8, 114);
                        this.label2.Name = "label2";
                        this.label2.Size = new System.Drawing.Size(140, 17);
                        this.label2.TabIndex = 3;
                        this.label2.Text = "慢        中        快";
                        // 
                        // tkbSpeed
                        // 
                        this.tkbSpeed.AutoSize = false;
                        this.tkbSpeed.LargeChange = 10;
                        this.tkbSpeed.Location = new System.Drawing.Point(2, 84);
                        this.tkbSpeed.Maximum = 50;
                        this.tkbSpeed.Minimum = -50;
                        this.tkbSpeed.Name = "tkbSpeed";
                        this.tkbSpeed.Size = new System.Drawing.Size(144, 28);
                        this.tkbSpeed.SmallChange = 5;
                        this.tkbSpeed.TabIndex = 2;
                        this.tkbSpeed.TickFrequency = 5;
                        this.tkbSpeed.Scroll += new System.EventHandler(this.tkbSpeed_Scroll);
                        // 
                        // nudPlayNum
                        // 
                        this.nudPlayNum.Location = new System.Drawing.Point(6, 34);
                        this.nudPlayNum.Name = "nudPlayNum";
                        this.nudPlayNum.Size = new System.Drawing.Size(138, 21);
                        this.nudPlayNum.TabIndex = 1;
                        this.nudPlayNum.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
                        this.nudPlayNum.Value = new System.Decimal(new int[] {
                                                                                                                                         1,
                                                                                                                                         0,
                                                                                                                                         0,
                                                                                                                                         0});
                        // 
                        // label1
                        // 
                        this.label1.Location = new System.Drawing.Point(4, 18);
                        this.label1.Name = "label1";
                        this.label1.Size = new System.Drawing.Size(142, 23);
                        this.label1.TabIndex = 0;
                        this.label1.Text = "播放次数(0为循环播放):";
                        // 
                        // groupBox1
                        // 
                        this.groupBox1.Controls.Add(this.btnSave);
                        this.groupBox1.Controls.Add(this.btnRead);
                        this.groupBox1.Location = new System.Drawing.Point(6, 4);
                        this.groupBox1.Name = "groupBox1";
                        this.groupBox1.Size = new System.Drawing.Size(150, 90);
                        this.groupBox1.TabIndex = 2;
                        this.groupBox1.TabStop = false;
                        this.groupBox1.Text = "文件操作";
                        // 
                        // btnSave
                        // 
                        this.btnSave.FlatStyle = System.Windows.Forms.FlatStyle.System;
                        this.btnSave.Location = new System.Drawing.Point(6, 20);
                        this.btnSave.Name = "btnSave";
                        this.btnSave.Size = new System.Drawing.Size(136, 24);
                        this.btnSave.TabIndex = 0;
                        this.btnSave.Text = "保存录制";
                        this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
                        // 
                        // btnRead
                        // 
                        this.btnRead.FlatStyle = System.Windows.Forms.FlatStyle.System;
                        this.btnRead.Location = new System.Drawing.Point(6, 54);
                        this.btnRead.Name = "btnRead";
                        this.btnRead.Size = new System.Drawing.Size(136, 24);
                        this.btnRead.TabIndex = 1;
                        this.btnRead.Text = "读取文件";
                        this.btnRead.Click += new System.EventHandler(this.btnRead_Click);
                        // 
                        // MainForm
                        // 
                        this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
                        this.BackColor = System.Drawing.Color.LightCyan;
                        this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
                        this.ClientSize = new System.Drawing.Size(456, 266);
                        this.Controls.Add(this.panel1);
                        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
                        this.MaximizeBox = false;
                        this.Name = "MainForm";
                        this.ShowInTaskbar = false;
                        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
                        this.Text = "鼠标按键V1.0";
                        this.TopMost = true;
                        this.Load += new System.EventHandler(this.MainForm_Load);
                        this.panel1.ResumeLayout(false);
                        this.groupBox2.ResumeLayout(false);
                        ((System.ComponentModel.ISupportInitialize)(this.tkbSpeed)).EndInit();
                        ((System.ComponentModel.ISupportInitialize)(this.nudPlayNum)).EndInit();
                        this.groupBox1.ResumeLayout(false);
                        this.ResumeLayout(false);

                }
                #endregion

                /// <summary>
                /// 应用程序的主入口点。
                /// </summary>
                [STAThread]
                static void Main() 
                {
                        Application.Run(new MainForm());
                }
                UserActivityHook mouseHook=new UserActivityHook();
                private void MainForm_Load(object sender, System.EventArgs e)
                {
                        RegisterHotKey(Handle, 1000, KeyModifiers.Control, Keys.F12);
                        RegisterHotKey(Handle, 1001, KeyModifiers.Control, Keys.F1);
                        RegisterHotKey(Handle, 1002, KeyModifiers.Control, Keys.F2);
                        RegisterHotKey(Handle, 1003, KeyModifiers.Control, Keys.F3);
                        RegisterHotKey(Handle, 1004, KeyModifiers.Control, Keys.F4);
                        mouseHook.Stop();
                        mouseHook.OnMouseActivity+=new MouseEventHandler(mouseHook_OnMouseActivity);
                }
                
                protected override void WndProc(ref Message m)//监视Windows消息
                {
                        const int WM_HOTKEY = 0x0312;//按快捷键
                        switch (m.Msg)
                        {
                                case WM_HOTKEY:
                                        if (m.WParam.ToInt32() == 1000)
                                        {
                                                this.Visible = !this.Visible;
                                        }
                                        else if (m.WParam.ToInt32() == 1001)
                                        {
                                                StartRecord();
                                        }
                                        else if (m.WParam.ToInt32() == 1002)
                                        {
                                                StopRecord();
                                        }
                                        else if (m.WParam.ToInt32() == 1003)
                                        {
                                                StartPlay();
                                        }
                                        else if (m.WParam.ToInt32() == 1004)
                                        {
                                                StopPlay();
                                        }
                                        break;
                        }
                        base.WndProc(ref m);
                }

                private void StartRecord()
                {
                        currentOp.Clear();
                        mouseHook.Start();
                }
                private void StopRecord()
                {
                        RecordOp.Stop();
                        mouseHook.Stop();
                }
                private void StartPlay()
                {
                        startOp.Start();
                }
                private void StopPlay()
                {
                        currentPs=0;
                        startNum=0;
                        startOp.Stop();
                }

                private void RecordOp_Tick(object sender, System.EventArgs e)
                {
                        Point p=new Point();
                        GetCursorPos(ref p);
                        OpClass op=new OpClass(p,MouseButtons.None);
                        currentOp.Add(op);
                }
                int currentPs=0;
                int startNum=0;
                private void startOp_Tick(object sender, System.EventArgs e)
                {
                        if(currentPs==currentOp.Count)
                        {
                                if(this.nudPlayNum.Value==0||startNum<this.nudPlayNum.Value-1)
                                {
                                        currentPs=0;
                                        startNum++;
                                        return;
                                }
                                        currentPs=0;
                                        startNum=0;
                                        startOp.Stop();
                        }
                        if(currentOp[currentPs]!=null)
                        {
                                OpClass po=(OpClass)currentOp[currentPs];
                        
                                SetCursorPos(po.GetLocation.X,po.GetLocation.Y);
                                if(po.GetMouseClick==MouseButtons.Left)
                                {
                                        mouse_event(MOUSEEVENTF_LEFTDOWN,po.GetLocation.X,po.GetLocation.Y,0,0);
                                        mouse_event(MOUSEEVENTF_LEFTUP,po.GetLocation.X,po.GetLocation.Y,0,0);
                                }
                                else if(po.GetMouseClick==MouseButtons.Right)
                                {
                                        mouse_event(MOUSEEVENTF_RIGHTDOWN,po.GetLocation.X,po.GetLocation.Y,0,0);
                                        mouse_event(MOUSEEVENTF_RIGHTUP,po.GetLocation.X,po.GetLocation.Y,0,0);
                                }
                                
                        }
                        currentPs++;
                }
                private void mouseHook_OnMouseActivity(object sender, MouseEventArgs e)
                {
                        OpClass op=new OpClass(new Point(e.X,e.Y),e.Button);
                        currentOp.Add(op);

                }

                private void tkbSpeed_Scroll(object sender, System.EventArgs e)
                {
                        this.startOp.Interval=51-this.tkbSpeed.Value;
                }

                private void chkIsL_CheckedChanged(object sender, System.EventArgs e)
                {
                        this.RecordOp.Start();
                }
                private void SaveCurrentOP(string filePath)
                {
                        StreamWriter sw=new StreamWriter(filePath,false,System.Text.Encoding.Default);
                        for(int i=0;i<currentOp.Count;i++)
                        {
                                OpClass op=(OpClass)currentOp;
                                sw.WriteLine("["+op.GetLocation.X+","+op.GetLocation.Y+"]="+op.GetMouseClick.ToString());
                        }
                        sw.Close();
                        MessageBox.Show("成功保存!");
                }
                private void btnSave_Click(object sender, System.EventArgs e)
                {
                        SaveFileDialog sfd=new SaveFileDialog();
                        sfd.Filter="*.BLK|*.BLK";
                        sfd.FileName=Application.StartupPath+"\\鼠标操作.BLK";
                        if(sfd.ShowDialog()==DialogResult.OK)
                        {
                                SaveCurrentOP(sfd.FileName);
                        }
                }
                private void OpenFile(string filePath)
                {
                        StreamReader sr=new StreamReader(filePath,System.Text.Encoding.Default);
                        currentOp.Clear();
                        while(sr.Peek()>0)
                        {
                                string temp=sr.ReadLine();
                                temp=temp.Replace("[","");
                                int cx=int.Parse(temp.Substring(0,temp.IndexOf(",")));
                                temp=temp.Replace(cx+",","");
                                int cy=int.Parse(temp.Substring(0,temp.IndexOf("]")));
                                temp=temp.Replace(cy+"]=","");
                                MouseButtons mouseClick=MouseButtons.None;
                                if(temp=="Left")
                                {
                                        mouseClick=MouseButtons.Left;
                                }
                                else if(temp=="Right")
                                {
                                        mouseClick=MouseButtons.Right;
                                }
                                else if(temp=="Middle")
                                {
                                        mouseClick=MouseButtons.Middle;
                                }
                                OpClass op=new OpClass(new Point(cx,cy),mouseClick);
                                currentOp.Add(op);
                        }
                        sr.Close();
                        MessageBox.Show("读取成功!");
                }
                private void btnRead_Click(object sender, System.EventArgs e)
                {
                        OpenFileDialog ofd=new OpenFileDialog();
                        ofd.Filter="*.BLK|*.BLK";
                        ofd.FileName=Application.StartupPath+"\\鼠标操作.BLK";
                        if(ofd.ShowDialog()==DialogResult.OK)
                        {
                                OpenFile(ofd.FileName);
                        }
                }
        }
        class OpClass
        {
                public OpClass(Point location,MouseButtons mouseClick)
                {
                        this.location=location;
                        this.mouseClick=mouseClick;
                }
                public OpClass(Point location,Keys key)
                {
                        this.location=location;
                        this.mouseClick=mouseClick;
                }
                private Point location;
                private MouseButtons mouseClick;
        private Keys key;
                public Point GetLocation
                {
                        get
                        {
                                return this.location;
                        }
                        set
                        {
                                this.location=value;
                        }
                }
                public MouseButtons GetMouseClick
                {
                        get
                        {
                                return this.mouseClick;
                        }
                        set
                        {
                                this.mouseClick=value;
                        }
                }
                new public string ToString()
                {
                        return ""+this.location.X+","+this.location.Y+"    "+this.mouseClick.ToString();
                }
        }
}


案例源码下载:
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏6 转播转播
回复 论坛版权

使用道具 举报

沙发
liuyilin 发表于 2014-12-3 10:55:03 | 只看该作者
不错,多谢,学习
板凳
coco345 发表于 2015-8-13 21:55:46 | 只看该作者
有意思,可惜没金币了
地板
 楼主| 乔克斯 发表于 2015-8-14 10:04:35 | 只看该作者
coco345 发表于 2015-8-13 21:55
有意思,可惜没金币了

发帖,回帖,签到,任务,上传资源。都可以获得金币。
5#
wxzhouq 发表于 2015-11-29 13:18:28 | 只看该作者
有意思,可惜没金币了
6#
舞影雾踪 发表于 2016-1-19 14:10:38 | 只看该作者
不错,多谢,学习
7#
yinghao2005 发表于 2016-10-10 23:41:46 | 只看该作者
没金币怎么挣
8#
1723031190 发表于 2016-10-27 17:39:53 | 只看该作者
不错不错好好学习。。。。
9#
莫语希 发表于 2017-4-18 13:23:56 | 只看该作者
#在这里快速回复#看看。。。。。。。。。。。。。。。。。。。。。。
10#
xiao大爷 发表于 2017-4-18 16:00:15 | 只看该作者
牛人好多啊
您需要登录后才可以回帖 登录 | 加入CSkin博客

本版积分规则

QQ|申请友链|小黑屋|手机版|Archiver|CSkin ( 粤ICP备13070794号

Powered by Discuz! X3.2  © 2001-2013 Comsenz Inc.  Designed by ARTERY.cn
GMT+8, 2024-4-20 02:10, Processed in 0.618054 second(s), 36 queries , Gzip On.

快速回复 返回顶部 返回列表