参考链接:https://www.jb51.net/article/62043.htm#:~:text=%E6%9C%AC%E6%96%87%E7%BB%99%E5%A4%A7%E5%AE%B6%E5%88%86%E4%BA%AB%E7%9A%84%E6%98%AF%E4%BD%BF%E7%94%A8C%23%E5%AE%9E%E7%8E%B0%E9%BC%A0%E6%A0%87%E9%92%A9%E5%AD%90%E5%8A%9F%E8%83%BD%EF%BC%8C%E7%A8%8B%E5%BA%8F%E5%B7%B2%E8%83%BD%E8%8E%B7%E5%8F%96%E9%BC%A0%E6%A0%87%E5%9D%90%E6%A0%87%EF%BC%8C%E5%85%B6%E4%BB%96%E5%B0%B1%E6%B2%A1%E5%88%AB%E7%9A%84%E5%8A%9F%E8%83%BD%E4%BA%86%EF%BC%8C%E6%9C%89%E9%9C%80%E8%A6%81%E7%9A%84%E5%B0%8F%E4%BC%99%E4%BC%B4%E5%8F%82%E8%80%83%E4%B8%8B%E5%90%A7%E3%80%82,C%23%E5%AE%9E%E7%8E%B0%E7%9A%84%E9%BC%A0%E6%A0%87%E9%92%A9%E5%AD%90%EF%BC%8C%E5%8F%AF%E4%BB%A5%E8%8E%B7%E5%8F%96%E9%BC%A0%E6%A0%87%E5%9C%A8%E5%B1%8F%E5%B9%95%E4%B8%AD%E7%9A%84%E5%9D%90%E6%A0%87%EF%BC%8C%E8%AE%B0%E5%BE%97%E8%A6%81%E4%BB%A5%E7%AE%A1%E7%90%86%E5%91%98%E6%9D%83%E9%99%90%E8%BF%90%E8%A1%8C%E6%89%8D%E8%A1%8C
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace app01
{
public partial class Form1 : Form
{
public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
//定义钩子句柄
public static int hHook = 0;
//定义钩子类型
public const int WH_MOUSE_LL = 14;
public HookProc MyProcedure;
//安装钩子
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
//卸载钩子
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern bool UnhookWindowsHookEx(int idHook);
//调用下一个钩子
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);
[StructLayout(LayoutKind.Sequential)]
public class POINT
{
public int x;
public int y;
}
[StructLayout(LayoutKind.Sequential)]
public class MouseHookStruct
{
public POINT pt;
public int hwnd;
public int wHitTestCode;
public int dwExtraInfo;
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (hHook == 0)
{
MyProcedure = new HookProc(this.MouseHookProc);
//这里挂节钩子
hHook = SetWindowsHookEx(WH_MOUSE_LL, MyProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
if (hHook == 0)
{
MessageBox.Show("SetWindowsHookEx Failed");
return;
}
button1.Text = "卸载钩子";
}
else
{
bool ret = UnhookWindowsHookEx(hHook);
if (ret == false)
{
MessageBox.Show("UnhookWindowsHookEx Failed");
return;
}
hHook = 0;
button1.Text = "安装钩子";
}
}
public int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
MouseHookStruct MyMouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));
if (nCode < 0)
{
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
else
{
String strCaption = "x = " + MyMouseHookStruct.pt.x.ToString("d") + " y = " + MyMouseHookStruct.pt.y.ToString("d");
this.Text = strCaption;
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
}
}
}