正常来说,老板键一般少不了:Alt+Ctrl+Shift+XX这种多组合方式,然而各类代码就是不直接说明,也没个提示,看来是有意隐藏,终于,还是被我发现其中的一些不为人知的隐藏属性:
下面看一下本人修改自网络常见的代码:
public delegate void HotkeyEventHandler(int HotKeyID);
public class SystemHotKey : System.Windows.Forms.IMessageFilter
{
List<UInt32> keyIDs = new List<UInt32>();
IntPtr hWnd;
public event HotkeyEventHandler OnHotkey;
public enum KeyFlags
{
Alt = 0x1,
Ctrl = 0x2,
Shift = 0x4,
Win = 0x8,
//组合键等于值相加
Alt_Ctrl = 0x3,
Alt_Shift = 0x5,
Ctrl_Shift = 0x6,
Alt_Ctrl_Shift = 0x7
}
[DllImport("user32.dll")]
public static extern UInt32 RegisterHotKey(IntPtr hWnd, UInt32 id, UInt32 fsModifiers, UInt32 vk);
[DllImport("user32.dll")]
public static extern UInt32 UnregisterHotKey(IntPtr hWnd, UInt32 id);
[DllImport("kernel32.dll")]
public static extern UInt32 GlobalAddAtom(String lpString);
[DllImport("kernel32.dll")]
public static extern UInt32 GlobalDeleteAtom(UInt32 nAtom);
public SystemHotKey(IntPtr hWnd)
{
this.hWnd = hWnd;
}
public int RegisterHotkey(KeyFlags keyflags, System.Windows.Forms.Keys Key)
{
System.Windows.Forms.Application.AddMessageFilter(this);
UInt32 hotkeyid = GlobalAddAtom(System.Guid.NewGuid().ToString());
RegisterHotKey((IntPtr)hWnd, hotkeyid, (UInt32)keyflags, (UInt32)Key);
keyIDs.Add(hotkeyid);
return (int)hotkeyid;
}
public void UnregisterHotkeys()
{
if (keyIDs.Count > 0)
{
System.Windows.Forms.Application.RemoveMessageFilter(this);
foreach (UInt32 key in keyIDs)
{
UnregisterHotKey(hWnd, key);
GlobalDeleteAtom(key);
}
keyIDs.Clear();
}
}
public bool PreFilterMessage(ref System.Windows.Forms.Message m)
{
if (m.Msg == 0x312)
{
if (OnHotkey != null)
{
foreach (UInt32 key in keyIDs)
{
if ((UInt32)m.WParam == key)
{
OnHotkey((int)m.WParam);
return true;
}
}
}
}
return false;
}
}
public class SystemHotKey : System.Windows.Forms.IMessageFilter
{
List<UInt32> keyIDs = new List<UInt32>();
IntPtr hWnd;
public event HotkeyEventHandler OnHotkey;
public enum KeyFlags
{
Alt = 0x1,
Ctrl = 0x2,
Shift = 0x4,
Win = 0x8,
//组合键等于值相加
Alt_Ctrl = 0x3,
Alt_Shift = 0x5,
Ctrl_Shift = 0x6,
Alt_Ctrl_Shift = 0x7
}
[DllImport("user32.dll")]
public static extern UInt32 RegisterHotKey(IntPtr hWnd, UInt32 id, UInt32 fsModifiers, UInt32 vk);
[DllImport("user32.dll")]
public static extern UInt32 UnregisterHotKey(IntPtr hWnd, UInt32 id);
[DllImport("kernel32.dll")]
public static extern UInt32 GlobalAddAtom(String lpString);
[DllImport("kernel32.dll")]
public static extern UInt32 GlobalDeleteAtom(UInt32 nAtom);
public SystemHotKey(IntPtr hWnd)
{
this.hWnd = hWnd;
}
public int RegisterHotkey(KeyFlags keyflags, System.Windows.Forms.Keys Key)
{
System.Windows.Forms.Application.AddMessageFilter(this);
UInt32 hotkeyid = GlobalAddAtom(System.Guid.NewGuid().ToString());
RegisterHotKey((IntPtr)hWnd, hotkeyid, (UInt32)keyflags, (UInt32)Key);
keyIDs.Add(hotkeyid);
return (int)hotkeyid;
}
public void UnregisterHotkeys()
{
if (keyIDs.Count > 0)
{
System.Windows.Forms.Application.RemoveMessageFilter(this);
foreach (UInt32 key in keyIDs)
{
UnregisterHotKey(hWnd, key);
GlobalDeleteAtom(key);
}
keyIDs.Clear();
}
}
public bool PreFilterMessage(ref System.Windows.Forms.Message m)
{
if (m.Msg == 0x312)
{
if (OnHotkey != null)
{
foreach (UInt32 key in keyIDs)
{
if ((UInt32)m.WParam == key)
{
OnHotkey((int)m.WParam);
return true;
}
}
}
}
return false;
}
}
以上有几个要点说一下:
1:System.Windows.Forms.Application.AddMessageFilter(this);这句需要对应System.Windows.Forms.Application.RemoveMessageFilter(this);这里用完要记得取消。
由于原来的程序,只在构造函数里添加,所以取消后,再设置就会失效了,这里直接在注册的时候给加上,取消时去掉,注意下这个效果即可。
2:热键的组合:
//组合键等于值相加
Alt_Ctrl = 0x3,
Alt_Shift = 0x5,
Ctrl_Shift = 0x6,
Alt_Ctrl_Shift = 0x7
这个是不经意思发觉的,网上的代码都没有提到,估计转的人太多了,知道的又不写出来。
3:把Hastable变更成List<Unint32>方式。
最近事比较多,写文都比较简单了,大伙见谅了。
版权声明:本文原创发表于 博客园,作者为 路过秋天,博客 http://cyq1162.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则视为侵权。
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则视为侵权。