wpf下使用NotifyIcon
以前在winForm下使用过NotifyIcon,到wpf找不到了,在wpf下还是直接用WinForm里的那个NotifyIcon实现最小到系统托盘
定义一个NotifyIcon成员 :
NotifyIcon notifyIcon = null;
WindowState ws; //记录窗体状态
加载窗体时初始化NotifyIcon:
this.notifyIcon = new NotifyIcon(); this.notifyIcon.BalloonTipText = "Hello, 文件监视器"; //设置程序启动时显示的文本 this.notifyIcon.Text = "文件监视器";//最小化到托盘时,鼠标点击时显示的文本 this.notifyIcon.Icon = new System.Drawing.Icon(@"b.ico");//程序图标 this.notifyIcon.Visible = true; notifyIcon.MouseDoubleClick += OnNotifyIconDoubleClick; this.notifyIcon.ShowBalloonTip(1000);
同时添加右键菜单:
System.Windows.Forms.MenuItem m1 = new System.Windows.Forms.MenuItem("open"); m1.Click += m1_Click; System.Windows.Forms.MenuItem m2 = new System.Windows.Forms.MenuItem("close"); m2.Click += m2_Click; System.Windows.Forms.MenuItem[] m = new System.Windows.Forms.MenuItem[] { m1, m2 }; this.notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(m);
事件为:
void m2_Click(object sender, EventArgs e) { if (System.Windows.MessageBox.Show("sure to exit?", "application", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes) { System.Windows.Application.Current.Shutdown(); } } void m1_Click(object sender, EventArgs e) { this.Show(); this.Activate(); }
对窗体添加事件:
this.SizeChanged += MainWindow_SizeChanged; this.Closing += MainWindow_Closing;
void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e) { if (ws == WindowState.Minimized) { this.Hide(); this.notifyIcon.Visible = true; } } void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) { e.Cancel = true; this.WindowState = WindowState.Minimized; ws = WindowState.Minimized; this.notifyIcon.Visible = true; this.notifyIcon.ShowBalloonTip(30, "注意", "大家好,这是一个事例", ToolTipIcon.Info); }
双击拖盘弹出窗体
private void OnNotifyIconDoubleClick(object sender, EventArgs e) { if (ws == WindowState.Minimized) { this.WindowState = WindowState.Normal; this.notifyIcon.Visible = false; } }
好,大概这些知识够用了,最后来一个完整的代码:
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); icon(); //保证窗体显示在上方。 wsl = WindowState; this.SizeChanged += MainWindow_SizeChanged; this.Closing += MainWindow_Closing; } void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e) { if (ws == WindowState.Minimized) { this.Hide(); this.notifyIcon.Visible = true; } } void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) { e.Cancel = true; this.WindowState = WindowState.Minimized; ws = WindowState.Minimized; this.notifyIcon.Visible = true; this.notifyIcon.ShowBalloonTip(30, "注意", "大家好,这是一个事例", ToolTipIcon.Info); } WindowState ws; WindowState wsl; NotifyIcon notifyIcon = null; private void icon() { this.notifyIcon = new NotifyIcon(); this.notifyIcon.BalloonTipText = "Hello, 文件监视器"; //设置程序启动时显示的文本 this.notifyIcon.Text = "文件监视器";//最小化到托盘时,鼠标点击时显示的文本 this.notifyIcon.Icon = new System.Drawing.Icon(@"b.ico");//程序图标 this.notifyIcon.Visible = true; notifyIcon.MouseDoubleClick += OnNotifyIconDoubleClick; this.notifyIcon.ShowBalloonTip(1000); System.Windows.Forms.MenuItem m1 = new System.Windows.Forms.MenuItem("open"); m1.Click += m1_Click; System.Windows.Forms.MenuItem m2 = new System.Windows.Forms.MenuItem("close"); m2.Click += m2_Click; System.Windows.Forms.MenuItem[] m = new System.Windows.Forms.MenuItem[] { m1, m2 }; this.notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(m); } void m2_Click(object sender, EventArgs e) { if (System.Windows.MessageBox.Show("sure to exit?", "application", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes) { System.Windows.Application.Current.Shutdown(); } } void m1_Click(object sender, EventArgs e) { this.Show(); this.Activate(); } private void OnNotifyIconDoubleClick(object sender, EventArgs e) { if (ws == WindowState.Minimized) { this.WindowState = WindowState.Normal; this.notifyIcon.Visible = false; } } private void Window_StateChanged(object sender, EventArgs e) { ws = WindowState; if (ws == WindowState.Minimized) { this.notifyIcon.Visible = true; } } private void Button_Click(object sender, RoutedEventArgs e) { this.notifyIcon.BalloonTipText = "有新信息"; } }