C # implement dynamic system tray icon

xiaoxiao2021-03-06  39

Visual C # is an important part of Microsoft .NET framework in Microsoft. Net framework. Microsoft's .NET platform is called a new generation of internet platforms. For technical developers, the two major features of the .NET platform are very excellent. First, when the public language it provides, it is the public language operation platform, and on the other hand is its huge and comprehensive unified programming class. Visual C # is to achieve many more powerful functions by calling these classes.

This article introduces you to a .NET platform written on the taskbar on the taskbar (similar to the Flashget, OICQ type system tray icon) with C # to experience the fun of C # programming with everyone.

I. Overview:

The icon program I refer to here is similar to the parking of the virus monitoring, media playback, etc. running in the taskbar in Windows, virus monitoring, media playback, etc. running in the background. They typically have click display or hidden main interface, right-click pop-up menu, display the functionality of the prompt information when the mouse stops on the icon.

The program mainly used several classes:

System.windows.Fomrs.Notifyicon class system.windows.forms.contextMenu class system.windows.Forms.MenuiteM class System.Windows.Forms.Label class system.Windows.Forms.Timer class

Second, the program is designed and running the environment:

Microsoft Window 2000 Server Edition or Window XP Edition .NET Framewrok SDK Beta 2

Third, specific implementation steps:

In Visual Studio, create a new C # project, named AnimateSystemTray. The illustration is as follows:

Now let's design the main interface of the program: first set the form's Text property to "Movie System Icon", MaxImiseBox, MinimiseBox, showintaskbar properties are set to false. Add a Label control, Notifyicon control, a ContextMenu control, and Timer controls. Set the Text property of the Label control to "Please click on the system tray icon"; set the ICON property of the Notifyicon control to an icon file:, the text property is set to "This is an example program!", The ContextMenu property is set to "ContextMenu1"; Next start Editing the ContextMenu control, the illustration is as follows:

Finally, you must go to the item in importing two icons (icon1.ico) files. Now we start writing code parts: First add the following data members in our class:

Private icon m_ICON1; private icon m_ICON2; private bool m_bflag; private bool m_bshowwnd;

Modify the FORM1 () function is:

public Form1 () {//// Required for Windows Form Designer support // InitializeComponent (); //// TODO: Add any constructor code after InitializeComponent call // m_bFlag = true; m_bShowWnd = true; try {m_Icon1 = new Icon ("Icon1.ico"); // Import Icon file m_ICON2 = new icon ("icon2.ico");} catch (exception e) {messagebox.show ("error" E.MESSAGE, "Animate Tray - Error" ); menuItem2.Enabled = false; menuItem3.Enabled = false;}} add Click event menuItem1, menuItem2, menuItem3, m_trayIcon, the message handler as follows: private void menuItem1_Click (object sender, System.EventArgs e) {timer1.Start ( ); // Open timer} Private void meneuitem2_click (object sender, system.eventargs e) {time1.stop (); // stop timer} private void meneuitem3_click (object sender, system.eventargs e) {Application.exit ( ); // Exit application} private void m_trayicon_click (object sender, system.eventargs e) {if (m_bshowwwnd == true) // Hide main interface {this.visible = false; m_bshowwnd = false;} else // Show main Interface {this.visible = true; m_bshowwnd = true;}} Finally, add TIMER1 TICK () function:

Private void Timer1_Tick (Object sender, system.eventargs e) {if (m_icon1! = null && m_ICON2! = null) // If the two icon files are properly loaded {// only Timer1 is started, then in two icons Between continuous selection transformation, realize the animation effect if (m_bflag == true) {m_trayicon.icon = m_ICON2; m_bflag = false;} else {m_trayicon.icon = m_ICON1; m_bflag = true;}}}

Ok, press CTRL F5 to try the effect! Run the program, will display a main window, you can also see an icon over the taskbar. When the mouse is moved, a simple prompt message will be displayed. Click this icon to hide the main window, click Right click, come out, select "Animation Icon Start" and "Animation Icon End" to control icon status.

four. to sum up:

转载请注明原文地址:https://www.9cbs.com/read-71119.html

New Post(0)