WPF. Catch last window click anywhere

一世执手 提交于 2019-11-29 04:34:42
Ray Burns

If you only care to capture mouse clicks anywhere in a given Window, simply subscribing to the MouseDown or PreviewMouseDown at the window level does the trick.

If you really want it to be global to the application (and not just to the window), you should subscribe to the InputManager.PreProcessInput or InputManager.PostProcessInput event and watch for mouse events:

public MyClickManagerClass()
{
  InputManager.Current.PreProcessInput += (sender, e) =>
  {
    if(e.StagingItem.Input is MouseButtonEventArgs)
      GlobalClickEventHandler(sender,
        (MouseButtonEventArgs)e.StagingItem.Input);
  }
}

Note that "sender" will always be the InputManager but you can map coordinates to other controls with MouseEventArgs.GetPosition(visual).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!