忽略WPF应用程序中的Alt + F4

kar*_*tal 16 c# wpf

如何在WPF应用程序中忽略Alt+ F4

小智 36

将其添加到UIElement/FramworkElement您不希望Alt + F4工作的地方.

wnd.KeyDown += new KeyEventHandler(wnd_KeyDown);

void wnd_KeyDown(object sender, KeyEventArgs e)
{
    if ( e.Key == Key.System && e.SystemKey == Key.F4)
    {
        e.Handled = true;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • +1是最简单的解决方案,不会阻止它关闭其他方式 (3认同)

Svi*_*ack 14

您可以启用OnClosing事件TFormcea.Cancel = true;在cea CancelEventArgs来自OnClosing参数时设置.

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.onclosing.aspx

C#

private void Form1_Closing(Object sender, CancelEventArgs e) {
   e.Cancel = true;
}
Run Code Online (Sandbox Code Playgroud)

C++

void Form1_Cancel( Object^ /*sender*/, CancelEventArgs^ e )
{
   e->Cancel = true;
}
Run Code Online (Sandbox Code Playgroud)

VB.NET

Private Sub Form1_Closing(sender As Object, e As _
   System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    e.Cancel = True
End Sub 'Form1_Closing
Run Code Online (Sandbox Code Playgroud)

  • Anant Anand的答案应该是接受的答案,因为这样可以防止它关闭其他方式,而不仅仅是Alt + F4 (2认同)
  • Anant Anand的答案更好. (2认同)
  • OP正在询问WPF,这是WinForms (2认同)

小智 8

只需在View(Xaml)文件中使用这样的输入绑定:

<Window.InputBindings>
    <KeyBinding Modifiers="Alt" Key="F4" Command="{Binding Path=ToDelegateCommandThatExecuteNothing}" />
</Window.InputBindings>
Run Code Online (Sandbox Code Playgroud)