This article illustrates how C # realizes the effect of flashing tray icons. Share to everyone for your reference, specific as follows:
When the user is logged on QQ or uses the Firemail mail system to automatically collect the mail, the tray icon will be flashing prompts the user to run the task.
The flashing icon can be implemented using a timer-switching tray icon, which can be obtained from the ImageList control. Add three icon to the ImageList control, the first icon indicates the tray icon after the form is started. The second and third icons represent the icons that switch periodically when a particular task occurs.
(1) Set the icon of the pallet can be converted from an Image object in the ImageList control to an Icon object
<summary>
///Set tray display icon
///</summary>
///<param name= "index" > image in the list of images </ 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) Timer event to realize the switch of 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 minimized and closed buttons of the form implement hidden forms, and when the form closes, the FormClosing event is executed, releasing all resources associated with the form. Therefore, the shutdown event needs to be canceled, the form is hidden, and the tray display function is realized.
<summary>
///The hidden form when the form is closed displays the tray icon
///</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: Displays the main form, hidden tray icon.
② "Run": Icon flashing effect, simulation task is running.
③ Stop: Restores the initialized icon and simulates the task to stop.
④ exit: Prompts the user and exits the application system.
The complete code is as follows:
namespace Notifyicondemo {public partial class Frmmain:form {//Toggle picture's identity private bool Iconflag = false;
Whether the system is running private bool Isrun = false;
<summary>//////</summary> public frmmain () {InitializeComponent ();
Set icon display picture this.seticonimg (0); ///<summary>///Set the icon for the tray display///</summary>///<param name= "index" > image in the list of images </para
m> 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>///Display main form///</summary>///<param name= "Sender" ></param>///
<param name= "E" ></param> private void Tsmimain_click (object sender, EventArgs e) {//Show main form This.
Visible = true; This. WindowState = System.Windows.Forms.FormWindowState.Normal;
Hide tray icon this.niMain.Visible = false; This.
Show (); ///<summary>///exit///</summary>///<param name= "sender" ></param>///&L
T;param name= "E" ></param> private void Tsmiexit_click (object sender, EventArgs e) {//SET tray hint information
This.niMain.BalloonTipText = "Successful exit demo!";
This.niMain.BalloonTipTitle = "Exit";
This.niMain.ShowBalloonTip (1000*3);
Delayed Exit Thread.Sleep (1000 * 2);
Release tray icon resource this.niMain.Dispose ();
Terminate thread application.exitthread (); ///<summary>///Hide forms when minimized, display tray icon///</summary>///<param name= "Sender" ></PARAM&G
T <param name= "E" ></param> private void Frmmain_sizechanged (object sender, EventArgs e) {if ( This. WindowState = = formwindowstate.minimized) {this.
Hide ();
This.niMain.Visible = true; }}///<summary>///closes the form implicitlyHidden form Display tray icon///</summary>///<param name= "sender" ></param>///<param name= "E" ></pa
ram> private void Frmmain_formclosing (object sender, FormClosingEventArgs e) {e.cancel = true; This.
Hide (); This.
ShowInTaskbar = false;//Cancels the display of the form on the taskbar this.niMain.Visible = true;
///<summary>///Hide form when loading forms///</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 Run Menu///</summary>///<param name= "Sender" ></param> <param name= "E" ></param> private void Tsmirun_click (object sender, EventArgs e) {THIS.T
smirun.enabled = false;
This.tsmiStop.Enabled = true;
Set Run state This.isrun = true; }///<summary>///Click the Stop menu///</summary>///<param name= "sender" ></param>// /<param name= "E" ></param> private void Tsmistop_click (object sender, EventArgs e) {This.tsmiru
N.enabled = true;
this.tsmiStop.Enabled = false;
Set to stop state This.isrun = false;
The recovery icon displays this.seticonimg (0);
}
}
}
Read more about C # Interested readers can view the site topics: "C # Common control usage Tutorial", "C # Data structure and algorithm tutorial", "C # object-oriented Program design Introductory Course" and "C # Programming Thread Usage Skills Summary"
I hope this article will help you with C # programming.