【问题标题】:Global Mouse Hook in WPFWPF 中的全局鼠标钩子
【发布时间】:2014-11-25 18:23:40
【问题描述】:

我需要在我的应用程序中在屏幕上获取鼠标位置。

我用过全局键鼠钩子here

在winforms下可以正常工作,但在wpf下不行。

我正在使用版本 1 的代码并使用以下代码

var activity = new UserActivityHook();
activity.OnMouseActivity += activity_OnMouseActivity;

但它没有工作,而是给了我以下错误:

附加信息:找不到指定的模块

下面的代码

public void Start(bool InstallMouseHook, bool InstallKeyboardHook)
{
    // install Mouse hook only if it is not installed and must be installed
    if (hMouseHook == 0 && InstallMouseHook)
    {
        // Create an instance of HookProc.
        MouseHookProcedure = new HookProc(MouseHookProc);

        //install hook
        hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProcedure,
            Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
        //If SetWindowsHookEx fails.
        if (hMouseHook == 0)
        {
            //Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set. 
            int errorCode = Marshal.GetLastWin32Error();
            //do cleanup
            Stop(true, false, false);
            //Initializes and throws a new instance of the Win32Exception class with the specified error. 
            throw new Win32Exception(errorCode);
        }
    }
}

WPF 有什么替代方案吗?还是我遗漏了什么?

【问题讨论】:

  • @SonerGönül 我只在 wpf 中收到此错误..它在 winforms 中工作正常..所以这里的链接对我的问题没有帮助。
  • 为了详细说明 Soner Gonul 基本上没有提供信息的响应,您似乎没有初始化 UserActivityHook。
  • 很多事情都错了——所以,我们在这里解决错误......
  • 您只想获取mouse position

标签: c# .net wpf hook


【解决方案1】:

dotctor 的回答给出了鼠标位置,但没有给出我正在寻找的事件。

所以我自己想通了。以下是我的发现,以供将来参考。

我们需要改变:

//install hook
hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProcedure,
             Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);

到以下:

// Install hook
hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProcedure, IntPtr.Zero, 0);

或者你也可以从here下载编辑好的cs文件,然后我们就可以使用这个事件了

activity.OnMouseActivity += activity_OnMouseActivity;

在此我们可以使用e.Xe.Y来获取鼠标位置。

【讨论】:

  • 链接死了,真可惜。我们能做些什么来解决这个问题? (Wayback 机器不工作)。
  • @JG3 抱歉。我没有文件了。无论如何。您只需要按照上面指定的方式编辑代码即可。
【解决方案2】:
  1. 寻求向导的帮助! (做简单的方法)
    添加对System.Windows.Forms 的引用并使用Control.MousePosition

  2. 成为炼金术士! (组合您的项目)
    结合Visual.PointToScreenMouse.GetPositionApplication.Current.MainWindow

  3. 使用能量脉轮 (win32)

     [DllImport("user32.dll")]
     [return: MarshalAs(UnmanagedType.Bool)]
     internal static extern bool GetCursorPos(ref Win32Point pt);
    
     [StructLayout(LayoutKind.Sequential)]
     internal struct Win32Point
     {
       public Int32 X;
       public Int32 Y;
     };
    
     public static Point GetMousePosition()
     {
        var w32Mouse = new Win32Point();
        GetCursorPos(ref w32Mouse);
    
        return new Point(w32Mouse.X, w32Mouse.Y);
     }
    

【讨论】:

  • 是的,我可以得到鼠标指针..使用你给的第三个选项..但我真正想要的是在鼠标移动时报告鼠标位置..我已经展示了一种全局鼠标移动事件在问题中...无论如何感谢您的答复,这是我能得到的最接近的答复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-21
相关资源
最近更新 更多