欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

wpf notifyIcon

程序员文章站 2022-06-27 16:40:50
private void Initialzie() { notifyIcon = new NotifyIcon(); notifyIcon.BalloonTipText = "学生考勤系统"; notifyIcon.Text = "学生考勤系统2"; this.notifyIcon.Icon = S ......

private void Initialzie()
{
notifyIcon = new NotifyIcon();
notifyIcon.BalloonTipText = "学生考勤系统";
notifyIcon.Text = "学生考勤系统2";
this.notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);
this.notifyIcon.Visible = false;
//打开菜单项
System.Windows.Forms.MenuItem open = new System.Windows.Forms.MenuItem("打开");
open.Click += new EventHandler(Show);
//退出菜单项
System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem("退出");
exit.Click += new EventHandler(Close);
//关联托盘控件
System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] { open, exit };
notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen);
notifyIcon.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler((o, e) =>
{
if (e.Button == MouseButtons.Left) this.Show(o, e);
});
}

private void Show(object sender, EventArgs e)
{
this.Visibility = Visibility.Visible;
this.ShowInTaskbar = true;
this.WindowState = WindowState.Maximized;
this.notifyIcon.Visible = false;
}

 

private void OnMini(string obj)
{
DispatcherHelper.CheckBeginInvokeOnUI(() => {
this.WindowState = WindowState.Minimized;
this.Visibility = Visibility.Collapsed;
this.ShowInTaskbar = false;
notifyIcon.Visible = true;

notifyIcon.ShowBalloonTip(2000);

});
}