Implement dynamic system tray icon under C #

Source: Internet
Author: User
Tags bool exit implement visual studio
Dynamic Visual C # is Microsoft's introduction to the next generation of program development languages. NET Framework, is an important part of the. Microsoft's. NET platform is a new generation of Internet platform. For technical developers,. NET platform is very remarkable, first of all, it provides common language runtime, common language running platform, on the other hand, it is a large and comprehensive unified programming class. Visual C # is the invocation of these classes, objects to achieve many of the more powerful features.

This article by introducing to you one. NET platform with C # written on the taskbar can be parked in the icon program (similar to the FlashGet, OICQ that kind of system tray icon) to share with you the fun of using C # programming.



First, overview:


The icon program I am referring to here is similar to the programs that are frequently contacted in Windows, such as virus monitoring, media playback, and so on, that are parked on the taskbar and run in the background. They generally have a click to show or hide the main interface, right-click pop-up menu, when the mouse stops on the icon to display the function of the message.



The program mainly used a few 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 design and operation of the environment:


(1) Microsoft company Windows 2000 Server version or Windows XP version

(2). Net Framewrok SDK Beta 2 Edition



Third, the concrete realization steps:


(1). The project to create a new C # under Visual Studio may be named Animatesystemtray. This is illustrated below:


(2). Now we're going to design the main interface of the program:

Set the form's Text property to the animation system diagram designator example, and the Maximisebox, Minimisebox, ShowInTaskbar properties are set to false.

Add label controls, NotifyIcon controls, ContextMenu controls, and one timer control to the form.

Set the Label control's Text property to right-click the system tray icon, and 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, and then the ContextMenu control is edited, as shown here:

Finally, you will import two icons (Icon1.ico), (Icon2.ico) files to the project.
(3). Now we start writing the Code section:

First, add the following data members to our class:

Private Icon M_icon1;
Private Icon M_icon2;
private bool M_bflag;
private bool M_bshowwnd;

Modify the Form1 () function to:

Public Form1 ()
{
//
Required for Windows Form Designer support
//
InitializeComponent ();
//
Todo:add any constructor the 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 the MenuItem1, menuItem2, MENUITEM3, M_trayicon Click events, and the message handler functions are as follows:
private void Menuitem1_click (object sender, System.EventArgs e)
{
Timer1. Start ();//Open Timer
}

private void Menuitem2_click (object sender, System.EventArgs e)
{
Timer1. Stop ();//Halt timer
}

private void Menuitem3_click (object sender, System.EventArgs e)
{
Application.exit ()//Exit Application
}

private void M_trayicon_click (object sender, System.EventArgs e)
{
if (M_bshowwnd = = true)//Hide Main interface
{
This. Visible = false;
M_bshowwnd = false;
}
else//Display Main interface
{
This. Visible = true;
M_bshowwnd = true;
}
}


Finally, add the timer1 tick () function:

private void Timer1_Tick (object sender, System.EventArgs e)
{
if (m_icon1!= null && m_icon2!= NULL)//If all two icon files are loaded correctly
{
As long as the timer1 is started, a continuous selection transformation between the two icons enables 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!

When you run the program, a main window is displayed, and you can see an extra icon on the taskbar. When the mouse is moved to stop, a simple message will be displayed, click on the icon will hide the main window, right-click, out of a menu, select "Animation icon start" and "animation icon End" can control the status of the icon.



Four. Summary:


After this program, we can not find it difficult to use C # to do some application of the program effect is very convenient. I used to use the Shell_NotifyIcon (), Trackpopmenu () and DestroyMenu () in the SDK, MFC, and add the message processing function, the workload is very large. Is it convenient for Microsoft to do the work for us now? Join the ranks of C # quickly.

(The executive editor, especially north lvye@staff.ccidnet.com)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.