NotifyIcon的应用

本文介绍了一个使用C#实现的应用程序功能,当窗体被最小化时,其会隐藏到任务栏的系统托盘中,并显示一个带有提示的气球通知。此外,文章还展示了如何通过双击托盘图标使窗体恢复显示,以及如何在托盘图标的上下文菜单中添加退出选项。

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

private void Form1_Resize(object sender, EventArgs e)
        
{
            
if (this.WindowState == FormWindowState.Minimized)
            
{
                
this.ShowInTaskbar = false;
                
this.Visible = false;
                
this.notifyIcon1.Visible = true;
                
this.notifyIcon1.ShowBalloonTip(2000"BalloonTip Title""Hello BalloonTip...", ToolTipIcon.Info);
                
this.notifyIcon1.BalloonTipClicked += new EventHandler(notifyIcon1_BalloonTipClicked);
                
this.notifyIcon1.Click += new EventHandler(notifyIcon1_Click);
                ContextMenuStrip context 
= new ContextMenuStrip();
                context.Text 
= "Context Menu";
                context.Name 
= "Context Menu Strip";
                context.ShowImageMargin 
= false;
                ToolStripMenuItem tool 
= new ToolStripMenuItem();
                tool.Name 
= "Tool Strip Menu Item";
                tool.Text 
= "Exit";
                context.Items.AddRange(
new ToolStripMenuItem[] { tool });
                tool.Click 
+= new EventHandler(tool_Click);
                
this.notifyIcon1.ContextMenuStrip = context;
            }

        }


        
void tool_Click(object sender, EventArgs e)
        
{
            
this.Close();
        }


        
void notifyIcon1_Click(object sender, EventArgs e)
        
{
            NotifyIcon nIcon 
= (NotifyIcon)sender;
            nIcon.GetType().InvokeMember(
"ShowContextMenu",
                BindingFlags.Instance 
| BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, nIcon, null);
        }


        
void notifyIcon1_BalloonTipClicked(object sender, EventArgs e)
        
{
            MessageBox.Show(
"You just click the BalloonTip...");
        }


        
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        
{
            
this.Visible = true;
            
this.WindowState = FormWindowState.Normal;
            
this.ShowInTaskbar = true;
            
this.notifyIcon1.Visible = false;
        }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值