15,473
社区成员




// Make sure we keep a reference to this delegate!
// If not, GC randomly collects it, and a NullReference exception is thrown
_hookCallback = new WinAPI.HookProc(HookCallbackProcedure);
Process curProcess = Process.GetCurrentProcess();
ProcessModule curModule = curProcess.MainModule;
_handleToHook = WinAPI.SetWindowsHookEx(
WH_MouseHook_LL,
_hookCallback,
curModule.BaseAddress,
0);
// Were we able to sucessfully start hook?
if (_handleToHook == 0)
{
throw new InvalidOperationException("设置全局钩子失败" + Marshal.GetLastWin32Error());
}
public partial class Form1 : Form
{
GlobalHook hook = new MouseHook();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
hook.Start();
}
private void button2_Click(object sender, EventArgs e)
{
hook.Stop();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}