Adding a System tray Icon using C#

No.of Views4223
Bookmarked0 times
Downloads 
Votes0
By  amalhashim   On  15 Feb 2010 23:02:10
Tag : CSharp , Windows Forms
Adding a System tray Icon using C#
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

Windows system tray enables end users to access frequently accessed applications with ease.
.Net framework provides classes and methods to manage our application at system tray easily.
In this article an trying to explain how we can place our application icon in system tray and how to capture the events.

Prerequisites

1.Net framework 2.0 and later version
2.Visual studio 2005 and later version

Adding a notify icon


{codecitation class="brush: c#; gutter: true;" width="600px"}

private System.Windows.Forms.NotifyIcon myAppIcon;

myAppIcon.Icon = new Icon(this.GetType(), "icon1.ico");
myAppIcon.Text = "My Application";

{/codecitation}


Adding a context menu

For adding context menu we can use the System.Windows.Forms.ContextMenu class.
For adding menu items to the contextmenu we can use System.Windows.Forms.MenuItem class.


{codecitation class="brush: c#; gutter: true;" width="600px"}

private System.Windows.Forms.NotifyIcon myAppIcon;
private System.Windows.Forms.ContextMenu contextMenu;
this.contextMenu = new System.Windows.Forms.ContextMenu();
this.myAppIcon.ContextMenu = this.contextMenu;
this.myAppIcon.Icon = new Icon(this.GetType(), "icon1.ico");
this.myAppIcon.Text = "My Application";


this.myAppIcon.Visible = true;
this.myAppIcon.DoubleClick += new System.EventHandler(this.myAppIcon_DoubleClick);
this.contextMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItemSettings,
this.menuItemEnabled,
this.menuItemQuit});

this.menuItemSettings.Index = 0;
this.menuItemSettings.Text = "&Application Settings";
this.menuItemSettings.Click += new System.EventHandler(this.menuItemSettings_Click);
this.menuItemEnabled.Checked = true;
this.menuItemEnabled.Index = 1;
this.menuItemEnabled.Text = "&Enabled";
this.menuItemEnabled.Click += new System.EventHandler(this.menuItemEnabled_Click);
this.menuItemQuit.Index = 2;
this.menuItemQuit.Text = "&Exit";
this.menuItemQuit.Click += new System.EventHandler(this.menuItemQuit_Click);


{/codecitation}

There is another helpful class System.Management.ManagementEventWatcher which can handle the system tray icon contains two static methods start() stop() to enable and disable the system tray functionality.


{codecitation class="brush: c#; gutter: true;" width="600px"}

private void StartMonitoring()
{
myAppIcon.Icon = new Icon( this.GetType() , "Enabled.ico" );
myAppIcon.Text = "PriorityMonitor (Enabled)";

if ( ProcessWatcher == null )
{
WqlEventQuery wmiQuery = new WqlEventQuery( "SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA \"Win32_Process\"" );            
ProcessWatcher = new ManagementEventWatcher( wmiQuery );
ProcessWatcher.EventArrived +=new EventArrivedEventHandler(ProcessWatcher_EventArrived);
}
ProcessWatcher.Start();
}

private void StopMonitoring()
{
myAppIcon.Icon = new Icon( this.GetType() , "Disabled.ico" );
myAppIcon.Text = "PriorityMonitor (Disabled)";

if ( ProcessWatcher == null ) return; // Stop not needed!

ProcessWatcher.Stop();
}

{/codecitation}

Conclusion

In this article i was trying to explain how to add a system tray icon for your .net application as well as how to add a context menu along with the usage of ManagementEventWatcher class. Hope you all liked it. For queries you can reach me at amalhashim@gmail.com

Thank you

Amal

 
Sign Up to vote for this article
 
About Author
 
amalhashim
Occupation-Software Engineer
Company-Aditi Technologies
Member Type-Senior
Location-Not Provided
Joined date-07 Jun 2009
Home Page-http://lamahashim.blogspot.com
Blog Page-http://lamahashim.blogspot.com
I have done my masters in Computer Applications and graduation in Computer Science. I have great passion in working with Microsoft tool and technologies. I am also a Microsoft Most Valuable Professional. Personally my objective is to design/develop applications which eases user experience and performs better in long run.
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top