|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionSay 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
Double-click the startButton button. Replace the private void menuItem1_Click(object sender, System.EventArgs e)
{
this.Show();
}
Double-click the startButton button. Replace the 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 private void hideButton_Click(object sender, System.EventArgs e)
{
this.Hide();
}
Double-click the Timer1 timer. Replace the 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!
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||