Click here to Skip to main content
15,860,972 members
Articles / Desktop Programming / Windows Forms
Article

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

Rate me:
Please Sign up or sign in to vote.
4.33/5 (25 votes)
11 Jul 20052 min read 263.5K   10K   135   44
An article on how to add a custom button to the caption bar.

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.
    C#
    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.
    C#
    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


Written By
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionCaption Bar Icon doesn't show up in Windows 10 Pin
Member 1239467926-Mar-16 8:24
Member 1239467926-Mar-16 8:24 
Generaldoent work on windows 7 Pin
billiout8-Sep-10 5:36
billiout8-Sep-10 5:36 
GeneralRe: doent work on windows 7 Pin
TyronM3-Oct-10 3:09
TyronM3-Oct-10 3:09 
GeneralRe: doent work on windows 7 Pin
bkaratte6-Aug-11 4:02
bkaratte6-Aug-11 4:02 
GeneralIs it possible to add that to all windows which gets open Pin
Prasanna Bhat7-Dec-09 4:02
professionalPrasanna Bhat7-Dec-09 4:02 
QuestionWTF no compiled .exe in both downloads? Pin
pathomax9-Jun-08 23:34
pathomax9-Jun-08 23:34 
Generalcannot convert file in visual studio 2005 Pin
yvanc15-May-08 16:55
yvanc15-May-08 16:55 
Generalfacing problem when from is resize dynamically Pin
yeshwantraut27-Dec-07 6:18
yeshwantraut27-Dec-07 6:18 
Generalremove when clicked Pin
hamid_m14-Jun-07 5:10
hamid_m14-Jun-07 5:10 
Generalnever clicked Pin
hamid_m14-Jun-07 4:54
hamid_m14-Jun-07 4:54 
QuestionPlease can you help ?? Pin
Ahmed M. Gamil14-May-07 0:54
professionalAhmed M. Gamil14-May-07 0:54 
AnswerRe: Please can you help ?? Pin
TyronM8-Jun-07 8:01
TyronM8-Jun-07 8:01 
GeneralThanks ! Pin
FXMC26-Dec-06 2:26
FXMC26-Dec-06 2:26 
GeneralAny books related to this type of programming Pin
dseknat20-Sep-06 20:06
dseknat20-Sep-06 20:06 
GeneralRe: Any books related to this type of programming Pin
Ri Qen-Sin20-Sep-06 20:36
Ri Qen-Sin20-Sep-06 20:36 
NewsExample Themed Button Pin
toertchn28-Feb-06 10:14
toertchn28-Feb-06 10:14 
GeneralRe: Example Themed Button Pin
tayspen18-Mar-06 9:40
tayspen18-Mar-06 9:40 
GeneralRe: Example Themed Button Pin
toertchn18-Mar-06 9:43
toertchn18-Mar-06 9:43 
AnswerRe: Example Themed Button Pin
gpailler23-Jul-07 1:35
gpailler23-Jul-07 1:35 
GeneralRe: Example Themed Button Pin
sameh_serag11-Mar-08 5:52
sameh_serag11-Mar-08 5:52 
GeneralWhen I Max. MDIChild it does not draw Button Pin
fayaz var13-Jan-06 23:54
fayaz var13-Jan-06 23:54 
GeneralRe: When I Max. MDIChild it does not draw Button Pin
sameh_serag11-Mar-08 5:45
sameh_serag11-Mar-08 5:45 
QuestionHow to Add in MDIChild Form Pin
fayaz var3-Jan-06 18:50
fayaz var3-Jan-06 18:50 
AnswerRe: How to Add in MDIChild Form Pin
TyronM5-Jan-06 8:41
TyronM5-Jan-06 8:41 
I'm glad that you can use it Smile | :)
Actually I havent done anything with MDIChilds yet but I guess it should be working when you replace all the WM_NC* Messages with their corresponding non-NC Messages.

Hope that helps,
Tyron
Generalvarious buttons for winXP skins Pin
Jon Hary22-Oct-05 5:04
Jon Hary22-Oct-05 5:04 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.