WPF。捕捉最后一个窗口点击任何地方 [英] WPF. Catch last window click anywhere

查看:245
本文介绍了WPF。捕捉最后一个窗口点击任何地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一个类可以捕获应用程序中的最后一次点击?像

Is there anyway that a class can catch the last click in the application? Something like

public class MyClickManagerClass
{
    public MyClickManagerClass()
    {
        // subscribe to a global click event
    }

    private void GlobalClickEventHandler(object sender, EventArgs e)
    {
        // do something with the click here
    }
}

感谢您的时间!

推荐答案

如果你只关心在给定的窗口中捕获鼠标点击, code> MouseDown 或 PreviewMouseDown 在窗口级别执行。

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.

如果你真的希望它是全局的(而不仅仅是窗口),你应该订阅 InputManager.PreProcessInput InputManager .PostProcessInput 事件并监视鼠标事件:

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);
  }
}

请注意,sender始终是InputManager,您可以将坐标映射到其他控件与 MouseEventArgs.GetPosition(visual)

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

这篇关于WPF。捕捉最后一个窗口点击任何地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆