NotifyIcon (.Net 2.0)

flynn

Regular
Joined
Jul 28, 2005
Messages
59
It is my understanding that in order to get an Icon to display in the system tray, I need to use the NotifyIcon.

Using the code below, I can get my application to work similar to other tray apps. I say similar in that when I click the "Close" button, the app will minimize, but instead of minimizing to the tray (like MSN messenger with the animation), my app minimizes just above the "Start" button. Is there a way to get the animation to go to the system tray? To get around this, I hide the app first, then minimize so you don't even see the minimize animation.

Code:
        private void Form1_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
                this.Hide();
        }
 
I don't know exactly how Microsoft achieved this result in MSN Messenger, but if I was going to try and achieve this result myself I do try the following steps.

1. save size / location of the form
2. set the client size to 0,0
3. start a loop
- move form slightly closer to toolbar
- check if at toolbar
- if not loop
4. hide form
5. reset size and location (so its right for when its restored)

As I say this is completely theoretical, it is possible there is a built in feature for doing this, but without research, this is the approach i would take.

EDIT:- Whilst I was looking on CodeProject for something completely unrelated, I came across an article on doing this. Its done in c++, but perhaps might give some pointers to help you.

http://www.codeproject.com/shell/minimizetotray.asp
 
Last edited:
There is a Windows API function (DrawAnimatedRects) that will draw the minimize animation from anywhere you want to anywhere you want. Once you have that, you can do the animation, then call the Hide method on the form. I recommend checking the user's settings before you show the animation for the sake of those who have the window maximize/minimize animations turned off.
 
Just to clarify, the link I posted follows the method that Marble describes. As I mentioned previously the code is in c++, but it explains the method if not the exact code.
 
Back
Top