Click here to Skip to main content
Click here to Skip to main content

Adding a 'Minimize to tray'-button to a Form's caption bar

By , 11 Jul 2005
 

Introduction

This short article shows you how to easily implement a 'Minimize to Tray' button to the caption bar of the form. The code that comes with this article only supports default forms and it doesn't support Visual Styles. Feel free to improve the code and to contact me if you need any help with it. It would be great if you send me a copy when you fix these previously mentioned limitations so that I can update this article.

Background

The current program I am working on needs to run in the background. So the best way to do this is to minimize that app to the tray bar where it is less annoying than in the task bar. So I added some code which causes the program to minimize when the user wants to close it via the Close button in the caption bar. It worked and for a time it was good. But it was a bad, not user-friendly way of doing it. I wanted to have a 'Minimize to Tray' button like eMule has - after doing some research, I noticed that this has not been done before in C#.

Using the code

  • Step 1: Add the file 'MinTrayBtn.cs' to your project, and add the NotifyIcon component with your IDE's designer. Set an icon for the NotifyIcon component and set the visibility to false.
  • Step 2: Now simply declare a MinTrayBtn variable in your Windows Form class to instantiate the object in the Form constructor.
    public class WinForm : System.Windows.Forms.Form {
    
        //... Some other code
        TyronM.MinTrayBtn mybutton;
        //... Some other code
    
        public WinForm() {
            mybutton = new TyronM.MinTrayBtn(this);
        }
    
    }
  • Step 3: Add a function to catch the MinTrayBtnClicked event and register the event handler. Use the Click-event of the NotifyIcon, then show the Form again.
    public class WinForm : System.Windows.Forms.Form {
        
        //... Some other code
        TyronM.MinTrayBtn mybutton;
        //... Some other code
    
        public WinForm() {
            mybutton = new TyronM.MinTrayBtn(this);
            mybutton.MinTrayBtnClicked += 
              new TyronM.MinTrayBtnClickedEventHandler(TrayBtn_clicked);
        }
        
        private void TrayBtn_clicked(object sender, EventArgs e) {
            this.Hide();
            this.notifyIcon1.Visible = true;
        }
    
    
        private void notifyIcon1_Click(object sender, System.EventArgs e) {
            this.Show();
            this.notifyIcon1.Visible = false;
        }
        
    }

Points of Interest

The caption bar is part of the non-client area of the window which is handled by Windows, so it isn't easy to add a button there. I had to draw the button by myself and capture various mouse events to clone the behaviour of the other buttons (this button behaves exactly like the others do, or at least it should do :)

Revision

  • 0.8.5

    11 Jul 2005

    • Changed: Variable caption button size (adjusts itself to the system metrics).
    • Fixed: Drawing issues on changes of the window width.
  • 0.8

    20 Apr 2005

    • Article creation.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Tyron Madlener
Austria Austria
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generaldoent work on windows 7memberbilliout8 Sep '10 - 5:36 
i tried on windows 7 and the button doent show up. if i click on the position which supposed to be the button it minimized to tray but the button dosnt show up. pls help :S
GeneralRe: doent work on windows 7memberTyronM3 Oct '10 - 3:09 
Probably because my code doesn't support XP Themes which you most likely have enabled in your Win7. However some guy updated my code in order to work with XP Themes. You can find the article under the name Add Transparent Menus and XP Titlebar Buttons to your application.
Give it a try!
GeneralRe: doent work on windows 7memberbkaratte6 Aug '11 - 4:02 
was anyone able to get the traybutton in the titlebar working on win7?
GeneralIs it possible to add that to all windows which gets openmemberPrasanna Bhat7 Dec '09 - 4:02 
I want to add my custom button which gets added to all the windows whichever opens. May be windows explorer or any other application.
QuestionWTF no compiled .exe in both downloads?memberpathomax9 Jun '08 - 23:34 
.. for those who have no compiler available, currently, maybe?
.. for those who simply cannot compile the project because of several reasons, maybe?
 
WTF!!
Generalcannot convert file in visual studio 2005memberyvanc15 May '08 - 16:55 
Hello, I'm trying to open the application in Visual Studio 2005 but it cannot seem to convert the project file into visual studio 2005. Can you upload a vs2005 version of the project? Thanks a lot.
 
By the way, this is the error I received from Visual Studio 2005:
Error converting project file. Element cannot contain attributes
Generalfacing problem when from is resize dynamicallymemberyeshwantraut27 Dec '07 - 6:18 
Hi,
 
I am using above class to paint button on title bar.when form loads it displays button properly.
 
But when i set from to new size it is drawing two buttons.I am not able to clean the button that was drawn before resizing the form,after set to new size i see two button one at old location and other new size next to form close button.
 
Can you please help me out.
 
Thanks
Yeshwant
Generalremove when clickedmemberhamid_m14 Jun '07 - 5:10 
when it call clicked event, remove button from caption and redraw it when mouse move over caption
how can i fix it?
Generalnever clickedmemberhamid_m14 Jun '07 - 4:54 
it never clicked in windows xp
what is SM_CXSIZE? and it might be for it
QuestionPlease can you help ??memberJustDeveloper14 May '07 - 0:54 
the minimize-to-tray code is good .. but all I want to know is how to draw that button on a hand made bar ... you know it's easy to create both the minimize and maximize buttons but the minimize to tray is some how difficult ... Please can you explain it to me ..Confused | :confused:
 
And God Be With You ...
JustDeveloper

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 12 Jul 2005
Article Copyright 2005 by Tyron Madlener
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid