这回咱们来讲讲系统热键的注册C# 热键注册 实例讲解
其实很简单就是两个API的调用:
[DllImport("user32.dll")]
private static extern int RegisterHotKey(IntPtr hwnd, int id, int fsModifiers, int vk);
[DllImport("user32.dll")]
private static extern int UnregisterHotKey(IntPtr hwnd, int id);
HotKey.dll 是我包装的一个DLL ,注册热键的代码
public static bool RegHotKey(IntPtr hwnd, int hotKey_id, int fsModifiers, int vk)
{
if (RegisterHotKey(hwnd, hotKey_id, fsModifiers, vk) == 0)
return false;
else
return true;
}
取消注册的代码 :
public static void UnRegHotKey(IntPtr hwnd, int hotKey_id)
{
UnregisterHotKey(hwnd, hotKey_id);
}
示例工程代码:
int id_hotkey=3;
private const int WM_HOTKEY = 0x312;
private const int WM_CREATE = 0x1;
private const int WM_DESTROY = 0x2;
private const int MOD_ALT = 0x1;
private const int MOD_CONTROL = 0x2;
private const int MOD_SHIFT = 0x4;
private const int VK_SPACE = 0x20;
[DllImport("user32.dll")]
private static extern int RegisterHotKey(IntPtr hwnd, int id, int fsModifiers, int vk);
[DllImport("user32.dll")]
private static extern int UnregisterHotKey(IntPtr hwnd, int id);
protected void regkey()
{
if (!HotKey.RegHotKey(Handle, id_hotkey, MOD_ALT | MOD_CONTROL | MOD_SHIFT, VK_SPACE))
{ MessageBox.Show("Error"); }
}
protected void unregkey()
{
HotKey.UnRegHotKey(Handle, id_hotkey);
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
switch (m.Msg)
{
case WM_HOTKEY:
switch (m.WParam.ToInt32())
{
case 3:
MessageBox.Show("Hot Key : ctrl + shift + alt + space");
break;
default:
break;
}
break;
case WM_CREATE:
regkey();
break;
case WM_DESTROY:
unregkey();
break;
default:
break;
}
}
附件中是演示程序,热键是: ctrl+shift+alt + 空格.
很简单吧.热键就讲到这吧.下一次我们说一个比较好玩的.就是"桌面视频播放".
看来这个大家都会了哦.没人回复呀.呵呵.看来得拿出一个好玩的来吸引大家了. 我在附件中加入我下次要说的桌面视频播放的那个演示程序的执行文件吧.让大家先睹为快.哈哈. 顺便在这里说一下用法
Ctrl+Shift + S 打开一个视频文件
Ctrl+Shift + O (字母 O) 调节透明度
Ctrl+Alt + 删除键 退出程序
发表于 @ 2008年03月13日 14:59:00|评论(loading...)|编辑