using System;
using System.Windows.Forms;
namespace Fountain.WinForm.NotifyDemo
{
public partial class FormMain : Form
{
/// <summary>
/// 通知控件
/// </summary>
private NotifyIcon notifyIcon = new NotifyIcon();
/// <summary>
/// 通知控件顯示菜單
/// </summary>
private ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
/// <summary>
/// 構(gòu)造方法
/// </summary>
public FormMain()
{
InitializeComponent();
}
/// <summary>
/// 窗體加載
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FormMain_Load(object sender, EventArgs e)
{
this.InitializeNotifyMenu();
this.notifyIcon.Text = this.Text;
this.notifyIcon.Visible = true;
this.notifyIcon.Icon = this.Icon;
this.notifyIcon.ContextMenuStrip = this.contextMenuStrip;
this.notifyIcon.DoubleClick += notifyIcon_DoubleClick;
}
/// <summary>
/// 托盤菜單
/// </summary>
private void InitializeNotifyMenu()
{
try
{
contextMenuStrip.Items.Clear();
ToolStripMenuItem showMenuItem = new ToolStripMenuItem("顯示界面");
showMenuItem.Tag = "顯示";
showMenuItem.Click += new EventHandler(ShowMenuItem_Click);
contextMenuStrip.Items.Add(showMenuItem);
ToolStripMenuItem sboutMenuItem = new ToolStripMenuItem("關(guān)于");
sboutMenuItem.Tag = "關(guān)于";
sboutMenuItem.Click += new EventHandler(AboutMenuItem_Click);
contextMenuStrip.Items.Add(sboutMenuItem);
ToolStripMenuItem exitMenuItem = new ToolStripMenuItem("退出");
exitMenuItem.Tag = "退出";
exitMenuItem.Click += new EventHandler(ExistMenuItem_Click);
contextMenuStrip.Items.Add(exitMenuItem);
}
catch(Exception exception)
{
throw new Exception(exception.Message);
}
}
/// <summary>
/// 右擊任務(wù)欄圖標(biāo)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void notifyIcon_DoubleClick(object sender, EventArgs e)
{
try
{
if (this.WindowState == FormWindowState.Normal)
{
this.WindowState = FormWindowState.Minimized;
this.Hide();
}
else if (this.WindowState == FormWindowState.Minimized)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();
}
}
catch (Exception objException)
{
throw new Exception(objException.Message);
}
}
/// <summary>
/// 顯示
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ShowMenuItem_Click(object sender, EventArgs e)
{
try
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();
}
catch (Exception objException)
{
throw new Exception(objException.Message);
}
}
/// <summary>
/// 關(guān)于
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AboutMenuItem_Click(object sender, EventArgs e)
{
try
{
}
catch (Exception objException)
{
MessageBox.Show(objException.Message);
}
}
/// <summary>
/// 退出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ExistMenuItem_Click(object sender, EventArgs e)
{
try
{
if (MessageBox.Show("你確定要退出程序嗎?","提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
{
this.notifyIcon.Visible = false;
this.notifyIcon.Dispose();
this.Dispose();
Application.Exit();
}
}
catch (Exception objException)
{
MessageBox.Show(objException.Message);
}
}
/// <summary>
/// 主窗體關(guān)閉
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
e.Cancel = true;
this.Hide();
this.notifyIcon.Dispose();
}
catch (Exception objException)
{
MessageBox.Show(objException.Message);
}
}
/// <summary>
/// 窗體大小變化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FormMain_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.ShowInTaskbar = false;
this.Hide();
this.notifyIcon.Visible = true;
}
}
}
}