我找到了注册热键的一小段代码:
[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0312)
MessageBox.Show("Hotkey pressed");
base.WndProc(ref m);
}
public FormMain()
{
InitializeComponent();
//Alt + A
RegisterHotKey(this.Handle, this.GetType().GetHashCode(), 1, (int)'A');
}它工作得很完美,但我的问题是我想使用两种不同的快捷方式。我知道第二个参数是id,所以我想我可以创建一个不同的id,并在WndProc函数中添加一个新的if语句,但我不确定如何处理。
简而言之,我该如何创建第二个快捷方式呢?
谢谢,
发布于 2011-01-16 22:44:21
RegisterHotKey(this.Handle, this.GetType().GetHashCode(), 1, (int)'A')不要在这里使用GetHashCode()。只需给你的热键编号,从0开始。没有任何混淆ids的危险,热键ids是每个句柄特定的。您将在WndProc()方法中获得id。使用m.WParam.ToInt32()获取值:
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0312) { // Trap WM_HOTKEY
int id = m.WParam.ToInt32();
MessageBox.Show(string.Format("Hotkey #{0} pressed", id));
}
base.WndProc(ref m);
}https://stackoverflow.com/questions/4704134
复制相似问题