WPF 在关闭程序了之后,托盘图标依然存在,请问怎么解决?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF 在关闭程序了之后,托盘图标依然存在,请问怎么解决?相关的知识,希望对你有一定的参考价值。
我做的托盘是按照这个大神的方法来做的:http://www.cnblogs.com/hayywcy/archive/2011/11/29/2267515.html
但是我关闭程序之后,托盘图标依然在,只有鼠标移过去的时候才会消失!
我关闭程序的方法是System.Windows.Application.Current.Shutdown();
我想知道。怎么样才能在关闭程序之后,图标也能同时消失?
在窗口的“将被销毁”事件中加入以下代码:
置托盘图标(,)'里面不填参数,就表示将托盘图标去掉。
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
notifyIcon.Visible = false; 追问
notifyIcon.Visible = false; 好像只要这句话就行了。
置托盘图标(,)'里面不填参数。。。。这样运行是错的!还是我用的方法不对?
我是这样写的
private System.Windows.Forms.NotifyIcon notifyIcon = null;
notifyIcon.Icon = new System.Drawing.Icon(".\\托盘图标.ico");
还是不行啊!不管用System.Windows.Application.Current.Shutdown();还是Close()
效果都一样!
你是怎麼关闭程序的?你VS调至调试还是点击X?
追问我用的是我自己写的方法关闭的
private void 关闭(object sender, System.Windows.Input.MouseButtonEventArgs e)
托盘1.Close();
this.Close();
WPF没用过,不够一般winform一般都是,程序没有正常退出会这样,用VS调试的时候,直接停止调试也是不整体退出程序。
追问好吧!谢谢了!
WPF之托盘图标的设定
原文:WPF之托盘图标的设定首先需要在项目中引用System.Windows.Forms,System.Drawing;
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Drawing; namespace WpfApplication1 { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); InitialTray(); } private System.Windows.Forms.NotifyIcon notifyIcon = null; private void InitialTray() { //设置托盘的各个属性 notifyIcon = new System.Windows.Forms.NotifyIcon(); notifyIcon.BalloonTipText = "程序开始运行"; notifyIcon.Text = "托盘图标"; notifyIcon.Icon = new System.Drawing.Icon(System.Windows.Forms.Application.StartupPath + "\\\\wp.ico"); notifyIcon.Visible = true; notifyIcon.ShowBalloonTip(2000); notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(notifyIcon_MouseClick); //设置菜单项 System.Windows.Forms.MenuItem menu1 = new System.Windows.Forms.MenuItem("菜单项1"); System.Windows.Forms.MenuItem menu2 = new System.Windows.Forms.MenuItem("菜单项2"); System.Windows.Forms.MenuItem menu = new System.Windows.Forms.MenuItem("菜单", new System.Windows.Forms.MenuItem[] { menu1 , menu2 }); //退出菜单项 System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem("exit"); exit.Click += new EventHandler(exit_Click); //关联托盘控件 System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] { menu , exit }; notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen); //窗体状态改变时候触发 this.StateChanged += new EventHandler(SysTray_StateChanged); } /// /// 窗体状态改变时候触发 /// /// /// private void SysTray_StateChanged(object sender, EventArgs e) { if (this.WindowState == WindowState.Minimized) { this.Visibility = Visibility.Hidden; } } /// /// 退出选项 /// /// /// private void exit_Click(object sender, EventArgs e) { if (System.Windows.MessageBox.Show("确定要关闭吗?", "退出", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes) { notifyIcon.Dispose(); System.Windows.Application.Current.Shutdown(); } } /// /// 鼠标单击 /// /// /// private void notifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e) { if (e.Button == System.Windows.Forms.MouseButtons.Left) { if (this.Visibility == Visibility.Visible) { this.Visibility = Visibility.Hidden; } else { this.Visibility = Visibility.Visible; this.Activate(); } } } } }
以上代码并非用户控件代码,只需加在主窗体中即可。
以上是关于WPF 在关闭程序了之后,托盘图标依然存在,请问怎么解决?的主要内容,如果未能解决你的问题,请参考以下文章