Possible Duplicate:
Drag Drop from .NET application to Explorer用于拖动跟踪全球鼠标事件/放处理
在我的应用它要求用户可以在具有ListView
从Windows资源管理器从文件拖放&下降的能力。这些文件位于服务器上,因此我需要下载它们以放置哪个用户点。为此,我决定设置钩子WH_MOUSE_LL
来跟踪全局鼠标事件,以获取用户放置文件的文件夹句柄,然后使用它获取加载文件的绝对路径。这里是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);
在结果我的应用程序不跟踪的全球性事件。请帮忙,我做错了什么?
感谢
我只是改变一列代码 HHOOK = SetWindowsHookEx函数(7,MouseHookProcedure,PDLL,0); 现在它跟踪窗口中的所有鼠标事件,但有时会给我错误:CallBackCollected委托。这是什么意思? 而最后一个问题: 我怎样才能得到知道它的处理文件夹的完整路径? 谢谢 –
如果您实际使用WH_MOUSE_LL而不是WH_MOUSE,则可能会出现此情况。但是* *要利用Windows中内置的拖放支持。 –