Is it possible to detect mouse clicks without listening to any mouse events defined in framework controls?
I mean, I don't want to write code like :
control.MouseLeftButtonDown += this.HandleMouseLeftButtonDown;
Yet I want to know if user clicks on the screen or not. Is it possible in C# (WPF or Silverlight)?
You can register a class handler in a static constructor you your main window, for example:
static MainWindow() {
EventManager.RegisterClassHandler(typeof (MainWindow),
Mouse.MouseDownEvent,
new MouseButtonEventHandler(OnGlobaMouseDown));
}
It will be a global handler for all MouseDown events.
You could use the Win32 API and detect the mouse message WM_MOUSE, something like this:
http://support.microsoft.com/kb/318804
or this example, showing use of the global mouse message WM_MOUSE_LL:
This is done by capturing the mouse. Which forces any mouse event to be directed to you, even if it moves outside of the window. Mouse.Capture() method.
如果需要处理所有应用程序的鼠标事件,最好的方法是订阅InputManager事件。
"I mean, I don't want to write code like :
control.MouseLeftButtonDown += this.HandleMouseLeftButtonDown;"
You could always use:
if (Mouse.LeftButton == MouseButtonState.Pressed)
{
...
}
You will need to include this though.
using System.Windows.Input;
This works for me in wpf.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.