我需要获取屏幕上的鼠标位置,而不是我的应用程序内部的位置。
我已经在这里使用了全局鼠标和键盘钩子(点击链接)
在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的替代方案或者我错过了什么?