Intercept every mouse click to WPF application

前端 未结 3 1180
梦如初夏
梦如初夏 2020-12-31 13:09

I\'m looking to intercept every mouse click in my WPF application. Seems this should be easy with the command routing mechanism, but sorry I\'m not finding anything.

3条回答
  •  北海茫月
    2020-12-31 13:50

    You can register a class handler:

    public partial class App : Application
        {
            protected override void OnStartup(StartupEventArgs e)
            {
                EventManager.RegisterClassHandler(typeof(Window), Window.PreviewMouseDownEvent, new MouseButtonEventHandler(OnPreviewMouseDown));
    
                base.OnStartup(e);
            }
    
            static void OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
            {
                Trace.WriteLine("Clicked!!");
            }
        }
    

    This will handle any PreviewMouseDown event on any Window created in the application.

提交回复
热议问题