WinForm和WPF 屏蔽Alt+F4键

两者都比较容易。当然可以用Win32的钩子来实现,不过我不会。

WinForm:比WPF稍简单些

 

1         private void MyForm_KeyDown(object sender, KeyEventArgs e)
2         {
3             if (e.KeyCode == Keys.F4 && e.Modifiers == Keys.Alt)
4             {
5                 e.Handled = true;
6             }
7         }

 

 

 

WPF: 只要在PreviewKeyDown和PreviesKeyUp中做处理即可,代码如下:

  

 1         private bool AltDown = false;
 2         private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
 3         {
 4 
 5             if (e.SystemKey == Key.LeftAlt || e.SystemKey == Key.RightAlt)
 6             {
 7                 AltDown = true;
 8             }
 9             else if (e.SystemKey == Key.F4 && AltDown)
10             {
11                 e.Handled = true;
12             }
13             //else if (e.SystemKey == …… && AltDown) // 可以在此处添加 自己需要的快捷键。
14             //{
15             //    this.Close();
16             //}
17         }
18 
19         private void Window_PreviewKeyUp(object sender, KeyEventArgs e)
20         {
21             if (e.SystemKey == Key.LeftAlt || e.SystemKey == Key.RightAlt)
22             {
23                 AltDown = false;
24             }
25         }

 

 

posted @ 2011-01-30 11:33  程序员阿呆  阅读(5549)  评论(1编辑  收藏  举报