Making a System Tray Icon for your application

本文档详细介绍了如何使用C#为应用程序添加系统托盘图标功能,包括创建托盘图标、使程序最小化到托盘、添加上下文菜单及通过气泡提示消息等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.dreamincode.net/forums/topic/116283-make-your-application-minimize-to-system-tray-in-c%23/

 

 

http://progtuts.info/14/your-c-program-in-the-system-tray/

 

This tutorial will cover the use of the NotifyIcon, better known as the System Tray Icon in c#. The NotifyIcon is a cool feature to add to your application, and you can see numerous examples of it's usage in for example μTorrent, MSN messenger, and your favourite antivirus program. Am sure I don't need to tell you the possibilities, so I'm just going to tell you how to use them. Best of all, NotifyIcon is easy to use, and provides lots of fun. So let's get to work!

What will this tutorial cover?

-How to make a tray icon
-How to make your application minimize to the tray
-How to add a context menu to the tray icon(but I won't explain the context menu itself here)
-How to show messages from the tray icon through a balloon

This tutorial is meant for beginning c# programmers who have mastered the basics, and are ready to add some more advanced features to their applications.

So how do I make a Tray Icon?

Right, let's make a new Windows Forms Application, name it as you please. Now go to the ToolBox, and select NotifyIcon, it's under the Common Controls.

Click on it, and drag it to your form. You will see NotifyIcon1 showing up in a gray box under your form. Now we need to add an icon to it, because without it, nothing shows up in the tray. So find a .ICO file, right click on NotifyIcon1, select properties, and then under Icon you can add your icon. Remember though, it will only accept .ICO files. Now when you run your program you'll see a tray icon showing up. That was easy right! Now let's make the little icon a bit more useful.

How do I minimize my application to the tray?

Ok let's create some code that does this: when the minimize button is clicked, the program minimizes to tray and the tray icon shows up. Double clicking on the tray icon shows the form again and removes the icon.

Set the NotifyIcon's visible property to false in the property editor. Now go to the property editor of Form1, click on the little lightning symbol to access the events, and double click on the Resize event, and change the code to:

view source

print?

1

private void Form1_Resize(object sender, EventArgs e)

2

{

 

3

    if (this.WindowState == FormWindowState.Minimized)//this code gets fired on every resize

4

    {                                                                                      //so we check if the form was minimized

   

 

5

        Hide();//hides the program on the taskbar

6

        notifyIcon1.Visible = true;//shows our tray icon

 

7

    }

8

}


Finally we need the code to make the program show up again when the icon is double clicked. So double click on NotifyIcon1 in the designer, to make the event handler for the double_click event show up, and type:

view source

print?

1

private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)

2

{

 

3

    Show();//shows the program on taskbar

4

    this.WindowState = FormWindowState.Normal;//undoes the minimized state of the form

 

5

    notifyIcon1.Visible = false;//hides tray icon again

6

}



And there you have it, your program closes to tray, and pops back up again when needed. You can change the name the user sees when hovering over the icon by changing the Text property.

How do I add a context menu to the tray icon?

Easy enough, but for the sake of keeping this tutorial short, I'm going to assume you know how to make a context menu. If not, it's in the toolbox under “Menus and toolbars” called ContextMenuStrip. Play with it, google it, am sure you can figure it out.

Now to add it to your tray icon, go to the property editor, and when clicking on the ContextMenuStrip property opens up a drop down list where you can select the appropriate Context Menu in case you have several, and the .NET framework handles the rest and makes it show up when right clicked on the tray icon.

How to pass messages through balloons?

Am sure you know those little balloons popping up from your tray, giving you info about things that have happened while the program was running in the background. You can specify a title, text, and pick an icon from a list of three possible ones(info, warning,error, or no icon). There are basically 2 ways of doing this:

By setting the BalloonTipIcon, BalloonTipText and BalloonTipTitle and then calling:

view source

print?

1

notifyIcon1.ShowBalloonTip(1000);//1000 is the time in milliseconds the balloon will show up



But a more flexible way is using an overload of ShowBalloonTip:

ShowBalloonTip(Int32, String, String, ToolTipIcon)

To give an example in code, let's make a balloon pop up three seconds after we minimized our form, just to simulate some event in your program. So go back to the Form1_Resize event we used earlier and add the last few lines to it so it becomes:

view source

print?

01

private void Form1_Resize(object sender, EventArgs e)

02

{

 

03

    if (this.WindowState == FormWindowState.Minimized)

04

    {                                                

 

05

        Hide();

06

        notifyIcon1.Visible = true;

 

07

          

08

        Thread.Sleep(3000);//pause for 3 seconds

 

09

        //shows a balloon for 1 sec with a title, some text, and the info icon

10

        //other possibilities are: TooltipIcon.None, Tooltipicon.Error, and TooltipIcon.Warning

 

11

        notifyIcon1.ShowBalloonTip(1000, "Hello", "This is a balloontip!", ToolTipIcon.Info);

12

    }

 

13

}



Don't forget to add: using System.Threading; to the top of your code or the Thread.Sleep part won't work. Now you'll see that three seconds after you minimize a balloon will pop up as shown in the picture in the top of this tutorial. Pretty cool right.

This finishes this tutorial, I hope it was useful. Feedback would be appreciated

 

 

Change the icon of the notifyIcon

 

 

notifyIcon.Icon = new
System.Drawing.Icon(AppDomain.CurrentDomain.BaseDi rectory + "running.ICO");


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值