5,696,576 members and growing! (16,290 online)
Email Password   helpLost your password?
General Reading » Hardware & System » General     Intermediate

Show progress using the NotifyIcon application tray icon

By rmortega77

Sample code to dynamically change the NotifyIcon application system tray Icon, to show the progress of a simulated process.
C#, Windows, .NET, Dev

Posted: 9 Mar 2007
Updated: 9 Mar 2007
Views: 21,188
Bookmarked: 33 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
7 votes for this Article.
Popularity: 2.59 Rating: 3.06 out of 5
2 votes, 28.6%
1
0 votes, 0.0%
2
1 vote, 14.3%
3
4 votes, 57.1%
4
0 votes, 0.0%
5

Screenshot - CsNotifyIconProgress.jpg

Introduction

Say we have a process that takes a lot of time to complete - wouldn't it be helpful to add a NotifyIcon control (hidden from our main form, so we do not bother the user until we are done) to show the progress in the system tray Icon area? Here is a simple code to achieve this, by drawing some text in the application system tray Icon dynamically.

Using the code

  1. Click File -> New -> Project. Create a ChangeIcon Visual C# Windows Application Project
  2. Click View -> Toolbox.
  3. From the Toolbox, drag a ContextMenu item into the Form1 form.
  4. Click in the Context Menu menu item, appearing at top of the form, change Type Here, with &Show. Press Enter.
  5. Double-click the newly created Show menu item.
  6. Change the menuItem1_Click method with:

Double-click the startButton button. Replace the startButton_Click method with:

private void menuItem1_Click(object sender, System.EventArgs e)
   {
      this.Show();
   }
  1. From the Toolbox, drag a NotifyIcon into the Form1 form.
  2. Right Click the NotifyIcon1 NotifyIcon -> Properties. Set the property name to trayIcon. Click in the Icon property, push the button which appears next to it, and browse for the App.ico within the Wizard created ChangeIcon folder. Finally, set the ContextMenu property to contextMenu1.
  3. From the Toolbox, drag a ProgressBar into the Form1 form. Set the Dock property to Top.
  4. From the Toolbox, drag a Button into the Form1 form. Change the text property to Start, and the name to startButton.
  5. From the Toolbox, drag another Button into the Form1 form. Change the text property to Hide, and name to hideButton.
  6. From the Toolbox, drag a Timer to the form.

Double-click the startButton button. Replace the startButton_Click method with:

private void startButton_Click(object sender, System.EventArgs e)
  {
    //

    timer1.Enabled = true;
    startButton.Enabled = false;
    //

    progressBar1.Value = progressBar1.Minimum;
    progressBar1.Step = 1;
  }

Double-click the hideButton button. Replace the hideButton_Click method with:

private void hideButton_Click(object sender, System.EventArgs e)
   {
       this.Hide();
   }

Double-click the Timer1 timer. Replace the timer1_Tick method with:

private void timer1_Tick(object sender, System.EventArgs e)
   {
      if (progressBar1.Value == progressBar1.Maximum)
      {
      timer1.Enabled = false;
      startButton.Enabled = true;
      }
      else
      {
      //

      progressBar1.PerformStep();
      //

      string text = "Ok";
      if (progressBar1.Value != progressBar1.Maximum)
         text = progressBar1.Value.ToString();
      //

      System.Resources.ResourceManager resources = new 
          System.Resources.ResourceManager(typeof(Form1));
      Bitmap bmp =( (System.Drawing.Icon)(resources.GetObject
          ("trayIcon.Icon"))).ToBitmap();

      // Create an ImageGraphics Graphics object from bitmap Image

      System.Drawing.Graphics ImageGraphics = 
          System.Drawing.Graphics.FromImage(bmp);

      // Draw random code within Image

      System.Drawing.Font drawFont = new System.Drawing.Font
          ("Arial Narrow", 20, FontStyle.Regular);
      System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush
          (System.Drawing.Color.Blue);
      //

      System.Drawing.StringFormat drawFormat = 
          new System.Drawing.StringFormat();
      ImageGraphics.DrawString(text,drawFont, drawBrush, -2, 1, drawFormat);
      //

      // Dispose used Objects

      //

      drawFont.Dispose();
      drawBrush.Dispose();
      ImageGraphics.Dispose();
      //

      trayIcon.Icon = Icon.FromHandle(bmp.GetHicon());
      trayIcon.Text = text;
    
      }
   }

Finally, press (F5). That's 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

About the Author

rmortega77


Rodolfo Ortega is a Cuban Computer Scientist. He works as IT Auditor for the CIMEX S.A. subsidiary in Holguin, Cuba. He lives and works in Holguin, in the eastern part of the island of Cuba.

You can contact him at rodolfom[]cimex.com.cu for any personal message: Ideas on new articles; bibliography about new APIs; any contribution you could be willing to make are very wellcome... He currently needs info about new Windows Vista Shell APIs.

Submit questions related with current article to the article forum.
Occupation: Software Developer
Company: CIMEX S.A.
Location: Cuba Cuba

Other popular Hardware & System articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralVery ClevermemberPat Tormey2:35 12 Aug '08  
GeneralFromHandle to Grave...membersteve_nenorse11:11 17 Mar '07  
GeneralIt's been a while...mvpColin Angus Mackay12:17 9 Mar '07  
GeneralRe: It's been a while...memberEd.Poore9:02 10 Mar '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Mar 2007
Editor: Sean Ewington
Copyright 2007 by rmortega77
Everything else Copyright © CodeProject, 1999-2008
Web16 | Advertise on the Code Project