您的位置:首页 > 其它

WinForm和WPF中注册热键

2016-04-29 11:47 190 查看
由于.Net没有提供专门的类库处理热键,所以需要直接调用windows API来解决。

HotKey为.NET调用Windows API的封装代码,主要是RegisterHotKey和UnregisterHotKey

class HotKey
{
/// <summary>
/// 如果函数执行成功,返回值不为0。
/// 如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。.NET方法:Marshal.GetLastWin32Error()
/// </summary>
/// <param name="hWnd">要定义热键的窗口的句柄</param>
/// <param name="id">定义热键ID(不能与其它ID重复) </param>
/// <param name="fsModifiers">标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效</param>
/// <param name="vk">定义热键的内容,WinForm中可以使用Keys枚举转换,
/// WPF中Key枚举是不正确的,应该使用System.Windows.Forms.Keys枚举,或者自定义正确的枚举或int常量</param>
/// <returns></returns>
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(
IntPtr hWnd,
int id,
KeyModifiers fsModifiers,
int vk
);
/// <summary>
/// 取消注册热键
/// </summary>
/// <param name="hWnd">要取消热键的窗口的句柄</param>
/// <param name="id">要取消热键的ID</param>
/// <returns></returns>
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd,
int id
);
/// <summary>
/// 向全局原子表添加一个字符串,并返回这个字符串的唯一标识符,成功则返回值为新创建的原子ID,失败返回0
/// </summary>
/// <param name="lpString"></param>
/// <returns></returns>
[DllImport("kernel32", SetLastError = true)]
public static extern short GlobalAddAtom(string lpString);
[DllImport("kernel32", SetLastError = true)]
public static extern short GlobalDeleteAtom(short nAtom);
/// <summary>
/// 定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值)
/// </summary>
[ComVisible(true)]
[Flags]
[TypeConverter(typeof(KeysConverter))]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Ctrl = 2,
Shift = 4,
WindowsKey = 8
}
}


  Windows Form中调用代码

private void Form1_Activated(object sender, EventArgs e)
{
HotKey.RegisterHotKey(Handle, 102, HotKey.KeyModifiers.Alt | HotKey.KeyModifiers.Ctrl, Keys.S);
HotKey.RegisterHotKey(Handle, 100, HotKey.KeyModifiers.Shift, Keys.S);

}

private void Form1_Leave(object sender, EventArgs e)
{
HotKey.UnregisterHotKey(Handle, 102);
HotKey.UnregisterHotKey(Handle, 100);
}

///
/// 监视Windows消息
/// 重载WndProc方法,用于实现热键响应
///
///
protected override void WndProc(ref Message m)
{
const int WM_HOTKEY = 0x0312;//如果m.Msg的值为0x0312那么表示用户按下了热键
//按快捷键
switch (m.Msg)
{
case WM_HOTKEY:
switch (m.WParam.ToInt32())
{
case 100:    //按下的是Shift+S
//此处填写快捷键响应代码
MessageBox.Show("按下的是Shift+S");
break;
case 101:    //按下的是Ctrl+B
//此处填写快捷键响应代码
break;
case 102:    //按下的是Ctrl+Alt+S
CaptureImageTool capture = new CaptureImageTool();

if (capture.ShowDialog() == DialogResult.OK)
{
Image image = capture.Image;
pictureBox1.Width = image.Width;
pictureBox1.Height = image.Height;
pictureBox1.Image = image;
}
break;
}
break;
}
base.WndProc(ref m);
}


  WPF中窗口句柄需要借助WindowInteropHelper来得到,处理函数的加入和Winform不同,需要HwndSource来添加处理函数。

void MainWindow_Unloaded(object sender, RoutedEventArgs e)
{
UnRegisterHotKey();
}

void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
RegisterHotKey();
}

private void UnRegisterHotKey()
{
IntPtr handle = new WindowInteropHelper(this).Handle;
int id = 1000;
bool r = HotKey.UnregisterHotKey(handle, id);

HotKey.UnregisterHotKey(handle, 102);
HotKey.UnregisterHotKey(handle, 100);
}

private void RegisterHotKey()
{
IntPtr handle = new WindowInteropHelper(this).Handle;
int id = 1000;
bool r = HotKey.RegisterHotKey(handle, id, HotKey.KeyModifiers.Ctrl, (int)System.Windows.Forms.Keys.F12);

HotKey.RegisterHotKey(handle, 102, HotKey.KeyModifiers.Alt | HotKey.KeyModifiers.Ctrl, (int)Keys.S);
HotKey.RegisterHotKey(handle, 100, HotKey.KeyModifiers.Shift, (int)Keys.S);

//获得消息源
System.Windows.Interop.HwndSource source = System.Windows.Interop.HwndSource.FromHwnd(handle);

source.AddHook(HotKeyHook);
}
static private IntPtr HotKeyHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
//热键处理过程
{
const int WM_HOTKEY = 0x0312;//如果m.Msg的值为0x0312那么表示用户按下了热键
if (msg == WM_HOTKEY)
{
switch (wParam.ToInt32())
{
case 100:    //按下的是Shift+S
//此处填写快捷键响应代码
MessageBox.Show("按下的是Shift+S");
break;
case 101:    //按下的是Ctrl+B
//此处填写快捷键响应代码
break;
case 102:    //按下的是Ctrl+Alt+S
break;
}
}
return IntPtr.Zero;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: