我正在编写一个 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
.