C# 托盤程式 例項 雙擊顯示窗體,最小化到托盤
阿新 • • 發佈:2019-01-03
原文:http://blog.csdn.net/lan_liang/article/details/7697742
單擊工作列 顯示-隱藏切換,右鍵選單,捕捉關閉窗體事件
[csharp] view plaincopyprint?- public partial class frmMain : Form
- {
- public frmMain()
- {
- InitializeComponent();
- }
- #region 登出
- publicvoid Logout()
-
{
- if (MessageBox.Show("確認要退出嗎?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
- {
- notifyIcon1.Visible = false;
- FormCollection fc = Application.OpenForms;
- if (fc != null && fc.Count > 0)
- {
-
foreach
- {
- window.Hide();
- }
- }
- CacheHelper.CurrentUsrName = "";
- CacheHelper.CurrentRoleId = 0;
- frmLogin fl = new frmLogin();
- fl.Show();
-
}
- }
- privatevoid 登出登陸ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Logout();
- }
- #endregion
- #region 修改密碼
- privatevoid 修改密碼ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- frmChangePwd fcp = new frmChangePwd();
- fcp.Show();
- }
- #endregion
- privatevoid frmMain_Load(object sender, EventArgs e)
- {
- }
- privatevoid frmMain_FormClosing(object sender, FormClosingEventArgs e)
- {
- if (MessageBox.Show("確認要退出嗎?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
- {
- Logout();
- }
- else
- {
- e.Cancel = true;
- }
- }
- privatevoid 最大化ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- ShowWin();
- }
- privatevoid 退出ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Logout();
- }
- privatevoid frmMain_SizeChanged(object sender, EventArgs e)
- {
- if (this.WindowState == FormWindowState.Minimized)
- {
- HideWin();
- }
- }
- privatevoid notifyIcon1_MouseClick(object sender, MouseEventArgs e)
- {
- if (e.Button == MouseButtons.Right)
- {
- contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
- }
- else
- {
- ShowWin();
- }
- }
- #region 隱藏顯示窗體
- /// <summary>
- /// 隱藏窗體
- /// </summary>
- privatevoid HideWin()
- {
- this.notifyIcon1.Visible = true;
- this.Hide();
- }
- /// <summary>
- /// 顯示主窗體
- /// </summary>
- privatevoid ShowWin()
- {
- if (Visible)
- {
- HideWin();
- }
- else
- {
- /////這裡注意順序很重要,先show 後設置state
- Show();
- WindowState = FormWindowState.Normal;
- }
- }
- #endregion
- }