using System.Windows.Forms;
Demo
private void Window_Loaded(object sender, RoutedEventArgs e)
{
InitialTray();
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
WSCommFunc.WriteLog(_logFile, string.Format("Window_Closing...."));
if (_notifyIcon != null)
{
_notifyIcon.Visible = false;
_notifyIcon.Dispose();
_notifyIcon = null;
}
System.Environment.Exit(0);
}
NotifyIcon _notifyIcon;
private void InitialTray()
{
_notifyIcon = new NotifyIcon();
// 当光标放在上面的提示文本
_notifyIcon.Text = "自动升级工具";
// 通知区域是否显示图标
_notifyIcon.Visible = true;
// 不显示任务栏按钮
ShowInTaskbar = false;
// 读取程序图标,来作为托盘图标
_notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);
MenuItem open = new MenuItem("显示");
open.Click += Open_Click;
MenuItem hide = new MenuItem("隐藏");
hide.Click += Hide_Click;
_notifyIcon.ContextMenu = new ContextMenu(new MenuItem[] { open, hide });
_notifyIcon.MouseDoubleClick += _notifyIcon_MouseDoubleClick;
}
private void _notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Open_Click(sender, e);
}
}
private void Hide_Click(object sender, EventArgs e)
{
this.ShowInTaskbar = false;
this.Visibility = Visibility.Hidden;
}
private void Open_Click(object sender, EventArgs e)
{
this.Visibility = Visibility.Visible;
this.ShowInTaskbar = true;
this.Activate();
}