上周需要实现软件最小化时以图标显示在任务栏的通知区域中,现在将实现方法记录如下:
1. 在软件主窗体FrmMain添加notifyIcon1控件,设置Icon,Text等属性。
2. 添加FrmMain_SizeChanged事件:
FrmMain_SizeChanged
private void FrmMain_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
notifyIcon1_Click
private void notifyIcon1_Click(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
//以下两句实现窗体显示在最前面的功能
this.TopMost = true;
this.TopMost = false;
}
右键菜单功能
//定义一个MenuItem数组,并把此数组同时赋值给ContextMenu对象
MenuItem[] mnuItms = new MenuItem[3];
mnuItms[0] = new MenuItem();
mnuItms[0].Text = "还原";
mnuItms[0].Click += new System.EventHandler(this.notifyIcon1_Click);//notifyIcon1_Click为点击图标呈现窗体事件
mnuItms[1] = new MenuItem("-");
mnuItms[2] = new MenuItem();
mnuItms[2].Text = "退出系统";
mnuItms[2].Click += new System.EventHandler(this.MenuItem_Quit_Click);//MenuItem_Quit_Click为退出系统事件
mnuItms[2].DefaultItem = true;
ContextMenu notifyiconMnu = new ContextMenu(mnuItms);
notifyIcon1.ContextMenu = notifyiconMnu; 到此,最小化时以图标显示在任务栏的通知区域中已实现。
1. 在软件主窗体FrmMain添加notifyIcon1控件,设置Icon,Text等属性。
2. 添加FrmMain_SizeChanged事件:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
private void FrmMain_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
3. 添加notifyIcon1_Click事件:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
private void notifyIcon1_Click(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
//以下两句实现窗体显示在最前面的功能
this.TopMost = true;
this.TopMost = false;
}
4. 实现右键菜单功能:在FrmMain()窗体构造函数,InitializeComponent();后添加以下代码:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
//定义一个MenuItem数组,并把此数组同时赋值给ContextMenu对象
MenuItem[] mnuItms = new MenuItem[3];
mnuItms[0] = new MenuItem();
mnuItms[0].Text = "还原";
mnuItms[0].Click += new System.EventHandler(this.notifyIcon1_Click);//notifyIcon1_Click为点击图标呈现窗体事件
mnuItms[1] = new MenuItem("-");
mnuItms[2] = new MenuItem();
mnuItms[2].Text = "退出系统";
mnuItms[2].Click += new System.EventHandler(this.MenuItem_Quit_Click);//MenuItem_Quit_Click为退出系统事件
mnuItms[2].DefaultItem = true;
ContextMenu notifyiconMnu = new ContextMenu(mnuItms);
notifyIcon1.ContextMenu = notifyiconMnu;