文章更新
- 20170511-初次成文
为什么会有这篇文章
目前在做一个的小APP,需要运行开始就缩小到任务栏。
实现主要功能
- 程序启动自动隐藏到任务栏右侧通知栏显示。(与系统托盘同义)
- 双击系统托盘图标显示、隐藏窗口;
- 右击系统托盘图标提供三个菜单选项,“退出”、“隐藏”、“显示”;
需要的控件
- 建一个WinForm程序—IconForm,将Form属性ShowInTaskbar改为false,这样程序将不会在任务栏中显示。
- 将Form属性WindowState选择为 Minimized,以便起来自动最小化隐藏
- 在工具栏中的“公共控件”里,拖入NotifyIcon控件—notifyIcon1,这个是程序运行任务栏右侧通知区域图标显示控件,为控件notifyIcon的属性Icon添加一个icon图标,或从代码中加入。
- 在工具栏中的“菜单和工具栏”里,拖入ContextMenuStrip—contextMenuStrip1,这个控件是右击时关联菜单。
- 右键notifyIcon1选择属性,将其属性ContextMenuStrip改加为contextMenuStrip1,这个时候notifyIcon1和contextMenuStrip1两个控件就关联了。
- 右键contextMenuStrip1,选择属性,进入Items,然后点击“添加”,这里添加三个菜单选项:exitMenuItem、hideMenuItem、showMenuItem,同时分别将其Text属性改为:退出、隐藏和显示。
添加代码
然后,右击窗体,选择属性,转到事件页面,双击 Load 事件,给窗体添加代码
1 2 3 4 5 6 7 8 9 10 11
| private void Form1_Load(object sender, EventArgs e) { string startup = Application.ExecutablePath; int pp = startup.LastIndexOf("\\"); startup = startup.Substring(0, pp); string icon = startup + "\\flower.ico"; notifyIcon1.Icon = new System.Drawing.Icon(icon); }
|
这里需要一个ICO图标,我的放在这里了,图片请放在启动路径下

双击SizeChanged事件,添加代码
1 2 3 4 5 6 7 8
| private void Form1_SizeChanged(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Minimized) { this.Hide(); this.notifyIcon1.Visible = true; } }
|
双击窗体上的菜单项,添加相关代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| private void toolStripMenuItem1_Click(object sender, EventArgs e) { if (MessageBox.Show("你确定要退出程序吗?", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK) { notifyIcon1.Visible = false; this.Close(); this.Dispose(); Application.Exit(); } }
private void toolStripMenuItem2_Click(object sender, EventArgs e) { this.Hide(); }
private void toolStripMenuItem3_Click(object sender, EventArgs e) { this.Show(); this.WindowState = FormWindowState.Normal; this.Activate(); }
|
转到窗体设计模式,右击notifyIcon1 ,选择属性,双击其中DoubleClick,添加相关代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| private void notifyIcon1_DoubleClick(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Normal) { this.WindowState = FormWindowState.Minimized; this.Hide(); } else if (this.WindowState == FormWindowState.Minimized) { this.Show(); this.WindowState = FormWindowState.Normal; this.Activate(); } }
|
完整的代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace IconForm { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e) { string startup = Application.ExecutablePath; int pp = startup.LastIndexOf("\\"); startup = startup.Substring(0, pp); string icon = startup + "\\testIcon.ico"; notifyIcon1.Icon = new Icon(icon); } private void Form1_SizeChanged(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Minimized) { this.Hide(); this.notifyIcon1.Visible = true; } }
private void exitMenuItem_Click(object sender, EventArgs e) { if (MessageBox.Show("你确定要退出程序吗?", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK) { notifyIcon1.Visible = false; this.Close(); this.Dispose(); Application.Exit(); } } private void hideMenuItem_Click(object sender, EventArgs e) { this.Hide(); } private void showMenuItem_Click(object sender, EventArgs e) { this.Show(); this.WindowState = FormWindowState.Normal; this.Activate(); }
private void notifyIcon1_DoubleClick(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Normal) { this.WindowState = FormWindowState.Minimized; this.Hide(); } else if (this.WindowState == FormWindowState.Minimized) { this.Show(); this.WindowState = FormWindowState.Normal; this.Activate(); } } } }
|
程序效果


参考文章
- C# winForm启动最小化到任务栏右侧通知栏并交互操作