Maximize C# Application from System Tray using Keyboard Shortcut

喜欢而已 提交于 2019-12-05 02:25:56

问题


Can I know if there is anyway that I can maximise my windows form application from the system tray using say a Keyboard Shortcut rather than clicking on it?

I am currently minimizing using this piece of code

//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;
    }
}

Hence, I need a keyboard shortcut that should maximize it. Much thanks!


回答1:


EDIT: If you simply want to 'reserve a key combination' to perform something on your application, a Low-Level keyboard hook whereby you see every keypress going to any other application is not only an overkill, but bad practice and in my personal view likely to have people thinking that you're keylogging! Stick to a HOT-KEY!

Given that your icon will not have keyboard focus, you need to register a global keyboard hotkey.

Other similar questions:

  • How can I register a global hot key to say CTRL+SHIFT+(LETTER)
  • Best way to tackle global hotkey processing in c#?

Example from Global Hotkeys With .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(); 
}



回答2:


To do this, you must use "Low-Level Hook". You will find all information about it on this article : http://blogs.msdn.com/b/toub/archive/2006/05/03/589423.aspx

Look at this too : http://www.codeproject.com/Articles/6362/Global-System-Hooks-in-NET




回答3:


I second Franck's suggestion about a global keyboard hook. Personally, I had very good experiences with the CodeProject article "Processing Global Mouse and Keyboard Hooks in C#".

As they write in their article, you can do things like:

private UserActivityHook _actHook;

private void MainFormLoad(object sender, System.EventArgs e)
{
    _actHook = new UserActivityHook();

    _actHook.KeyPress += new KeyPressEventHandler(MyKeyPress);
}

You could then call a function in your MyKeyPress handler that opens your window.




回答4:


If you follow the guide here. It will show you how to register a global shortcut key.

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<KeyPressedEventArgs>(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
    }
}


来源:https://stackoverflow.com/questions/12423207/maximize-c-sharp-application-from-system-tray-using-keyboard-shortcut

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!