簡體   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