1.在form窗体中拖入notifyIcon控件。
2.设置控件的text属性和指定ICO图标文件。
3.运行程序,发现右下角的出现了系统托盘。
更多技巧:
1.添加右键菜单:
在窗体上拖入contextMenuStrip控件,设置notifyIcon控件的contextMenuStrip属性名称为拖入的contextMenuStrip控件名称。
2.窗口关闭时窗口隐藏,通过右键菜单显示和关闭:
代码如下:
设置窗口的关闭为隐藏:
1: bool isClose = false;//设置标示 标示窗口是否真正关闭2: private void Form5_FormClosing(object sender, FormClosingEventArgs e)3: {
4: //如果不是真正关闭 则取消关闭事件 讲窗体设置为隐藏5: if (!isClose)6: {
7: e.Cancel = true;8: this.Visible = false;9: }
10: }
使用托盘的右键菜单关闭窗体:
1: /// <summary>2: // 托盘的右键关闭菜单 真正关闭窗体3: /// </summary>4: /// <param name="sender"></param>5: /// <param name="e"></param>6: private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)7: {
8:
9: isClose = true;10: Application.Exit();
11: }
使用托盘的右键菜单显示隐藏的窗体:
1: /// <summary>2: // 托盘的右键显示菜单 显示隐藏的窗体3: // </summary>4: // <param name="sender"></param>5: // <param name="e"></param>6: rivate void 显示ToolStripMenuItem_Click(object sender, EventArgs e)7:
8: this.Visible = true;
3.窗口最小化到系统托盘上
使用窗体SizeChanged事件隐藏窗体:
1: /// <summary>2: /// 设置系统状态为最小化的时候 窗体隐藏 注意使用的SizeChanged事件3: /// </summary>4: /// <param name="sender"></param>5: /// <param name="e"></param>6: private void Form5_SizeChanged(object sender, EventArgs e)7: {
8: //判断状态为最小化状态时隐藏窗体9: if (this.WindowState == FormWindowState.Minimized)10: {
11: this.Visible = false;12: }
13: }
使用托盘右键菜单显示隐藏的窗体,并设置窗体的显示方式为正常状态:
1: /// <summary>2: // 托盘的右键显示菜单 显示隐藏的窗体3: // </summary>4: // <param name="sender"></param>5: // <param name="e"></param>6: rivate void 显示ToolStripMenuItem_Click(object sender, EventArgs e)7:
8: this.Visible = true;9: this.WindowState = FormWindowState.Normal;//设置显示状态为正常状态
4.双击系统托盘图标显示和隐藏窗体
使用notifyIcon控件的DoubleClick事件:
1: /// <summary>2: /// notifyIcon控件的DoubleClick事件3: /// </summary>4: /// <param name="sender"></param>5: /// <param name="e"></param>6: private void notifyIcon1_DoubleClick(object sender, EventArgs e)7: {
8: bool isShow = this.Visible == true ? false : true;9: this.Visible = isShow;10: }
全部代码如下:
1: using System;2: using System.Collections.Generic;3: using System.ComponentModel;4: using System.Data;5: using System.Drawing;6: using System.Linq;7: using System.Text;8: using System.Windows.Forms;9:
10: namespace TestApp11: {
12: /// <summary>13: /// 系统托盘演示14: /// 樊凯15: /// http://kay.cnblogs.com16: /// </summary>17: public partial class Form5 : Form18: {
19: public Form5()20: {
21: InitializeComponent();
22: }
23:
24: private void Form5_Load(object sender, EventArgs e)25: {
26:
27: }
28: bool isClose = false;//设置标示 标示窗口是否真正关闭29: private void Form5_FormClosing(object sender, FormClosingEventArgs e)30: {
31: //如果不是真正关闭 则取消关闭事件 讲窗体设置为隐藏32: if (!isClose)33: {
34: e.Cancel = true;35: this.Visible = false;36: }
37: }
38:
39: /// <summary>40: /// 托盘的右键关闭菜单 真正关闭窗体41: /// </summary>42: /// <param name="sender"></param>43: /// <param name="e"></param>44: private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)45: {
46:
47: isClose = true;48: Application.Exit();
49: }
50: /// <summary>51: /// 托盘的右键显示菜单 显示隐藏的窗体52: /// </summary>53: /// <param name="sender"></param>54: /// <param name="e"></param>55: private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)56: {
57: this.Visible = true;58: this.WindowState = FormWindowState.Normal;//设置显示状态为正常状态59: }
60:
61: /// <summary>62: /// 设置系统状态为最小化的时候 窗体隐藏 注意使用的SizeChanged事件63: /// </summary>64: /// <param name="sender"></param>65: /// <param name="e"></param>66: private void Form5_SizeChanged(object sender, EventArgs e)67: {
68: //判断状态为最小化状态时隐藏窗体69: if (this.WindowState == FormWindowState.Minimized)70: {
71: this.Visible = false;72: }
73: }
74:
75: /// <summary>76: /// notifyIcon控件的DoubleClick事件77: /// </summary>78: /// <param name="sender"></param>79: /// <param name="e"></param>80: private void notifyIcon1_DoubleClick(object sender, EventArgs e)81: {
82: bool isShow = this.Visible == true ? false : true;83: this.Visible = isShow;84: }
85: }
86: }
The End!