1、向窗体上添加如下控件:MenuStrip menuStrip1, NotifyIcon ni_frmMain,Timer timer1, ContentMenuStrip cms_notify。其中notify中包含显示、退出等。
2、实现的代码:
#region 托盘相关代码 #region 私有方法 处理窗体的 显示 隐藏 关闭(退出) /// <summary> /// 显示 /// </summary> private void ShowMainForm() { this.Show(); this.WindowState = this.lastFormState; this.Activate(); this.ShowInTaskbar = true; //任务栏中显示窗体图标 //this.ni_frmMain.Visible = false;//图盘中隐藏图标 } /// <summary> /// 隐藏 /// </summary> private void HideMainForm() { this.Hide(); this.ShowInTaskbar = false; //任务栏中显示窗体图标 //this.ni_frmMain.Visible = true;//图盘中隐藏图标 } /// <summary> /// 关闭(退出) /// </summary> private void ExitMainForm() { if (MessageBox.Show("您确定要退出主程序吗?", "确认退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK) { this.ni_frmMain.Visible = false; this.Close(); this.Dispose(); Application.Exit(); } } #endregion #region 右键菜单处理,显示 隐藏 退出 /// <summary> /// 显示 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tsmi_notifyShow_Click(object sender, EventArgs e) { ShowMainForm(); } /// <summary> /// 隐藏 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tsmi_notifyHide_Click(object sender, EventArgs e) { HideMainForm(); } /// <summary> /// 退出 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tsmi_notifyExit_Click(object sender, EventArgs e) { ExitMainForm(); } #endregion #region 私有方法 双击托盘上图标时,显示窗体 private void ni_frmMain_DoubleClick(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Minimized) { ShowMainForm(); } else { this.WindowState = FormWindowState.Minimized; HideMainForm(); } } #endregion #region 点最小化按钮时,最小化到托盘 private void frmMain_SizeChanged(object sender, EventArgs e) { if (this.WindowState != FormWindowState.Minimized) { this.ShowInTaskbar = true; //任务栏中窗体图标显示 //this.ni_frmMain.Visible = false; this.lastFormState = this.WindowState; } else { HideMainForm(); } } #endregion #region 窗体关闭时最小化到托盘 private void frmMain_FormClosing(object sender, FormClosingEventArgs e) { //e.Cancel = true; //this.ShowInTaskbar = false; //任务栏中窗体图标消失 //this.ni_frmMain.Visible = true; //HideTipForm(); } #endregion #region 图标闪烁 开启 关闭 /// <summary> /// 开启 /// </summary> private void StartFlicker() { this.timer1.Enabled = true; this.timer1.Start(); } /// <summary> /// 关闭 /// </summary> private void CloseFlicker() { this.timer1.Stop(); this.timer1.Enabled = false; this.ni_frmMain.Icon = Truelore.Fare.Client.Properties.Resources.truelore0; } private int i = 0; private void timer1_Tick(object sender, EventArgs e) { if (i < 1) { this.ni_frmMain.Icon = Truelore.Fare.Client.Properties.Resources.truelore0; i++; return; } else { this.ni_frmMain.Icon = Truelore.Fare.Client.Properties.Resources.truelore1; i = 0; } } #endregion #endregion
时间: 2024-11-05 23:37:47