Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Minimize to system tray

Status
Not open for further replies.

iamanson

Programmer
Aug 17, 2001
42
AU
How can I make a C# application minimize to system tray?
Thanks!
 
Funny, I just went through this myself. Here's how I went about it:

First declare the Tray Icon (and Context Menu if you want one):

private System.Windows.Forms.NotifyIcon s_TrayIcon;
private System.Windows.Forms.ContextMenu ctxMenu;

In InitializeComponent():

this.s_TrayIcon = new System.Windows.Forms.NotifyIcon(this.components);
this.ctxMenu = new System.Windows.Forms.ContextMenu();

//
// s_TrayIcon
//
this.s_TrayIcon.ContextMenu = this.ctxMenu;
this.s_TrayIcon.Icon = ((System.Drawing.Icon)(resources.GetObject("s_TrayIcon.Icon")));
this.s_TrayIcon.Text = "Tool Tip";
this.s_TrayIcon.Visible = true;
this.s_TrayIcon.DoubleClick += new System.EventHandler(this.s_EventHandler);

I have a seperate function for initializing the Context Menu, but it's basically this:

private void InitializeContextMenu()
{
ctxMenu.MenuItems.Add( new MenuItem("lauch", new EventHandler(launch)));
ctxMenu.MenuItems.Add( new MenuItem("Check Now", new EventHandler(CheckNowEvent)));
ctxMenu.MenuItems.Add( new MenuItem("Settings", new EventHandler(OnOpen)));
ctxMenu.MenuItems.Add( new MenuItem("-"));
ctxMenu.MenuItems.Add( new MenuItem("Exit", new EventHandler(OnExit)));
}

Then the only thing left is to trap the users exit. I've done the same as MSN messenger and removed the minimize and maximize buttons, and then trapped the on_exit event. I did this by adding

this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);

to initializeComponent, and then adding:

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
this.ShowInTaskbar = false;
this.Visible = false;

e.Cancel = true;
}

And then one of the event handlers for the context menu reverses this process. I think that should be enough to get you going. Let me know if you need anything else, or if anybody out there has any suggestions on how to do this better as I'm still pretty new to it =)
 
hi , I was looking for a way to minimaze to sys tray .... and I am glad that I've found your post. Well to , to be honest at first reading I didn't understud much from you post but I've saved it and tomorow I am going to try it .

I have a question : how can I trace key presses( as if my application had focus ) even if my application is in sys tray ? Is this possible?

Any link to a tutorial about minimize to sys tray and related would pe apreciated. Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top