兩者都比較容易。當然可以用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 }
轉載于:https://www.cnblogs.com/icewwn/archive/2011/01/30/1947790.html