繁体   English   中英

关闭MDI表单和子级时如何正确提示用户?

[英]How to properly prompt the user when closing an MDI form and children?

我正在编写一个IRC客户端,其中有一个带有服务器和通道窗口的MDI父级。 当您关闭“服务器”窗口时,它会提示用户,如果他们要关闭该窗口,则会关闭与服务器的连接等。

我希望关闭MDI父级时只有一个提示,而不是每个服务器都有一个提示。 问题是,当用户尝试关闭父窗体时,子窗体的OnFormClosing在父窗体之前被调用。

另一种选择是使用MDI的DefWndProc在子窗口之前捕获MDI,然后在子窗口中“杀死”子窗口。

''' <remarks>Intercept the user clicking on the CLOSE button (or ALT+F4'ing) before the closing starts.</remarks>
Protected Overrides Sub DefWndProc(ByRef m As System.Windows.Forms.Message)

    Try
        Const SC_CLOSE = &HF060 'http://msdn.microsoft.com/en-us/library/ms646360%28v=vs.85%29.aspx

        If (m.Msg = WndMsg.WM_SYSCOMMAND) _
        AndAlso (m.WParam.ToInt32 = SC_CLOSE) Then

            If (Not Me.ExitApplicationPrompt()) Then ' Do your "close child forms" here
                m.Msg = 0 'Cancel the CLOSE command
            End If

        End If

    Catch ex As Exception
        My.ExceptionHandler.HandleClientError(ex)
    End Try

    MyBase.DefWndProc(m)

End Sub

将MDI子窗体的FormClosing事件修改为以下内容:

private void MyChildForm_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason == CloseReason.MdiFormClosing)
    {
        e.Cancel = true;
        return;
    }

    // Child window closing code goes here
}

然后将全局关闭提示/逻辑放在MDI父窗体的FormClosing事件中。 提示:与窗口类型测试结合使用this.MdiChildren ,即childForm is IServerForm

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM