winform控件之notifyicon

发布时间:2025-12-10 11:25:55 浏览次数:7

notifyicon控件是指定可在通知区域创建图标的组件,比如我们在右下角的状态栏想要显示某些东西,就可以使用这个控件

这个控件的属性和事件都特别简单,一般它会和ContextMenu一起使用,完成后的功能是下图这个样子的

 

1.界面布局

这个例程中主要我们是想看右下角的样子,所以主界面没有任何显示,只添加了三个控件

notifyicon,timer和contextMenu,当然我们需要给notifyicon添加图标,然后设置contextmenu为下图样子

 

2.代码示例

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace WindowsFormsApplication1{public partial class Form1 : Form{public Form1(){InitializeComponent();timer1.Start();//启动定时器来闪烁图标}private void notifyIcon1_MouseClick(object sender, MouseEventArgs e){//点击左键的时候显示提示if (e.Button == MouseButtons.Left)notifyIcon1.ShowBalloonTip(30, "注意", "测试用例", ToolTipIcon.Warning);}private bool blink = false;private void timer1_Tick(object sender, EventArgs e){if (!blink){notifyIcon1.Icon = Properties.Resources.test;}else{notifyIcon1.Icon = Properties.Resources.test2;}blink = !blink;}private void toolStripMenuItem2_Click(object sender, EventArgs e){//显示窗口this.WindowState = System.Windows.Forms.FormWindowState.Normal;}private void toolStripMenuItem3_Click(object sender, EventArgs e){//退出this.Close();}}}

这里其实就做了三件事情

1.在点击左键的时候显示通知

2.在点击右键的时候显示contextmenu,然后在选择对应选项的时候进行对应的处理

3.用一个定时器来定时更换图标,类似于一个动态提示

 

需要做网站?需要网络推广?欢迎咨询客户经理 13272073477