注册热键
SetHotKey(false, false, true, false, Keys.Q);
#region 快捷键
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(
IntPtr hWnd, // window 句柄
int id, // 标识符
KeyModifiers fsModifiers,
Keys vk
);
/// <summary>
/// 卸载快捷键
/// </summary>
/// <param name="hWnd"></param>
/// <param name="id"></param>
/// <returns></returns>
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd, // window 句柄
int id // 标识符
);
[Flags()]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}
/// <summary>
/// 触发热键时要执行的命令
/// </summary>
private void ProcessHotkey()
{
if (Status.IsHide == false)
{
Status.IsHide = true;
//MsgBox.Alert("隐藏");
this.Hide();
}
else
{
Status.IsHide = false;
this.Show();
//MsgBox.Alert("显示");
}
}
protected override void WndProc(ref Message m)//监视Windows消息
{
const int WM_HOTKEY = 0x0312;//按快捷键
switch (m.Msg)
{
case WM_HOTKEY:
ProcessHotkey();//调用主处理程序
break;
}
base.WndProc(ref m);
}
private bool key_Ctrl = false;
private bool key_Shift = false;
private bool key_Alt = false;
private bool key_Windows = false;
private Keys key_other;
/// <summary>
/// 注册快捷键
/// </summary>
/// <param name="bCtrl"></param>
/// <param name="bShift"></param>
/// <param name="bAlt"></param>
/// <param name="bWindows"></param>
/// <param name="nowKey"></param>
private void SetHotKey(bool bCtrl, bool bShift, bool bAlt, bool
bWindows, Keys nowKey)
{
try
{
this.key_Alt = bAlt;
this.key_Ctrl = bCtrl;
this.key_Shift = bShift;
this.key_Windows = bWindows;
this.key_other = nowKey;
KeyModifiers modifier = KeyModifiers.None;
if (this.key_Ctrl)
modifier |= KeyModifiers.Control;
if (this.key_Alt)
modifier |= KeyModifiers.Alt;
if (this.key_Shift)
modifier |= KeyModifiers.Shift;
if (this.key_Windows)
modifier |= KeyModifiers.Windows;
RegisterHotKey(Handle, 100, modifier, nowKey);
}
catch
{
MsgBox.AlertExclamation("快捷键定义错误!/n可能有别的程序正在使用。");
}
}
#endregion