本文介绍了WPF在模态窗口外拦截点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以检查用户何时在模态窗口外单击?我想以某种方式规避模态逻辑,因为如果窗口不显示为模态,它不会显示在活动窗口的顶部,并且,现在,这是正确显示它的唯一方法。我没有找到一个正确的方法来做那些(因为停用事件不会再触发..)

Is it possible to check when the user has clicked outside a modal window? I'd like to somehow circumvent the modal logic because if the window isn't displayed as modal, it will not be shown on top of the active window, and, for now, this is the only way to display it correctly. I haven't found a proper way to do just that (since the "deactivate" event will no longer be triggered..)

推荐答案

p>即使它是一个模态窗口(显示 ShowDialog()调用),可以添加一些偶数处理程序到窗口的类,并检查鼠标点击窗口如下:

Even if it's a modal window (displayed with ShowDialog() calls), one can add some even handlers to the window's class and make it check for the mouse clicks outside the window like this:

    private void Window_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if (posX < 0 || posX > this.Width || posY < 0 || posY > this.Height)
            this.Close();
    }

    private void Window_MouseMove(object sender, MouseEventArgs e)
    {
        Point p = e.GetPosition(this);

        posX = p.X; // private double posX is a class member
        posY = p.Y; // private double posY is a class member
    }

    private void Window_Activated(object sender, EventArgs e)
    {
        System.Windows.Input.Mouse.Capture(this, System.Windows.Input.CaptureMode.SubTree);
    }

这对我来说是一个困难的上下文:mingled MFC,WindowsForms猛oth象的应用程序 - 没有互操作,没有其他复杂的东西。希望它帮助其他人面对这种奇怪的行为。

This did the job for me, in a difficult context: mingled MFC, WindowsForms mammoth of an app - no interop, no other complicated stuff. Hope it helps others facing this odd behavior.

这篇关于WPF在模态窗口外拦截点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-29 20:44
查看更多