Click here to Skip to main content
15,860,859 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

 
Generalconvert to VB.NET Pin
jugomkd22-Jul-05 8:34
jugomkd22-Jul-05 8:34 
GeneralRe: convert to VB.NET Pin
Anonymous25-Jul-05 14:03
Anonymous25-Jul-05 14:03 
GeneralRe: convert to VB.NET Pin
jugomkd25-Jul-05 22:49
jugomkd25-Jul-05 22:49 
GeneralRe: convert to VB.NET Pin
Thomas Stockwell25-Aug-05 4:07
professionalThomas Stockwell25-Aug-05 4:07 
GeneralRe: convert to VB.NET Pin
jugomkd26-Aug-05 16:48
jugomkd26-Aug-05 16:48 
AnswerRe: convert to VB.NET Pin
Thomas Stockwell2-Sep-05 5:00
professionalThomas Stockwell2-Sep-05 5:00 
GeneralRe: convert to VB.NET Pin
fayaz var7-Jan-06 0:47
fayaz var7-Jan-06 0:47 
GeneralRe: convert to VB.NET Pin
shabonaa6-Jan-06 10:12
shabonaa6-Jan-06 10:12 
GeneralAdd NotifyIcon Component Pin
emc218-Jul-05 6:44
emc218-Jul-05 6:44 
GeneralRe: Add NotifyIcon Component Pin
Tyron Madlener18-Jul-05 8:49
Tyron Madlener18-Jul-05 8:49 
GeneralWM_NCMOUSELEAVE event Pin
kobalcz7-Jul-05 23:32
kobalcz7-Jul-05 23:32 
GeneralRe: WM_NCMOUSELEAVE event Pin
Tyron Madlener9-Jul-05 4:01
Tyron Madlener9-Jul-05 4:01 
QuestionXP version ? Pin
sbouli6-Jun-05 2:24
sbouli6-Jun-05 2:24 
AnswerRe: XP version ? Pin
Thomas Stockwell3-Sep-05 2:11
professionalThomas Stockwell3-Sep-05 2:11 
GeneralNice... but... Pin
Carl Mercier20-Apr-05 7:10
Carl Mercier20-Apr-05 7:10 
GeneralRe: Nice... but... Pin
Tyron Madlener21-Apr-05 1:51
Tyron Madlener21-Apr-05 1:51 
GeneralRe: Nice... but... Pin
GeminiMan21-Apr-05 6:52
GeminiMan21-Apr-05 6:52 
GeneralRe: Nice... but... Pin
Super Lloyd25-May-05 12:43
Super Lloyd25-May-05 12:43 
GeneralRe: Nice... but... Pin
HumanOsc13-Jul-05 3:54
HumanOsc13-Jul-05 3:54 

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.