簡體   English   中英

如何使用Alt + F4鍵關閉子窗口?

[英]How to Close Child Window Using Alt + F4 Key?

Alt + F4是關閉表單的快捷方式。 當我在MDI環境中使用此快捷方式時,該應用程序已關閉,因此該快捷方式顯然適用於“容器”,而不適用於“子窗體”。

捕獲此事件並關閉活動子而不是容器的最佳實踐是什么

我讀到有關在MDI激活時將Alt + F4注冊為熱鍵的信息。 當MDI停用時,取消注冊熱鍵。 因此,熱鍵不會影響其他窗口。

有人可以告訴您有關注冊Alt + F4或其他更好內容的信息

您可以在winform中更改void Dispose(bool disposing)方法來關閉子窗體,如下所示:

protected override void Dispose(bool disposing)
{
    if (/* you need to close a child form */)
    {
        // close the child form, maybe by calling its Dispose method
    }
    else
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }
}

編輯:正如我的評論者所說,您無需修改​​覆蓋的Dispose方法,而應覆蓋OnFormClosing方法,如下所示:

protected override void OnFormClosing(FormClosingEventArgs e)
{
    if (/* you need to close the child form */)
    {
        e.Cancel = true;
        // close the child form, maybe with childForm.Close();
    }
    else
        base.OnFormClosing(e);
}

由於尚無人回答這個問題,因此可以通過以下兩個步驟來完成:

步驟1:使用這種簡單的邏輯使用Alt + F4觸發MDI子窗體的關閉。

private void child_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Alt && e.KeyCode == Keys.F4) 
    {
        this.Close();
    }
}

第2步:還使用此技巧來禁用影響父MDI表單的Alt + F4效果。

private void parent_FormClosing(object sender, FormClosingEventArgs e)
{
    // note the use of logical OR instead of logical AND here
    if (Control.ModifierKeys == Keys.Alt || Control.ModifierKeys == Keys.F4) 
    { 
        e.Cancel = true;
        return;
    }    
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM