Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 234
Judah Gabriel Himango Avatar asked Sep 05 '08 21:09

Judah Gabriel Himango


People also ask

How do I minimize to system tray?

Press Alt + F1 and that window will minimize to the tray.


1 Answers

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.

like image 186
FlySwat Avatar answered Oct 18 '22 00:10

FlySwat