我想在我的上下文菜单中为菜单项添加快捷方式。我正在使用以下代码:
mnuContextMenu = new ContextMenu();
saveMenuItem = new MenuItem("Speichern", SaveFile, Shortcut.CtrlS);
mnuContextMenu.MenuItems.Add(saveMenuItem);
快捷方式甚至显示在上下文菜单中,但是当我按下按键时它没有响应。
有人知道为什么吗?
我解决了这个问题......使用下面的代码...
使用系统;使用System.Windows.Forms;
命名空间 我的应用{公开课 课程 : 表格{私有常量 int WM_HOTKEY = 0x0312;[System.Runtime.InteropServices.DllImport("user32.dll")]private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
public static void Main()
{
Application.Run(new Program());
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// rej short key
RegisterHotKey(Handle, 1, (int)ModifierKeys.Control | (int)ModifierKeys.Shift, Keys.F.GetHashCode());
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
// check msg
if (m.Msg == WM_HOTKEY)
{
//short key
int key = m.WParam.ToInt32();
if (key == 1)
{
//
// example: this.Show();
}
}
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
//exit and free res
UnregisterHotKey(Handle, 1);
base.OnFormClosing(e);
}
}
}