整理的c#winform窗體熱鍵隱藏代碼及存在的問題
最近要做一個winform窗體熱鍵隱藏的程序,在網上搜了很多這樣的代碼,自己整理了一個比較簡潔的:
1
using System.Runtime.InteropServices;2

3
Boolean hide = true;4

5
[DllImport("user32.dll", SetLastError = true)] //導入注冊函數6
public static extern bool RegisterHotKey(7
IntPtr hWnd, // handle to window8
int id, // hot key identifier9
KeyModifiers fsModifiers, // key-modifier options10
Keys vk // virtual-key code11
);12

13
[DllImport("user32.dll", SetLastError = true)]14
public static extern bool UnregisterHotKey(15
IntPtr hWnd, // handle to window16
int id // hot key identifier17
);18

19

20
[Flags()]21
public enum KeyModifiers22

{23
None = 0,24
Alt = 1,25
Control = 2,26
Shift = 4,27
Windows = 828
}29
private void Form1_Load(object sender, EventArgs e)30

{31
RegisterHotKey(Handle, 100, 0, Keys.F10);//窗體載入時注冊熱鍵32
}33

34
protected override void WndProc(ref Message m)//監視Windows消息35

{36
const int WM_HOTKEY = 0x0312;//按快捷鍵37
switch (m.Msg)38

{39
case WM_HOTKEY:40
this.menuItem10.PerformClick(); //調用主處理程序41
break;42
}43
base.WndProc(ref m);44
}45

46
private void menuItem10_Click(object sender, EventArgs e)47

{48
49
if (hide == true)50

{51
this.ShowInTaskbar = false;52
this.WindowState = System.Windows.Forms.FormWindowState.Minimized;53
hide = false;54
}55
else56

{57
this.ShowInTaskbar = true;58
this.WindowState = System.Windows.Forms.FormWindowState.Normal;59
hide = true;60
61
}62
}以上代碼按下F10鍵能夠將窗體隱藏,但隱藏后再無法調出,按下alt+tab能調出,可是再按F10鍵就再也無法隱藏了,不知道問題在哪里?哪位大俠指點一下。
浙公網安備 33010602011771號