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

Asked

Viewed 2,024 times

7

How to do that when closing the application (either by close button, ALT F4, or any method other than the process manager), the application continues running in the Windows icon tray:

Bandeja de ícones

And it is very complicated to make a menu for when click with the context button (usually right button of the mouse) about the application icon in the tray?

3 answers

5


To do is not, to solve everything that implies can be a little more. To pass on to you all that is necessary would be very long. You can ask specific questions. To give you a basis to start I found a response in the OS who can take a test.

static class Program {
    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

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


public class MyCustomApplicationContext : ApplicationContext {
    private NotifyIcon trayIcon;

    public MyCustomApplicationContext() {
        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();
    }
}

I put in the Github for future reference.

A most complete tutorial.

5

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

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

To show the NotifyIcon where the application is closed, you can implement the event FormClosing in his form would be 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 take the visibility of your NotifyIcon before closing the application, otherwise the icon will remain in the system tray until the user passes the mouse over the icon. (I don’t know why this behavior, but I’ve suffered a lot with it).

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.