//注册热键的API
[DllImport("user32")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint control, Keys vk);
//解除注册热键的API
[DllImport("user32")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
private void Form1_Load(object sender, System.EventArgs e)
{
RegisterHotKey(this.Handle,11, 2, Keys.Q); //注册热键
RegisterHotKey(this.Handle, 22, 2, Keys.A);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
UnregisterHotKey(this.Handle, 11); // 撤消注册热键
UnregisterHotKey(this.Handle, 22);
}
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case 0x0312: //热键消息
if (m.WParam.ToString() == "11") // 按下CTRL+Q 隐藏窗口
{
this.Hide();
}
else if (m.WParam.ToString() == "22") //按下CTRL+A显示窗口
{
this.Visible = true;
}
break;
}
base.WndProc(ref m);
}