我如何禁用ALT+F4关闭,但按H将关闭程序
本文关键字:但按 关闭程序 关闭 何禁用 ALT+F4 | 更新日期: 2023-09-27 17:50:29
例如:
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.H)
{
this.Close();
return true;
}
else
{
return base.ProcessDialogKey(keyData);
}
}
当我禁用Alt+f4工作时,我不能使用e.Cancel = true;
,因为它禁用按H键关闭程序。
Try This:
解决方案1:
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.H)
{
this.Close();
return true;
}
else if (keyData == Keys.Alt | keyData == Keys.F4)
{
return base.ProcessDialogKey(keyData);
}
return true;
}
解决方案2:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Alt | e.KeyCode==Keys.F4)
{
e.Handled = true;
}
else if (e.KeyCode == Keys.H)
{
this.Close();
}
}
试试这个:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = (e.CloseReason == CloseReason.UserClosing);
}
这样,只有当用户试图通过用户界面关闭表单时,您才能取消关闭。
检查这个:如何禁用Alt + F4关闭形式?
如何设置一些全局变量关闭?
bool closeForm = false;
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.H)
{
closeForm = true;
this.Close();
return true;
}
else
{
return base.ProcessDialogKey(keyData);
}
}
在FormClosing事件中,只需检查变量,就像这样
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (!closeForm)
e.Cancel = true;
}