C# 监听鼠标操作

        前面两篇都在写关于键盘的监听操作以及衍生的一些自定义处理,这篇写一下关于鼠标的监听操作。

        虽然我们一般情况下鼠标用的比较多,但是比起来,反而键盘监听的功能比较实用,而鼠标感觉没什么监听的必要,但是如果你接触过自动化操作脚本之类的话,会发现其实鼠标监听的作用更大;通过这几篇内容,我想我们可以整合一下,做一个简单的操作录制功能,用来解决一下日常的繁琐切循环的操作,这个功能之前有发过一个简单的版本,但不是通过HOOK去实现的,而是让用户自己手动录制,再去执行。

        使用HOOK的话,直接监听一遍用户的操作,记录相应的动作即可,同时可操作性也有很大提高(emmmm,我觉得回头有时间我自己先封装下,可能用途还不小,同时想起之前写的关键字查询的工具,以为只是偶尔之需,万万没想到它在我生活、工作中也帮了我很多。。。)

这里代码只是做了简单的事件记录,其他操作可根据需求自行处理。

HOOK的工具类可参考前面的文章,这里不赘述,贴下需要更改的代码。

实现功能:

使用C#监听鼠标的按下、弹起等事件

开发环境:

开发工具: Visual Studio 2013

.NET Framework版本:4.5

实现代码:

/// <summary>
        /// 鼠标结构
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public class MouseHookStruct
        {
            public POINT pt; // 鼠标位置
            public int hWnd;
            public int wHitTestCode;
            public int dwExtraInfo;
        }

        /// <summary>
        /// 鼠标位置结构
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public class POINT
        {
            public int x;
            public int y;
        }


        //定义为鼠标钩子
        public int WH_MOUSE_LL = 14;

        //相关鼠标事件
        public event MouseEventHandler MouseDown;
        public event MouseEventHandler MouseUp;

        //相关动作
        public const int WM_MOUSEMOVE = 0x200; // 鼠标移动
        public const int WM_LBUTTONDOWN = 0x201;// 鼠标左键按下
        public const int WM_RBUTTONDOWN = 0x204;// 鼠标右键按下
        public const int WM_MBUTTONDOWN = 0x207;// 鼠标中键按下
        public const int WM_LBUTTONUP = 0x202;// 鼠标左键抬起
        public const int WM_RBUTTONUP = 0x205;// 鼠标右键抬起
        public const int WM_MBUTTONUP = 0x208;// 鼠标中键抬起

        //hookid
        private int hookID = 0;


        private int MouseHookProc(int nCode, int wParam, IntPtr lParam)
        {
            if ((nCode >= 0))
            {
                MouseHookStruct hookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));

                MouseEventArgs e = null;
                switch (wParam)
                {
                    case WM_LBUTTONDOWN:
                        e = new MouseEventArgs(MouseButtons.Left, 1, hookStruct.pt.x, hookStruct.pt.y, 0);
                        MouseDown(this, e);
                        break;
                    case WM_RBUTTONDOWN:
                        e = new MouseEventArgs(MouseButtons.Right, 1, hookStruct.pt.x, hookStruct.pt.y, 0);
                        MouseDown(this, e);
                        break;
                    case WM_LBUTTONUP:
                        e = new MouseEventArgs(MouseButtons.Left, 1, hookStruct.pt.x, hookStruct.pt.y, 0);
                        MouseUp(this, e);
                        break;
                    case WM_RBUTTONUP:
                        e = new MouseEventArgs(MouseButtons.Right, 1, hookStruct.pt.x, hookStruct.pt.y, 0);
                        MouseUp(this, e);
                        break;
                    default:
                        break;
                }
            }

            return CallNextHookEx(hookID, nCode, wParam, lParam);
        }
  private void MouseDown(object sender, MouseEventArgs e)
        {
            listBox1.Items.Add(string.Format("在{0},{1}位置按下了鼠标{2}键", e.X, e.Y, e.Button.ToString()));
        }

        private void MouseUp(object sender, MouseEventArgs e)
        {
            listBox1.Items.Add(string.Format("在{0},{1}位置松开了鼠标{2}键", e.X, e.Y, e.Button.ToString()));
        }

 HookUtil keyHook = new HookUtil();
        private void btnBegin_Click(object sender, EventArgs e)
        {
            keyHook.MouseDown += new MouseEventHandler(MouseDown);
            keyHook.MouseUp += new MouseEventHandler(MouseUp);
            keyHook.StartHook();
            btnBegin.Enabled = false;
            btnEnd.Enabled = true;
        }

实现效果:

由简入繁,拿来即用

更多精彩,请搜索公 Z 号:Csharp 小记

Microsoft Visual Studio 2010做的C#实时监控鼠标位置和左键点击时的位置实例,主要代码: public class MouseHook { private Point point; private Point Point { get { return point; } set { if (point != value) { point = value; if (MouseMoveEvent != null) { var e = new MouseEventArgs(MouseButtons.None, 0, point.X, point.Y, 0); MouseMoveEvent(this, e); } } } } private int hHook; private const int WM_LBUTTONDOWN = 0x201; public const int WH_MOUSE_LL = 14; public Win32Api.HookProc hProc; public MouseHook() { this.Point = new Point(); } public int SetHook() { hProc = new Win32Api.HookProc(MouseHookProc); hHook = Win32Api.SetWindowsHookEx(WH_MOUSE_LL, hProc, IntPtr.Zero, 0); return hHook; } public void UnHook() { Win32Api.UnhookWindowsHookEx(hHook); } private int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam) { Win32Api.MouseHookStruct MyMouseHookStruct = (Win32Api.MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(Win32Api.MouseHookStruct)); if (nCode < 0) { return Win32Api.CallNextHookEx(hHook, nCode, wParam, lParam); } else { if (MouseClickEvent != null) { MouseButtons button = MouseButtons.None; int clickCount = 0; switch ((Int32)wParam) { case WM_LBUTTONDOWN: button = MouseButtons.Left; clickCount = 1; break; } var e = new MouseEventArgs(button, clickCount, point.X, point.Y, 0); MouseClickEvent(this, e); } this.Point = new Point(MyMouseHookStruct.pt.x, MyMouseHookStruct.pt.y); return Win32Api.CallNextHookEx(hHook, nCode, wParam, lParam); } } public delegate void MouseMoveHandler(object sender, MouseEventArgs e); public event MouseMoveHandler MouseMoveEvent; public delegate void MouseClickHandler(object sender, MouseEventArgs e); public event MouseClickHandler MouseClickEvent; }
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值