使用键盘快捷键从系统托盘中最大化C#应用程序

我是否可以知道我是否可以使用键盘快捷键而不是单击它来从系统托盘中最大化我的Windows窗体应用程序?

我目前正在尽量减少使用这段代码

//Minimize to Tray with over-ride for short cut private void MinimiseToTray(bool shortCutPressed) { notifyIcon.BalloonTipTitle = "Minimize to Tray App"; notifyIcon.BalloonTipText = "You have successfully minimized your app."; if (FormWindowState.Minimized == this.WindowState || shortCutPressed) { notifyIcon.Visible = true; notifyIcon.ShowBalloonTip(500); this.Hide(); } else if (FormWindowState.Normal == this.WindowState) { notifyIcon.Visible = false; } } 

因此,我需要一个键盘快捷键,它应该最大化它。 非常感谢!

编辑:如果你只是想“保留一个组合键”来在你的应用程序上执行某些操作,那么你可以看到每个按键进入任何其他应用程序的低级键盘钩子不仅是一种过度杀伤,而且是一种不好的做法,在我个人看来可能会有人认为你是键盘记录! 坚持热键!

鉴于您的图标不具有键盘焦点,您需要注册全局键盘热键。

其他类似的问题:

  • 如何注册全局热键以表示CTRL + SHIFT +(LETTER)
  • 解决c#中全局热键处理的最佳方法?

使用.NET的全局热键示例:

 Hotkey hk = new Hotkey(); hk.KeyCode = Keys.1; hk.Windows = true; hk.Pressed += delegate { Console.WriteLine("Windows+1 pressed!"); }; if (!hk.GetCanRegister(myForm)) { Console.WriteLine("Whoops, looks like attempts to register will fail " + "or throw an exception, show error to user"); } else { hk.Register(myForm); } // .. later, at some point if (hk.Registered) { hk.Unregister(); } 

为此,您必须使用“低级别挂钩”。 您可以在本文中找到有关它的所有信息: http : //blogs.msdn.com/b/toub/archive/2006/05/03/589423.aspx

看看这个: http : //www.codeproject.com/Articles/6362/Global-System-Hooks-in-NET

我是弗兰克关于全球键盘钩子的建议。 就个人而言,我对CodeProject文章“ 在C#中处理全局鼠标和键盘挂钩 ”有很好的经验。

当他们在文章中写道时,你可以做以下事情:

 private UserActivityHook _actHook; private void MainFormLoad(object sender, System.EventArgs e) { _actHook = new UserActivityHook(); _actHook.KeyPress += new KeyPressEventHandler(MyKeyPress); } 

然后,您可以在MyKeyPress处理程序中调用一个打开窗口的函数。

如果你按照这里的指南。 它将向您展示如何注册全局快捷键。

 public partial class Form1 : Form { KeyboardHook hook = new KeyboardHook(); public Form1() { InitializeComponent(); // register the event that is fired after the key press. hook.KeyPressed += new EventHandler(hook_KeyPressed); // register the CONTROL + ALT + F12 combination as hot key. // You can change this. hook.RegisterHotKey(ModifierKeys.Control | ModifierKeys.Alt, Keys.F12); } private void hook_KeyPressed(object sender, KeyPressedEventArgs e) { // Trigger your function MinimiseToTray(true); } private void MinimiseToTray(bool shortCutPressed) { // ... Your code } }