views:

680

answers:

3

How can I send the window minimized to tray when on click close button? Also how to show icon in tray when application start?

+7  A: 

WinForm:

One approach is to set the Cancel property of FormClosingEventArgs in the FormClosing event of your window and instead minimize to tray. For minimizing to tray, see this article:

Window Tray Minimizer

Code Project has more articles on the topic, but the one I linked worked for me.

WPF:

I've never had to do this in WPF but did poke around for a solution. I found this:

Creating a Tray Icon for a WPF Application

You'll find the code works but I recommend testing. The article addresses opening an application minimized to the tray.

You might also find this sample on MSDN useful:

Notification Icon Sample

Jay Riggs
This one is not easy to attach to a WPF app.
Sergey Aldoukhov
You're right Sergey. I Googled and found some resources but nothing I'd consider fully satisfactory.
Jay Riggs
+2  A: 

There is nothing that comes embedded with WPF. From implementations that you can find on the net, there is an "easy" one, that uses WinForms:

http://msdn.microsoft.com/en-us/library/aa972170.aspx

But I like this one more (can be used for balloon tips too)

http://www.codeproject.com/KB/WPF/wpf_notifyicon.aspx

Sergey Aldoukhov
+1  A: 

In winforms you can overload WndProc and watch for the WM_CLOSE message.

    WM_CLOSE = 0x0010

    protected override void WndProc(ref Message m)
    {
      if(m.Msg == WM_CLOSE)
      {
        this.Hide();
        trayIcon.Show();
      }

    }
scottm