我如何关闭窗体,当用户点击窗体的窗外? [英] How do I close a form when a user clicks outside the form's window?

查看:138
本文介绍了我如何关闭窗体,当用户点击窗体的窗外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想如果用户点击外,随时随地关闭System.Windows.Forms.Form中。我已经使用IMessageFilter试过,但即使如此,没有任何消息传递给PreFilterMessage。我如何收到表格的窗口之外的点击?

I want to close a System.Windows.Forms.Form if the user clicks anywhere outside it. I've tried using IMessageFilter, but even then none of the messages are passed to PreFilterMessage. How do I receive clicks outside a form's window?

推荐答案

与感谢的 href=\"http://stackoverflow.com/questions/298626/what-do-these-wndproc-codes-mean\">这个问题,我发现这个解决方案,它允许我使用ShowDialog的:

With thanks to p-daddy in this question, I've found this solution which allows me to use ShowDialog:

protected override void OnShown(EventArgs e)
{
    base.OnShown(e);
    this.Capture = true;
}

protected override void OnCaptureChanged(EventArgs e)
{
    if (!this.Capture)
    {
        if (!this.RectangleToScreen(this.DisplayRectangle).Contains(Cursor.Position))
        {
            this.Close();
        }
        else
        {
            this.Capture = true;
        }
    }

    base.OnCaptureChanged(e);
}

这篇关于我如何关闭窗体,当用户点击窗体的窗外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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