winform 添加热键
//如果函数执行成功,返回值不为0。
//如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(
IntPtr hWnd, //要定义热键的窗口的句柄
int id, //定义热键ID(不能与其它ID重复)
KeyModifiers fsModifiers, //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效
Keys vk //定义热键的内容
);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd, //要取消热键的窗口的句柄
int id //要取消热键的ID
);
调用api
//撤销热键
HotKey.UnregisterHotKey(Handle, 10091);
HotKey.UnregisterHotKey(Handle, 10092);
//注册热键
HotKey.RegisterHotKey(Handle, 10091, HotKey.KeyModifiers.Ctrl, Keys.Z);//撤销快捷键
HotKey.RegisterHotKey(Handle, 10092, HotKey.KeyModifiers.Ctrl, Keys.Y);//前进快捷键
调用
protected override void WndProc(ref Message m)
{
//按快捷键
switch (m.Msg)
{
case 0x0312:
switch (m.WParam.ToInt32())
{
case 10091:
//ctrl+Z
break;
case 10092:
//ctrl+Y
break;
}
break;
}
base.WndProc(ref m);
}