Click here to Skip to main content
15,892,072 members
Articles / Desktop Programming / Windows Forms

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 264.9K   10K   135  
An article on how to add a custom button to the caption bar.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace MinTrayBtn_Demo
{
	/// <summary>
	/// Summary description for WinForm.
	/// </summary>
	public class WinForm : System.Windows.Forms.Form
	{
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.Label label;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.NotifyIcon notifyIcon1;
		/// <summary>
		/// Required designer variable.
		/// </summary>

		TyronM.MinTrayBtn mybutton;
	
		public WinForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			mybutton = new TyronM.MinTrayBtn(this);
			mybutton.MinTrayBtnClicked+=new TyronM.MinTrayBtnClickedEventHandler(TrayBtn_clicked);


			this.label.Text="The Button works now for every caption bar size but it "+
							"won't work with WinXP styles. For preventing the button to be displayed "+
							"on systems with visual styles, see http://www.codeproject.com/csharp/xptheme.asp\n\n"+
							"That's all. Have fun :)\n";
		}

		public 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;
		}


		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose (bool disposing)
		{
			if (disposing)
			{
				if (components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent() {
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(WinForm));
			this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
			this.label1 = new System.Windows.Forms.Label();
			this.label = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// notifyIcon1
			// 
			this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
			this.notifyIcon1.Text = "notifyIcon1";
			this.notifyIcon1.Click += new System.EventHandler(this.notifyIcon1_Click);
			// 
			// label1
			// 
			this.label1.Dock = System.Windows.Forms.DockStyle.Top;
			this.label1.Location = new System.Drawing.Point(0, 0);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(272, 23);
			this.label1.TabIndex = 1;
			this.label1.Text = "=== \'Minimize to Tray\' - Button ===";
			this.label1.TextAlign = System.Drawing.ContentAlignment.TopCenter;
			// 
			// label
			// 
			this.label.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
						| System.Windows.Forms.AnchorStyles.Right)));
			this.label.Location = new System.Drawing.Point(0, 24);
			this.label.Name = "label";
			this.label.Size = new System.Drawing.Size(272, 120);
			this.label.TabIndex = 0;
			this.label.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// WinForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(272, 133);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.label);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MinimumSize = new System.Drawing.Size(260, 160);
			this.Name = "WinForm";
			this.Text = "WinForm";
			this.ResumeLayout(false);
		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new WinForm());
		}
		
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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