wpf 托盘

 

代码网上有很多,具体如下:

记得添加引用:

System.Drawing
System.Windows.Forms
 /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private System.Windows.Forms.NotifyIcon notifyIcon = null;
        public MainWindow()
        {
            InitializeComponent();
            InitialNotifyIcon();
        }
        private void InitialNotifyIcon()
        {
            this.Visibility = Visibility.Hidden;

            notifyIcon = new System.Windows.Forms.NotifyIcon();
            notifyIcon.BalloonTipText = "边界数据传输已启动";
            notifyIcon.Text = "边界数据传输";

            notifyIcon.Icon = new System.Drawing.Icon(Application.GetResourceStream(new Uri("/BDT.Monitor;component/Resources/img/favicon.ico", UriKind.Relative)).Stream);
            notifyIcon.Visible = true;
            notifyIcon.ShowBalloonTip(2000);
            notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(notifyIcon_MouseClick);

            //添加菜单项
            System.Windows.Forms.MenuItem setting = new System.Windows.Forms.MenuItem("监控");
            System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem("退出");
            exit.Click += new EventHandler(exit_Click);

            //添加关联
            System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] { setting, exit };
            notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen);

            this.StateChanged += new EventHandler(Win_StateChanged);
        }

        /// <summary>
        /// 鼠标单击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        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();
                }
            }
        }

        /// <summary>
        /// 窗体状态改变时候触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Win_StateChanged(object sender, EventArgs e)
        {
            if (this.WindowState == WindowState.Minimized)
            {
                this.Visibility = Visibility.Hidden;
            }
        }


        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void exit_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();
            }
        }
    }

 

注意这段中引用的是资源文件,图片会打包到程序里面,不用带着图片到处跑。

notifyIcon.Icon = new System.Drawing.Icon(Application.GetResourceStream(new Uri("/BDT.Monitor;component/Resources/img/favicon.ico", UriKind.Relative)).Stream);

 

posted @ 2016-01-19 16:38  Fady  阅读(363)  评论(0编辑  收藏  举报