天天看点

WPF ,利用Winform库中的NotifyIcon实现托盘小程序

   运行界面如下所示:

WPF ,利用Winform库中的NotifyIcon实现托盘小程序
WPF ,利用Winform库中的NotifyIcon实现托盘小程序

       图1                                             图2

       代码很少,如下所示:      

using System;
using System.Windows;
using System.Windows.Forms;
using System.Drawing;

namespace Royen
{
    public partial class SysTray : Window
    {
        private NotifyIcon notifyIcon=null;

        public SysTray()
        {
            InitializeComponent();

            InitialTray();
        }

        private void InitialTray()
        {
            //隐藏主窗体
            this.Visibility = Visibility.Hidden;
                
            //设置托盘的各个属性
            notifyIcon = new NotifyIcon();
            notifyIcon.BalloonTipText = "systray runnning...";         
            notifyIcon.Text = "systray";
            notifyIcon.Icon = new System.Drawing.Icon("http://www.cnblogs.com/res/spring.ico");
            notifyIcon.Visible = true;
            notifyIcon.ShowBalloonTip(2000);            
            notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(notifyIcon_MouseClick);
            
            //设置菜单项
            MenuItem setting1 = new MenuItem("setting1");
            MenuItem setting2 = new MenuItem("setting2");
            MenuItem setting = new MenuItem("setting", new MenuItem[]{setting1,setting2});

            //帮助选项
            MenuItem help = new MenuItem("help");

            //关于选项
            MenuItem about = new MenuItem("about");

            //退出菜单项
            MenuItem exit = new MenuItem("exit");
            exit.Click += new EventHandler(exit_Click);

            //关联托盘控件
            MenuItem[] childen = new MenuItem[] {setting,help,about,exit};
            notifyIcon.ContextMenu = new ContextMenu(childen);

            //窗体状态改变时候触发
            this.StateChanged += new EventHandler(SysTray_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 == 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 SysTray_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();
            }
        }
    }
}
           

继续阅读