在我的应用程序中,要求用户能够从文件拖放ListView
到 Windows 资源管理器。该文件位于服务器上,因此我需要下载它们以放置哪个用户点。为此,我决定设置WH_MOUSE_LL
用于跟踪全局鼠标事件的钩子,以获取用户放置文件的文件夹的句柄,然后使用它来获取加载文件的绝对路径。这是带有 HOOK 功能的 id DLL:
[StructLayout(LayoutKind.Sequential)]
public class MouseHookStruct
{
public POINT pt;
public int hwnd;
public int wHitTestCode;
public int dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
private struct MSLLHOOKSTRUCT
{
public POINT pt;
public uint mouseData;
public uint flags;
public uint time;
public IntPtr dwExtraInfo;
}
private enum MouseMessages
{
WM_LBUTTONDOWN = 0x0201,
WM_LBUTTONUP = 0x0202,
WM_MOUSEMOVE = 0x0200,
WM_MOUSEWHEEL = 0x020A,
WM_RBUTTONDOWN = 0x0204,
WM_RBUTTONUP = 0x0205
}
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int CallNextHookEx(int idHook, int nCode,
IntPtr wParam, IntPtr lParam);
public int LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam)
{
//Marshall the data from the callback.
//MouseHookStruct MyMouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));
MSLLHOOKSTRUCT MyMouseHookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
if (nCode >= 0 &&
MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam)
{
String strCaption = "x = " +
MyMouseHookStruct.pt.x.ToString("d") +
" y = " +
MyMouseHookStruct.pt.y.ToString("d");
//You must get the active form because it is a static function.
Console.WriteLine(strCaption);
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
}
}
然后我注册它SetWindowsHookEx
:
[DllImport("kernel32.dll")]
public static extern IntPtr LoadLibrary(string dllToLoad);
IntPtr pDll = LoadLibrary(@"C:\Users\вова\Documents\visual studio 2010\Projects\MouseHook\MouseHook\bin\Debug\MouseHook.dll");
MouseHookProcedure = new HookProc(msHookObj.LowLevelMouseProc);
hHook = SetWindowsHookEx(WH_MOUSE,
MouseHookProcedure,
pDll,
0);
结果我的应用程序不跟踪全局事件。请帮忙,我做错了什么?
谢谢