Click here to Skip to main content
13,045,248 members (49,549 online)
Click here to Skip to main content
Articles » Languages » C# » How To » Downloads

Stats

208.9K views
8.1K downloads
133 bookmarked
Posted 19 Apr 2005

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

, 11 Jul 2005
An article on how to add a custom button to the caption bar.
bin
Debug
MinTrayBtnDemo.exe.manifest
Release
favicon.ico
MinTrayBtn Demo.bdsproj
MinTrayBtn Demo.cmbx
MinTrayBtn Demo.prjx
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

Share

About the Author

Tyron Madlener
Austria Austria
No Biography provided

You may also be interested in...

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