2.加notifyicon控件notifyIcon1,为控件notifyIcon1的属性Icon添加一个icon图标。
3.添加窗体最小化事件(首先需要添加事件引用):
1 // this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);
2 //上面一行是主窗体InitializeComponent()方法中需要添加的引用
3 private void Form1_SizeChanged(object sender, EventArgs e)
4 {
5 if (this.WindowState==FormWindowState.Minimized)
6 {
7 this.Hide();
8 this.notifyIcon1.Visible=true;
9 }
10 }
用别的事件也是可以的:
1 //当窗体大小发生变化时执行该代码:控制窗体是否在任务栏显示图标
2 private void Form1_Resize(object sender, EventArgs e)
3 {
4 if (this.WindowState == FormWindowState.Minimized)
5 {
6 this.ShowInTaskbar = false;
7 }
8 else if (this.WindowState == FormWindowState.Normal || this.WindowState == FormWindowState.Maximized)
9 {
10 this.ShowInTaskbar = true;
11 }
12 }
4.添加点击图标事件(首先需要添加事件引用):
1 private void notifyIcon1_Click(object sender, EventArgs e)
2 {
3 this.Visible = true;
4 this.WindowState = FormWindowState.Normal;
5 this.notifyIcon1.Visible = false;
6 }
5.可以给notifyIcon添加右键菜单:
主窗体中拖入一个ContextMenu控件contextMenu1,点中控件,在上下文菜单中添加菜单,notifyIcon1的ContextMenu行为中选中contextMenu1作为上下文菜单。
(可以在子菜单中添加行为)
【三】实例实践:在文本框中输入文字,在系统托盘中显示提示气泡信息