Winfrom 实现系统托盘
今天查了下winfrom系统怎么最小化到系统托盘。
原来有2个控件就可以实现了:contextMenuStrip;notifyIcon;
实现代码如下:
#region 私有方法 处理窗体的 显示 隐藏 关闭(退出) private void ExitMainForm() { if (MessageBox.Show("您确定要退出化验数据接收程序吗?", "确认退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK) { this.notifyIcon1.Visible = false; this.Close(); this.Dispose(); Application.Exit(); } } private void HideMainForm() { this.Hide(); } private void ShowMainForm() { this.Show(); this.WindowState = FormWindowState.Normal; this.Activate(); } #endregion #region 右键菜单处理,显示 隐藏 退出 private void menuItem_Show_Click(object sender, EventArgs e) { ShowMainForm(); } private void menuItem_Hide_Click(object sender, EventArgs e) { HideMainForm(); } private void menuItem_Exit_Click(object sender, EventArgs e) { ExitMainForm(); } #endregion //双击图标 private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) { if (this.WindowState == FormWindowState.Normal) { this.WindowState = FormWindowState.Minimized; HideMainForm(); } else if(this.WindowState == FormWindowState.Minimized) { ShowMainForm(); } } private void Form2_SizeChanged(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Minimized) { HideMainForm(); } } private void Form2_FormClosing(object sender, FormClosingEventArgs e) { e.Cancel = true; HideMainForm(); } private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) { this.Close(); this.Dispose(); } private void 登录ToolStripMenuItem_Click(object sender, EventArgs e) { ShowMainForm(); }
注意。还需要设置一下:将notifyIcon1的ContextMenuStrip书香设置成contextMenuStrip1。然后就可以了。
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号