Aperture
Aperture

Reputation: 2427

How to let Windows form EXIT to system tray?

I mean exit to system tray, not minimize to system tray, ie when you click on the "red cross" in the top right corner on a Windows form, instead of application closing, it runs in the system tray instead.

Upvotes: 3

Views: 2348

Answers (3)

Michael Baldry
Michael Baldry

Reputation: 2028

It is not possible to 'exit' to the system tray, a process with a message queue must be running to allow an icon to appear in the tray and function correctly (e.g. not disappear when you hover over it)

The way adobe and others handle this is to have a separate application that just does the system tray icon, when you click it and your application isn't running it starts it, if the application is already running, it brings it to the foreground.

Upvotes: 1

Dave D
Dave D

Reputation: 8972

Handle the FormClosing event, block the Close (e.Cancel = true) and instead minimize to the system tray as you would if you were, well, minimizing to the system tray.

You will, of course, have to have a condition under which closing the form doesn't minimize to the system tray. For example, tracking the state and if the form is already minimized then allowing the form to actually close.

Upvotes: 2

ajay_whiz
ajay_whiz

Reputation: 17931

You can hook to Form Closing event and cancel the closing event and hide the form

sample code

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }

Upvotes: 3

Related Questions