且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我如何关闭窗体,当用户点击窗体的窗外?

更新时间:2023-12-01 23:46:34

与感谢的 href=\"http://***.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);
}