CodeKicks.com
Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Wednesday, February 06, 2008

Windows System Tray Programming using C# and VB.NET

Windows System Tray provides easy access to the applications which we use frequently. .NET provides classes with many methods to manage your application at system tray using C# and VB.NET very easily. This article gives you a quick introduction of these .NET classes and methods and provides the download of demo application.

image

Following are the impotent classes to begin with to provide SystemTray functionality for .NET applications.
Examples may not be displayed properly in your browser, So you can download the source code given at the end of this article.

  1. System.Windows.Forms.NotifyIcon
    NotifyIcon is class defined in the namespace System.Windows.Forms which is useful to create an Icon to be displayed at the system tray.
    Following is the C# code to create NotifyIcon for System Tray.
    private System.Windows.Forms.NotifyIcon notifyIcon;







    When Starting the Systemtray service
    notifyIcon.Icon = new Icon( this.GetType() , "Enabled.ico" );
    notifyIcon.Text = "Process (Enabled)";




    When Stooping the Systemtray service
    notifyIcon.Icon = new Icon( this.GetType() , "Disabled.ico" );
    notifyIcon.Text = "Process (Disabled)";






  1. System.Windows.Forms.ContextMenu

    ContextMenu is also defined in the namespace System.Windows.Forms which is useful to create Menu to be displayed when right clicking on the system tray icon.


  2. System.Windows.Forms.MenuItem

    ContextMenu is also defined in the namespace System.Windows.Forms which is useful to create Menu items to be displayed when right clicking on the system tray icon. Now you can customize your system tray functionality by writing event handlers to these menu items. example displaying a form.


    Following is the sample code to create Context menu and associating it with notify icon.

    private System.Windows.Forms.NotifyIcon notifyIcon;
    private
    System.Windows.Forms.ContextMenu contextMenu;
    this.contextMenu = new System.Windows.Forms.ContextMenu();
    this.notifyIcon.ContextMenu = this.contextMenu;
    this.notifyIcon.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon.Icon")));
    this.notifyIcon.Text = "notifyIcon";


    this.notifyIcon.Visible = true;
    this.notifyIcon.DoubleClick += new System.EventHandler(this.notifyIcon_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);



  3. System.Management.ManagementEventWatcher

    This is the key class to handle the system tray icon contains two static methods start() stop() to enable and disable the system tray functionality.


    Following is the sample code to Start and Stop the System Tray Service.




    private void StartMonitoring()
    {
    notifyIcon.Icon = new Icon( this.GetType() , "Enabled.ico" );
    notifyIcon.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()
    {
    notifyIcon.Icon = new Icon( this.GetType() , "Disabled.ico" );
    notifyIcon.Text = "PriorityMonitor (Disabled)";

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

    ProcessWatcher.Stop();
    }



Download the demo application

Post a Comment