码迷,mamicode.com
首页 > Windows程序 > 详细

鼠标 hook 源码 C#版

时间:2020-01-19 22:21:54      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:load   stat   cal   rop   builder   app   static   send   namespace   

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace MouseHook20200119
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        #region  所需

        public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
        //整数14表示全局钩子类型,其他类型对应的数字可以从MicrosoftSDK中的Winuser.h头文件中查看
        public const int WH_MOUSE = 14;
        [DllImport("user32.dll")]
        public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
        [DllImport("user32.dll")]
        public static extern bool UnhookWindowsHookEx(int idHook);
        [DllImport("user32.dll")]
        public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);
        #endregion

        static int hHook = 0;
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        StringBuilder sb = new StringBuilder();
        public int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            switch ((int)wParam)
            {
                case 513: //0x201
                          //sb.AppendLine("鼠标左键按下");

                    break;
                case 516://0x204
                    //sb.AppendLine("鼠标右键");
                    break;
                case 519:
                    //sb.AppendLine("鼠标中键");
                    break;
                case 514://0x202
                         //sb.AppendLine("鼠标左键松开");

                    break;
                case 517:
                    //sb.AppendLine("鼠标右键");
                    break;
                case 520:
                    //sb.AppendLine("鼠标中键");
                    break;
            }
            //var t = sb.ToString();
            //if (nCode < 0)
            //{
            //    return 1;
            //    //返回真;即截断消息传递,
            //    //否则不截断
            //    //return CallNextHookEx(hHook, nCode, wParam, lParam);
            //}
            //else
            //{
            //    return 1;
            //}
            return CallNextHookEx(hHook, nCode, wParam, lParam);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (hHook == 0) //句柄未初始化
            {
                hHook = SetWindowsHookEx(WH_MOUSE,
                   MouseHookProc,
                   IntPtr.Zero,
                   0
                    );
            }
            if (hHook > 0)
            {
                MessageBox.Show("安装钩子成功!");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            UnhookWindowsHookEx(hHook);
            MessageBox.Show(hHook.ToString());

        }
    }
}

鼠标 hook 源码 C#版

标签:load   stat   cal   rop   builder   app   static   send   namespace   

原文地址:https://www.cnblogs.com/enych/p/12215566.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!