サーバーとチャネル ウィンドウを持つ MDI 親がある IRC クライアントを作成しています。サーバーウィンドウを閉じると、ユーザーにプロンプトが表示され、ユーザーがウィンドウを閉じたい場合は、サーバーへの接続が閉じられます。
サーバーごとにプロンプトを表示するのではなく、MDI の親が閉じられたときにプロンプトを 1 つだけ表示したいと考えています。問題は、ユーザーが親を閉じようとすると、子フォームの 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
。