C# 如何使用Alt+F4键关闭子窗口?
Alt+F4是关闭窗体的快捷方式。 在MDI环境中使用此快捷方式时,应用程序将关闭,因此 显然,快捷方式适用于“容器”,而不适用于 “儿童形式” 捕获此事件并关闭活动服务器的最佳做法是什么 子容器而不是容器 我读到有关在MDI激活时将Alt+F4注册为热键的内容。 当MDI停用时,取消注册热键。 因此,热键不会影响其他窗口 有人可以告诉你如何注册Alt+F4或其他更好的方法你可以更改winform中的void Disposebool disposing方法来关闭子窗体,如下所示:C# 如何使用Alt+F4键关闭子窗口?,c#,winforms,mdichild,C#,Winforms,Mdichild,Alt+F4是关闭窗体的快捷方式。 在MDI环境中使用此快捷方式时,应用程序将关闭,因此 显然,快捷方式适用于“容器”,而不适用于 “儿童形式” 捕获此事件并关闭活动服务器的最佳做法是什么 子容器而不是容器 我读到有关在MDI激活时将Alt+F4注册为热键的内容。 当MDI停用时,取消注册热键。 因此,热键不会影响其他窗口 有人可以告诉你如何注册Alt+F4或其他更好的方法你可以更改winform中的void Disposebool disposing方法来关闭子窗体,如下所示: protect
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子窗体的关闭 第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;
}
}
不要这样做。不要弄乱我的操作系统键盘快捷键。CTRL+F4是您想要执行的操作的公认快捷方式。同意DMOS。如果希望窗口使用ALT+F4关闭,请不要将其设为MDI子级。改为创建顶级窗口。您的想法是正确的,但使用Dispose方法是不正确的。替代OnFormClosing,并将e.Cancel设置为true。
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;
}
}