C Method for Realizing Flashing Tray Icon Effect

  • 2021-10-15 11:12:29
  • OfStack

In this paper, an example is given to describe the method of realizing the effect of flashing tray icon by C #. Share it for your reference, as follows:

When the user is logging in to QQ or using the Firemail mail system to automatically collect mail, the tray icon flashes to remind the user of the running task.

Flash icons can be timed to switch tray icons, tray icons can be obtained from the ImageList control. Add 3 icon in the ImageList control, and the first icon represents the tray icon after the form starts. The second and third icons respectively represent timed switching icons when specific tasks occur.

(1) Setting the tray icon can be converted from an Image object in an ImageList control to an Icon object


/// <summary>
///  Set the icon displayed on the tray 
/// </summary>
/// <param name="index"> Index of pictures in image list </param>
private void setIconImg(int index)
{
  Image img = this.imgIcon.Images[index];
  Bitmap b = new Bitmap(img);
  Icon icon = Icon.FromHandle(b.GetHicon());
  this.niMain.Icon = icon;
}

(2) Switching of timer event realization icon


/// <summary>
///  Timer toggle icon display 
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tmrIcon_Tick(object sender, EventArgs e)
{
  if (iconFlag)
  {
    this.setIconImg(1);
    iconFlag = !iconFlag;
  }
  else
  {
    this.setIconImg(2);
    iconFlag = !iconFlag;
  }
}

(3) The Minimize button and Close button of the form realize the function of hiding the form. When the form is closed, FormClosing event will be executed to release all resources associated with this form. Therefore, it is necessary to cancel the closing event and realize the hiding function of the form and the display function of the tray.


/// <summary>
///  Hide the form display tray icon when closing the form 
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
{
  e.Cancel = true;
  this.Hide();
  this.niMain.Visible = true;
}

(4) The tray association menu has four functions:

① "Display": Display the main form and hide the tray icon.
② "Run": The icon flashes, and the simulation task is running.
③ "Stop": Restore the initialized icon and simulate the task to stop.
④ "Exit": Prompt the user and exit the application system.
The complete code is as follows:


namespace NotifyIconDemo
{
  public partial class FrmMain : Form
  {
    // Toggle the identity of a picture 
    private bool iconFlag = false;
    // Is the system running 
    private bool isRun = false;
    /// <summary>
    ///
    /// </summary>
    public FrmMain()
    {
      InitializeComponent();
      // Settings icon Picture displayed 
      this.setIconImg(0);
    }
    /// <summary>
    ///  Set the icon displayed on the tray 
    /// </summary>
    /// <param name="index"> Index of pictures in image list </param>
    private void setIconImg(int index)
    {
      Image img = this.imgIcon.Images[index];
      Bitmap b = new Bitmap(img);
      Icon icon = Icon.FromHandle(b.GetHicon());
      this.niMain.Icon = icon;
    }
    /// <summary>
    ///  Show the main form 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void tsmiMain_Click(object sender, EventArgs e)
    {
      // Show the main form 
      this.Visible = true;
      this.WindowState = System.Windows.Forms.FormWindowState.Normal;
      // Hide tray icon 
      this.niMain.Visible = false;
      this.Show();
    }
    /// <summary>
    ///  Quit 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void tsmiExit_Click(object sender, EventArgs e)
    {
      // Set the prompt information of tray 
      this.niMain.BalloonTipText = " Successful exit Demo!";
      this.niMain.BalloonTipTitle = " Quit ";
      this.niMain.ShowBalloonTip(1000*3);
      // Delayed exit 
      Thread.Sleep(1000 * 2);
      // Release tray icon resources 
      this.niMain.Dispose();
      // Terminate a thread 
      Application.ExitThread();
    }
    /// <summary>
    ///  Hide form and show tray icon when minimized 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void FrmMain_SizeChanged(object sender, EventArgs e)
    {
      if (this.WindowState == FormWindowState.Minimized)
      {
        this.Hide();
        this.niMain.Visible = true;
      }
    }
    /// <summary>
    ///  Hide the form display tray icon when closing the form 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
    {
      e.Cancel = true;
      this.Hide();
      //this.ShowInTaskbar = false;// Cancel the display of the form in the taskbar 
      this.niMain.Visible = true;
    }
    /// <summary>
    ///  Hide the form when it is loaded 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void FrmMain_Load(object sender, EventArgs e)
    {
      this.Hide();
    }
    /// <summary>
    ///  Timer toggle icon display 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void tmrIcon_Tick(object sender, EventArgs e)
    {
      if (!this.isRun)
      {
        return;
      }
      if (iconFlag)
      {
        this.setIconImg(1);
        iconFlag = !iconFlag;
      }
      else
      {
        this.setIconImg(2);
        iconFlag = !iconFlag;
      }
    }
    /// <summary>
    ///  Click the Run menu 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void tsmiRun_Click(object sender, EventArgs e)
    {
      this.tsmiRun.Enabled = false;
      this.tsmiStop.Enabled = true;
      // Set the running status 
      this.isRun = true;
    }
    /// <summary>
    ///  Click Stop Menu 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void tsmiStop_Click(object sender, EventArgs e)
    {
      this.tsmiRun.Enabled = true;
      this.tsmiStop.Enabled = false;
      // Set to stop state 
      this.isRun = false;
      // Restore icon display 
      this.setIconImg(0);
    }
  }
}

For more readers interested in C # related content, please check out the topics on this site: "C # Common Control Usage Tutorial", "C # Data Structure and Algorithm Tutorial", "C # Object-Oriented Programming Introduction Tutorial" and "C # Programming Thread Use Skills Summary"

I hope this article is helpful to everyone's C # programming.


Related articles: