What's the proper way to minimize to tray a C# WinForms app?

Judah Gabriel Himango picture Judah Gabriel Himango · Sep 5, 2008 · Viewed 63.6k times · Source

What is the proper way to minimize a WinForms app to the system tray?

Note: minimize to system tray; on the right side of the taskbar by the clock. I'm not asking about minimizing to taskbar, which is what happens when you hit the "minus" button on the window.

I've seen hackish solutions like, "minimize, set ShowInTaskbar = false, then show your NotifyIcon."

Solutions like that are hackish because the app doesn't appear to minimize to the tray like other apps, the code has to detect when to set ShowInTaskbar = true, among other issues.

What's the proper way to do this?

Answer

FlySwat picture FlySwat · Sep 6, 2008

There is actually no managed way to do that form of animation to the tray in native winforms, however you can P/Invoke shell32.dll to do it:

Some good info here (In the comments not the post):

http://blogs.msdn.com/jfoscoding/archive/2005/10/20/483300.aspx

And here it is in C++:

http://www.codeproject.com/KB/shell/minimizetotray.aspx

You can use that to figure out what stuff to Pinvoke for your C# version.