我已经阅读了一些关于禁用Alt+的文章F4,到目前为止,它已经部分运行了.
目前,我在我的主要表单上有这个,并且它只是在我需要它时才允许关闭表单.
private void Alerter_FormClosing(object sender, FormClosingEventArgs e)
{
if (_altF4Pressed)
{
if (e.CloseReason == CloseReason.UserClosing)
e.Cancel = true;
_altF4Pressed = false;
}
}
private void Alerter_KeyDown(object sender, KeyEventArgs e)
{
if (e.Alt && e.KeyCode == Keys.F4)
_altF4Pressed = true;
}
Run Code Online (Sandbox Code Playgroud)
但问题是,当我显示一个对话框时,这不起作用.表单仍然在Alt+上关闭F4,并且必须不会发生这种情况,因为它为用户提供了绕过进程和过程的方法.
目前,我的警报表格被称为使用;
public static DialogResult show(string param)
{
thisParam = param;
using (Alerter alert = new Alerter())
{
if (alert.ShowDialog() == DialogResult.OK) { return DialogResult.OK; }
else { return DialogResult.Cancel; }
}
}
Run Code Online (Sandbox Code Playgroud)
我的主要形式用它调用它;
Alerter.show(rxLine);
Run Code Online (Sandbox Code Playgroud)
我怎么能阻止新表格被关闭,除非我明确告诉它?
从如何阻止用户关闭我的C#应用程序?,你可以这样做:
protected override CreateParams CreateParams
{
get
{
const int CS_NOCLOSE = 0x200;
CreateParams cp = base.CreateParams;
cp.ClassStyle |= CS_NOCLOSE;
return cp;
}
}
Run Code Online (Sandbox Code Playgroud)
无需在FormClosing事件中跟踪键或取消关闭.此外,角落中的关闭按钮将被禁用,但您仍然可以关闭该表单this.Close();
| 归档时间: |
|
| 查看次数: |
928 次 |
| 最近记录: |