开发者

How to detect mouse clicks without listening to any mouse events defined in framework controls?

Is it possible to detect mouse clicks wit开发者_如何学编程hout 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:

http://www.codeproject.com/KB/cs/globalhook.aspx


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.


If you need to handle mouse events of all your application, the best way is to subscribe to InputManager events.


"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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜