鼠标挂钩断开连接

时间:2016-10-26 09:36:47

标签: c# .net mouse

我正在尝试实现一个颜色选择器,它从屏幕中的任何位置获取颜色。 要做到这一点,我打算使用全局鼠标钩子来监听WM_MOUSEMOVE,以便在鼠标移动时更新颜色并听取鼠标点击确认(WM_LBUTTONDOWN)或取消(WM_RBUTTONDOWN)操作。

我已经遵循了许多教程中的一个,我想出了这个(在控制台应用程序中,只是为了测试过程是否有效):

static IntPtr hook;
static bool click;
static NativeMethods.LowLevelHookStruct llhs;

static void Main(string[] args)
{
  hook = NativeMethods.SetWindowsHookEx(NativeMethods.WH_MOUSE_LL, MouseHookCallback, (IntPtr)null, 0);
  if (hook != IntPtr.Zero)
  {
    Console.WriteLine("Hook Set");
    while (!Console.KeyAvailable) {
      Console.WriteLine("{0} {1} {2}", hook, llhs.pt.x, llhs.pt.y);

      if(click) Console.WriteLine("click!");
      click = false;

      System.Threading.Thread.Sleep(250); 
    }
  }
}

public static IntPtr MouseHookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
  if (nCode >= 0)
  {
    NativeMethods.LowLevelHookStruct hookStruct = (NativeMethods.LowLevelHookStruct)Marshal.PtrToStructure(lParam, typeof(NativeMethods.LowLevelHookStruct));

    if (NativeMethods.MouseMessages.WM_MOUSEMOVE == (NativeMethods.MouseMessages)wParam)
    {
      llhs = hookStruct;
    }

    if (NativeMethods.MouseMessages.WM_LBUTTONDOWN == (NativeMethods.MouseMessages)wParam)
    {
      click = true;
    }
    else if (NativeMethods.MouseMessages.WM_RBUTTONDOWN == (NativeMethods.MouseMessages)wParam)
    {
    }
  }

  return NativeMethods.CallNextHookEx(hook, nCode, wParam, lParam);
}

NativeMethods只是一个我保留所有DllImport相关内容的类。

一旦我运行控制台应用程序,鼠标光标会卡住几秒钟,而在控制台中我得到了这个 - 即使光标 卡住了

Hook Set
3945554872 0 0
3945554872 0 0
3945554872 0 0
3945554872 0 0
...

在调试中进行,似乎我的钩子永远不会被调用,甚至一次。 知道什么可能是错的吗?

1 个答案:

答案 0 :(得分:0)

关注@Hans Passant评论后,我将测试代码移至WinForms应用程序,并开始回调。

然后,发现回调被垃圾收集只是一个问题,所以我所要做的就是改变

hook = NativeMethods.SetWindowsHookEx(NativeMethods.WH_MOUSE_LL, MouseHookCallback, (IntPtr)null, 0);

private NativeMethods.LowLevelHookProc _hookCallback;

...

_hookCallback = new NativeMethods.LowLevelHookProc(MouseHookCallback);
hook = NativeMethods.SetWindowsHookEx(NativeMethods.WH_MOUSE_LL, _hookCallback, (IntPtr)null, 0);

为了保持对回调的引用,以便它不会被GCed。