Keep application (Windows Form) open in the icon tray with C #

7

When you close the application (either close button, ALT F4 , or any method other than the process manager), continue running in the Windows Icon Tray:

And is it too complicated to make a menu when you click the context button (usually right mouse button ) over the application icon in the tray?

    
asked by anonymous 07.10.2015 / 12:43

3 answers

5

Doing is not, solving everything this implies may be a bit more. To give you everything you need would be very long. You can go asking specific questions. To give you a baseline to get started I found a answer in the SO that can test.

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        Application.Run(new MyCustomApplicationContext());
    }
}


public class MyCustomApplicationContext : ApplicationContext
{
    private NotifyIcon trayIcon;

    public MyCustomApplicationContext ()
    {
        // Initialize Tray Icon
        trayIcon = new NotifyIcon()
        {
            Icon = Resources.AppIcon,
            ContextMenu = new ContextMenu(new MenuItem[] {
                new MenuItem("Exit", Exit)
            }),
            Visible = true
        };
    }

    void Exit(object sender, EventArgs e)
    {
        // Hide tray icon, otherwise it will remain shown until user mouses over it
        trayIcon.Visible = false;

        Application.Exit();
    }
}

A more complete tutorial .

    
07.10.2015 / 12:58
4

To send the application to the system tray, you can add a NotifyIcon to the designer of your form and make it visible whenever you minify or (in this case) close the application.

To add a context menu to your NotifyIcon you can add a ContextMenuStrip (still in the form designer) and bind the menu to NotifyIcon using the ContextMenuStrip property.

To show NotifyIcon whenever the application is closed , you can implement the FormClosing event in your main form , something like:

>
public form1_FormClosing(object sender, EventArgs e)
{
    e.Cancel = true; // Cancelar o fechamento do form
    Hide(); // Ocultar o form
    // use this.WindowState = FormWindowState.Minimized; para minimizar
    notifyIcon.Visible = true; // Mostrar o notify icon
}

An important point is that you can not forget to get the visibility of your NotifyIcon before closing the application, otherwise the icon will remain in the system tray until the user passes the mouse above the icon. (I do not know why this behavior, but I have suffered a lot with it).

    
07.10.2015 / 13:13
0
1) Acrescentar no form o "notifyIcon";
2) Acrescentar código no evento "notifyIcon1_MouseDoubleClick";
3) Código:
        if (this.WindowState == FormWindowState.Minimized)
        {
            notifyIcon1.Icon = SystemIcons.Application;
            notifyIcon1.BalloonTipText = "Aplicação minimizada";
            notifyIcon1.ShowBalloonTip(1000);
        }

        else if (this.WindowState == FormWindowState.Normal)
        {
            notifyIcon1.BalloonTipText = "Aplicação maximizada";
            notifyIcon1.ShowBalloonTip(1000);
        }

4) Código no botão que minimiza:
    private void button2_Click(object sender, EventArgs e)
    {
        Hide(); // Ocultar o form
        this.WindowState = FormWindowState.Minimized;
    }
    
28.12.2018 / 15:02