題目全部在標題中。對不起,如果它是重複的,我真的找不到任何東西。如何在WPF窗口外捕獲MouseUp事件?
回答
嗯,非常感謝Anatoliy Nikolaev指導我正確的方向!那article幫了我很大的忙,這個answer對於從文章工作中做出代碼很有用。這是我現在有:
internal static class MouseHook
{
private delegate int HookProc(int nCode, int wParam, IntPtr lParam);
private static int _mouseHookHandle;
private static HookProc _mouseDelegate;
private static event MouseUpEventHandler MouseUp;
public static event MouseUpEventHandler OnMouseUp
{
add
{
Subscribe();
MouseUp += value;
}
remove
{
MouseUp -= value;
Unsubscribe();
}
}
private static void Unsubscribe()
{
if (_mouseHookHandle != 0)
{
int result = UnhookWindowsHookEx(_mouseHookHandle);
_mouseHookHandle = 0;
_mouseDelegate = null;
if (result == 0)
{
int errorCode = Marshal.GetLastWin32Error();
throw new Win32Exception(errorCode);
}
}
}
private static void Subscribe()
{
if (_mouseHookHandle == 0)
{
_mouseDelegate = MouseHookProc;
_mouseHookHandle = SetWindowsHookEx(WH_MOUSE_LL,
_mouseDelegate,
GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName),
0);
if (_mouseHookHandle == 0)
{
int errorCode = Marshal.GetLastWin32Error();
throw new Win32Exception(errorCode);
}
}
}
private static int MouseHookProc(int nCode, int wParam, IntPtr lParam)
{
if (nCode >= 0)
{
MSLLHOOKSTRUCT mouseHookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
if (wParam == WM_LBUTTONUP)
{
if (MouseUp != null)
{
MouseUp.Invoke(null, new Point(mouseHookStruct.pt.x, mouseHookStruct.pt.y));
}
}
}
return CallNextHookEx(_mouseHookHandle, nCode, wParam, lParam);
}
private const int WH_MOUSE_LL = 14;
private const int WM_LBUTTONUP = 0x0202;
[StructLayout(LayoutKind.Sequential)]
private struct POINT
{
public int x;
public int y;
}
[StructLayout(LayoutKind.Sequential)]
private struct MSLLHOOKSTRUCT
{
public POINT pt;
public uint mouseData;
public uint flags;
public uint time;
public IntPtr dwExtraInfo;
}
[DllImport("user32.dll", CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall, SetLastError = true)]
private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, int dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall, SetLastError = true)]
private static extern int UnhookWindowsHookEx(int idHook);
[DllImport("user32.dll", CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall)]
private static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);
[DllImport("kernel32.dll")]
public static extern IntPtr GetModuleHandle(string name);
}
public delegate void MouseUpEventHandler(object sender, Point p);
並使用它很簡單:
MouseHook.OnMouseUp += MouseHookMouseUp;
我實際上沒有檢查如果這工作,但只是由它的外觀+1。 我會告訴你,這正是我一直在尋找的。 –
這個工程很好,你知道一個方法來取消到MouseHookProc後的消息處理,可以說我有一個winform控件 ,我不想恢復從它的單擊事件,WM_LBUTTONDOWN我趕上消息處理,並在某些條件我希望它停在那裏,並且控件不會收到鼠標輸入。 –
@eran otzap如果我理解正確,如果您使用我的答案中的代碼,則需要退訂:'MouseHook.OnMouseUp - = MouseHookMouseUp'。爲了停止控制接收鼠標輸入,我想,你需要使用一個更加「傳統的」事件處理程序,比如'control.MouseUp + = control_MouseUp'和一個方法應該是這樣的:'void control_MouseUp(object sender,MouseButtonEventArgs e) { e.Handled = true; }' – lena
- 1. 使用jquery捕獲窗口滾動條的MouseUp事件
- 2. 如何在mousedown元素之外捕獲mouseup事件?
- 3. 如何捕獲發生在(WPF)窗口之外的鼠標事件?
- 4. 無法在IE7中捕獲mouseup事件
- 5. 在NSDocument中捕獲窗口事件
- 6. 在父窗口中捕獲WM_MOUSEMOVE事件
- 7. WPF事件捕獲。控制觸發mouseup但不mousedown
- 8. this.dragmove()干擾mouseup事件wpf
- 9. 如何捕捉窗口關閉事件?
- 10. 捕獲窗口關閉事件
- 11. 捕獲事件時,瀏覽器窗口
- 12. 捕獲鼠標鬆開事件/窗口
- 13. 如何在autoit v3中捕獲窗口焦點事件
- 14. 如何在JFrame中捕獲JDialogs窗口關閉事件
- 15. 如何使用javascript在彈出窗口中捕獲事件?
- 16. 如何捕獲NSButton上的鼠標事件(mouseDown和mouseUp)?
- 17. 即使在WPF的外部控件中也觸發MouseUp事件
- 18. 如何捕獲附件響應窗口的onload事件?
- 19. 等待窗口關閉並捕獲窗口事件
- 20. 在wpf DataGrid中使用MouseUp事件
- 21. 如何捕獲從新的Popup IE窗口觸發的事件
- 22. 如何捕獲從彈出IE窗口觸發的事件
- 23. 如何捕獲窗口最小化事件?
- 24. 如何在wpf中捕獲特定控件的mousedown事件並在窗口的任何位置釋放?
- 25. 在wpf網頁瀏覽器中捕獲和取消新窗口打開事件
- 26. 在IE 7/8窗口外出現`mouseup`文件夾的問題
- 27. 在WPF中的所有窗口上捕獲組合鍵而不在每個窗口上添加事件
- 28. 觸發事件從另一個WPF窗口WPF窗口
- 29. WPF - 如何停止TabControl在MouseUp事件中更改索引?
- 30. 在DataGrid中捕獲RowEdited事件WPF
看到這個['link'](http://www.codeproject.com/Questions/362352/Csharp -Mouse-Event-outside-the-Form),它指的是'WinForms',但對於WPF將是相同的。也看到這個問題:http://stackoverflow.com/questions/13732070/how-can-i-capture-mouse-events-that-occur-outside-of-a-wpf-window –