C#处理的窗体最小化到托盘,以及双击恢复窗口代码。
一:添加notifyIcon1控件。添加各种事件响应······
二:右键快捷菜单的添加:
1:添加contextMenuStrip控件,设置各个项
2:添加各个项的处理事件
3:与前面的notifyIcon1控件绑定(通过contextMenuStrip属性设置)
- #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 退出ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- ExitMainForm();
- }
- private void 还原ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- ShowMainForm();
- }
- #endregion
- #region 双击托盘上图标时,显示窗体
- private void notifyIcon1_DoubleClick_1(object sender, EventArgs e)
- {
- ShowMainForm();
- }
- #endregion
- #region 点最小化按钮时,最小化到托盘
- private void Form_Server_SizeChanged(object sender, EventArgs e)
- {
- if (this.WindowState == FormWindowState.Minimized)
- {
- HideMainForm();
- }
- }
- #endregion
- #region 窗体关闭时最小化到托盘
- private void Form_Server_FormClosing(object sender, FormClosingEventArgs e)
- {
- e.Cancel = true;
- HideMainForm();
- }
- #endregion
阅读(4831) | 评论(0) | 转发(0) |