WPF 学习笔记-在WPF下创建托盘图标

首先需要在项目中引用System.Windows.Forms,System.Drawing;

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Windows;
  6 using System.Windows.Controls;
  7 using System.Windows.Data;
  8 using System.Windows.Documents;
  9 using System.Windows.Input;
 10 using System.Windows.Media;
 11 using System.Windows.Media.Imaging;
 12 using System.Windows.Navigation;
 13 
 14 using System.Windows.Shapes;
 15 using System.Drawing;
 16 
 17 namespace WpfApplication1
 18 {
 19     ///
 20     /// Interaction logic for MainWindow.xaml
 21     ///
 22     public partial class MainWindow : Window
 23     {
 24         public MainWindow()
 25         {
 26             InitializeComponent();
 27             InitialTray();
 28         }
 29         private System.Windows.Forms.NotifyIcon notifyIcon = null;
 30         private void InitialTray()
 31         {
 32 
 33             //设置托盘的各个属性
 34             notifyIcon = new System.Windows.Forms.NotifyIcon();
 35             notifyIcon.BalloonTipText = \"程序开始运行\";
 36             notifyIcon.Text = \"托盘图标\";
 37             notifyIcon.Icon = new System.Drawing.Icon(System.Windows.Forms.Application.StartupPath + \"\\\\wp.ico\");
 38             notifyIcon.Visible = true;
 39             notifyIcon.ShowBalloonTip(2000);
 40             notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(notifyIcon_MouseClick);
 41 
 42             //设置菜单项
 43             System.Windows.Forms.MenuItem menu1 = new System.Windows.Forms.MenuItem(\"菜单项1\");
 44             System.Windows.Forms.MenuItem menu2 = new System.Windows.Forms.MenuItem(\"菜单项2\");
 45             System.Windows.Forms.MenuItem menu = new System.Windows.Forms.MenuItem(\"菜单\", new System.Windows.Forms.MenuItem[] { menu1 , menu2 });
 46 
 47             //退出菜单项
 48             System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem(\"exit\");
 49             exit.Click += new EventHandler(exit_Click);
 50 
 51             //关联托盘控件
 52             System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] { menu , exit };
 53             notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen);
 54 
 55             //窗体状态改变时候触发
 56             this.StateChanged += new EventHandler(SysTray_StateChanged);
 57         }
 58         ///
 59         /// 窗体状态改变时候触发
 60         ///
 61         ///
 62 
 63         ///
 64 
 65         private void SysTray_StateChanged(object sender, EventArgs e)
 66         {
 67             if (this.WindowState == WindowState.Minimized)
 68             {
 69                 this.Visibility = Visibility.Hidden;
 70             }
 71         }
 72 
 73         ///
 74         /// 退出选项
 75         ///
 76         ///
 77 
 78         ///
 79 
 80         private void exit_Click(object sender, EventArgs e)
 81         {
 82             if (System.Windows.MessageBox.Show(\"确定要关闭吗?\",
 83                                                \"退出\",
 84                                                 MessageBoxButton.YesNo,
 85                                                 MessageBoxImage.Question,
 86                                                 MessageBoxResult.No) == MessageBoxResult.Yes)
 87             {
 88                 notifyIcon.Dispose();
 89                 System.Windows.Application.Current.Shutdown();
 90             }
 91         }
 92 
 93         ///
 94         /// 鼠标单击
 95         ///
 96         ///
 97 
 98         ///
 99 
100         private void notifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
101         {
102             if (e.Button == System.Windows.Forms.MouseButtons.Left)
103             {
104                 if (this.Visibility == Visibility.Visible)
105                 {
106                     this.Visibility = Visibility.Hidden;
107                 }
108                 else
109                 {
110                     this.Visibility = Visibility.Visible;
111                     this.Activate();
112                 }
113             }
114         }
115     }
116 }

以上代码并非用户控件代码,只需加在主窗体中即可。

原帖地址:http://www.cnblogs.com/hayywcy/archive/2011/11/29/2267515.html

原文地址:https://www.cnblogs.com/lyghost/p/2759257.html