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>
/// 构造方法
/// </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("关于");
sboutMenuItem.Tag = "关于";
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>
/// 右击任务栏图标
/// </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>
/// 关于
/// </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>
/// 主窗体关闭
/// </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;
}
}
}
}