FROM http://www.piaoyi.org/c-sharp/C-RegisterHotKey.html
//注册热键的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)
{
//注册热键 (窗体句柄,热键ID,辅助键,实键)
//辅助键说明: None = 0, Alt = 1, crtl= 2, Shift = 4, Windows = 8
//如果有多个辅助键则,例如 alt+crtl是3 直接相加就可以了
RegisterHotKey(this.Handle, 123, 2, Keys.Q);
RegisterHotKey(this.Handle, 456, 2, Keys.W);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
UnregisterHotKey(this.Handle, 123);
UnregisterHotKey(this.Handle, 456);
}
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case 0x0312: //这个是window消息定义的注册的热键消息
if (m.WParam.ToString() == "123") // 按下CTRL+Q隐藏
{
this.Hide();
}
else if (m.WParam.ToString() == "456") // 按下CTRL+W显示
{
this.Visible = true;
}
break;
}
base.WndProc(ref m);
}
0 意見:
張貼留言