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

, 11 Jul 2005
Rate this:
Please Sign up or sign in to vote.
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.
    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 Smile | :)

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

Share

About the Author

Tyron Madlener

Austria Austria
No Biography provided

Comments and Discussions

 
AnswerRe: Please can you help ?? PinmemberTyronM8-Jun-07 9:01 
GeneralThanks ! PinmemberFXMC26-Dec-06 3:26 
GeneralAny books related to this type of programming Pinmemberdseknat20-Sep-06 21:06 
GeneralRe: Any books related to this type of programming PinmemberBehind The Scene20-Sep-06 21:36 
NewsExample Themed Button Pinmembertoertchn28-Feb-06 11:14 
GeneralRe: Example Themed Button Pinmembertayspen18-Mar-06 10:40 
GeneralRe: Example Themed Button Pinmembertoertchn18-Mar-06 10:43 
AnswerRe: Example Themed Button Pinmemberp-gregg23-Jul-07 2:35 
GeneralRe: Example Themed Button Pinmembersameh_serag11-Mar-08 6:52 
GeneralWhen I Max. MDIChild it does not draw Button Pinmemberfayaz var14-Jan-06 0:54 
GeneralRe: When I Max. MDIChild it does not draw Button Pinmembersameh_serag11-Mar-08 6:45 
QuestionHow to Add in MDIChild Form Pinmemberfayaz var3-Jan-06 19:50 
AnswerRe: How to Add in MDIChild Form PinmemberTyronM5-Jan-06 9:41 
Generalvarious buttons for winXP skins PinmemberJon Hary22-Oct-05 6:04 
Generalconvert to VB.NET Pinmemberjugomkd22-Jul-05 9:34 
GeneralRe: convert to VB.NET PinsussAnonymous25-Jul-05 15:03 
GeneralRe: convert to VB.NET Pinmemberjugomkd25-Jul-05 23:49 
GeneralRe: convert to VB.NET Pinmember1tg4625-Aug-05 5:07 
GeneralRe: convert to VB.NET Pinmemberjugomkd26-Aug-05 17:48 
AnswerRe: convert to VB.NET Pinmember1tg462-Sep-05 6:00 
GeneralRe: convert to VB.NET Pinmemberfayaz var7-Jan-06 1:47 
GeneralRe: convert to VB.NET Pinmembershabonaa6-Jan-06 11:12 
GeneralAdd NotifyIcon Component Pinmemberemc218-Jul-05 7:44 
GeneralRe: Add NotifyIcon Component PinmemberTyron Madlener18-Jul-05 9:49 
GeneralWM_NCMOUSELEAVE event Pinmemberkobalcz8-Jul-05 0:32 

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

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

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