[C# winform]How To:Create and use TrayIcon Control

step1. Create a solution with C# winfrom project. now,we hava a new Form named "form1"

step2. Drag-Drop an NotifyIcon control named "notifyIcon1"

step3. Drag-Drop a ContextMenu control named "contextMenu1"

step4. Set notifyIcon1's properties:
        A.Icon: Select an icon file
        B.Text: Change it as your wish
        C.ContextMenu:Choose "contextMenu1" in the dropdownlist
        D.Visable:Let it be "false"

step5. Add contextMenu1's child Items:
        A.Add an new item named "menuItem1" and property "Text" is "show" 
        B.Add the second item named "menuItem3" and property "Text" is "close" 

step6.Add methods for controlling TrayIcon:

None.gif private   void  TrayIconClose()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   notifyIcon1.Dispose();
ExpandedBlockEnd.gif}

None.gif
private   void  TrayIconShow()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   notifyIcon1.Visible
=true;
InBlock.gif   
this.ShowInTaskbar=false;
InBlock.gif   
this.Hide();
ExpandedBlockEnd.gif}

None.gif
private   void  TrayIconHide()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   notifyIcon1.Visible
=false;
InBlock.gif   
this.ShowInTaskbar=true;
InBlock.gif   
this.Show();
InBlock.gif   
this.WindowState=FormWindowState.Normal;
ExpandedBlockEnd.gif}

step7.Add form1's Resize Events ,with codes like this:

None.gif private   void  Form1_Resize( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   
if(this.WindowState==FormWindowState.Minimized)
InBlock.gif    TrayIconShow();
ExpandedBlockEnd.gif}

step8. Add MenuItems' Events

None.gif private   void  menuItem1_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   
if(this.WindowState==FormWindowState.Minimized)
InBlock.gif    TrayIconHide();
ExpandedBlockEnd.gif}

None.gif
private   void  menuItem3_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   Application.Exit();
ExpandedBlockEnd.gif}

None.gif

step9.Enjoy it after hitting F5

Example Source !

转载于:https://www.cnblogs.com/COCPOWER/archive/2005/08/11/212971.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值