C#使用鼠标钩子

FormMain.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//
using HookTest;

namespace HookTest
{
    public partial class FormMain : Form
    {
        //钩子类型
        /// WH_MOUSE_LL -> 14
        public const int WH_MOUSE_LL = 14;
        //
        //Windows消息
        /// WM_LBUTTONUP -> 0x0202
        public const int WM_LBUTTONUP = 514;

        ClassWinAPI.HOOKPROC hHockProc;//钩子处理函数
        int hMouseHook;//钩子句柄

        public FormMain()
        {
            InitializeComponent();
        }
        public IntPtr MouseHookProc(int code, int wParam, System.IntPtr lParam)
        {
            //Windows消息
            uint msg = (uint)wParam;
            //鼠标信息
            ClassWinAPI.MouseHookStruct mouseHookStruct = (ClassWinAPI.MouseHookStruct) System.Runtime.InteropServices.Marshal.PtrToStructure(
                lParam,typeof(ClassWinAPI.MouseHookStruct));
            if (msg == WM_LBUTTONUP)
            {
                this.Text = mouseHookStruct.pt.x.ToString() + "--" + mouseHookStruct.pt.y.ToString();
            }
            //
            IntPtr pMouseHook = System.Runtime.InteropServices.Marshal.AllocHGlobal(System.Runtime.InteropServices.Marshal.SizeOf(hMouseHook));
            System.Runtime.InteropServices.Marshal.StructureToPtr(hMouseHook, pMouseHook, true);
            System.Runtime.InteropServices.Marshal.DestroyStructure(pMouseHook,typeof(IntPtr));
            return ClassWinAPI.CallNextHookEx(pMouseHook,code,wParam,lParam);
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            hHockProc = new ClassWinAPI.HOOKPROC(MouseHookProc);
            hMouseHook = ClassWinAPI.SetWindowsHookExW(WH_MOUSE_LL, hHockProc, IntPtr.Zero,0);
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            ClassWinAPI.UnhookWindowsHookEx(hMouseHook);
        }
    }
}

ClassWinAPI.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HookTest
{
    class ClassWinAPI
    {
        [System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute(System.Runtime.InteropServices.CallingConvention.StdCall)]
        public delegate IntPtr HOOKPROC(int code, int wParam, System.IntPtr lParam);

        [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
        public struct POINT
        {
            public int x;
            public int y;
        };

        [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
        public struct MouseHookStruct
        {
            public POINT pt;
            public int hwnd;
            public int wHitTestCode;
            public int dwExtraInfo;
        };

        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "SetWindowsHookExW")]
        public static extern int SetWindowsHookExW(
            int idHook,
            HOOKPROC lpfn,
            [System.Runtime.InteropServices.InAttribute()] System.IntPtr hmod, 
            uint dwThreadId);

        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "UnhookWindowsHookEx")]
        [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
        public static extern bool UnhookWindowsHookEx(int hhk);


        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "CallNextHookEx")]
        [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.SysInt)]
        public static extern IntPtr CallNextHookEx(
            [System.Runtime.InteropServices.InAttribute()] System.IntPtr hhk, 
            int nCode,
            int wParam,
            [System.Runtime.InteropServices.InAttribute()] System.IntPtr lParam);
    }
}


猜你喜欢

转载自blog.csdn.net/Zhangchen9091/article/details/44571597