winform 程序经常需要注册一些快捷方式,比如窗体隐藏时,按指定的快捷键显示出来
1.引用系统类库System.Runtime.InteropServices
2.注册系统底层方法
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
[DllImport("user32.dll")]
private static extern int DefWindowProc(IntPtr hwnd, int wMsg, int wParam, int lParam);
private const int WM_SETHOTKEY = 0x32;
private const int WM_SHOWWINDOW = 0x18;
private const int HK_SHIFTB = 0x142;
3.调用并注册
//设置热键
SendMessage(this.Handle, WM_SETHOTKEY, HK_SHIFTB, 0);
//执行热键
DefWindowProc(this.Handle, WM_SHOWWINDOW, 0, 0);
当前注册的为按:Shift+b显示窗体
Shift+a 则需要修改成:HK_SHIFTB = 0x141
Shift+c 则需要修改成:HK_SHIFTB = 0x143Shift+f 则需要修改成:HK_SHIFTB = 0x146