Loading

[WPF]创建系统栏小图标

隐藏窗体任务栏图标

/// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            this.ShowInTaskbar = false;
        }
    }

 

 

系统图标类 

  public class SystemTray
    {
        public static SystemTray Instance;
        /// <summary>
        /// 静态构造函数,在类第一次被创建或者静态成员被调用的时候调用
        /// </summary>
        static SystemTray()
        {
            Instance=new SystemTray();
        }

        public NotifyIcon Ni { get; set; }

        private SystemTray()
        {
            Ni = new System.Windows.Forms.NotifyIcon();
            Stream iconStream = Application.GetResourceStream(new Uri("pack://application:,,,/StikyNotes;component/MyLogo.ico")).Stream;
            Ni.Icon = new System.Drawing.Icon(iconStream);
            Ni.Visible = true;
            Ni.MouseClick += Ni_MouseClick;
        }

        private void Ni_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                System.Windows.Controls.ContextMenu NotifyIconMenu = (System.Windows.Controls.ContextMenu)App.Current.FindResource("NotifyIconMenu");
                NotifyIconMenu.IsOpen = true;
                App.Current.MainWindow?.Activate();
            }

            if (e.Button == MouseButtons.Left)
            {
                var wins=WindowsManager.Instance.Windows;
                foreach (var i in wins)
                {
                    i.Activate();
                }
            }
        }

        /// <summary>
        /// 销毁系统托盘图标的资源
        /// </summary>
        public void DisposeNotifyIcon()
        {
            Ni?.Dispose();
        }
    }

 

在App.xaml中:

 public partial class App : Application
    {
        System.Threading.Mutex mutex;
        protected override void OnStartup(StartupEventArgs e)
        {
            bool ret;
            mutex = new System.Threading.Mutex(true, "StikyNotesAPP", out ret);

            if (!ret)
            {
                MessageBox.Show("程序已经运行了");
                Environment.Exit(0);
            }

            base.OnStartup(e);
            var systemtray = SystemTray.Instance;
        }

        protected override void OnDeactivated(EventArgs e)
        {
            base.OnDeactivated(e);
            System.Windows.Controls.ContextMenu menu = this.FindResource("NotifyIconMenu") as ContextMenu;
            if (menu.IsOpen == true)
            {
                menu.IsOpen = false;
            }

        }



        /// <summary>
        /// 新建窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuOpen_Click(object sender, RoutedEventArgs e)
        {
            var MainWindow = new MainWindow();
            MainWindow.DataContext = new MainViewModel(MainWindow, new WindowsData());
            WindowsManager.Instance.Windows.Add(MainWindow);
            MainWindow.Show();
        }

        /// <summary>
        /// 窗体退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuExit_Click(object sender, RoutedEventArgs e)
        {
            SystemTray.Instance.DisposeNotifyIcon();
            App.Current.Shutdown();
        }
        /// <summary>
        /// 窗体退出事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnExit(ExitEventArgs e)
        {
            base.OnExit(e);
            XMLHelper.SaveObjAsXml(ProgramData.Instance, "StikyNotesData.xml");
            SystemTray.Instance.DisposeNotifyIcon();

        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            var programData = XMLHelper.DecodeXML<ProgramData>("StikyNotesData.xml");
            if (programData != null)
            {
                var windowsDatas = programData.Datas;
                ProgramData.Instance.IsWindowTopMost = programData.IsWindowTopMost;
                ProgramData.Instance.IsStartUpWithSystem = programData.IsStartUpWithSystem;
                MainWindow MainWindow;
                //有创建过的窗口
                if (windowsDatas.Count > 0)
                {
                    for (int i = 0; i < windowsDatas.Count; i++)
                    {
                        MainWindow = new MainWindow();
                        MainWindow.DataContext = new MainViewModel(MainWindow, windowsDatas[i]);
                        MainWindow.Show();
                    }
                }
                else//以前的窗口都被删掉了
                {
                    MainWindow = new MainWindow();
                    MainWindow.DataContext = new MainViewModel(MainWindow, new WindowsData());
                    MainWindow.Show();
                }
            }
            //没有创建过的窗口
            else
            {
                var MainWindow = new MainWindow();
                MainWindow.DataContext = new MainViewModel(MainWindow, new WindowsData());
                MainWindow.Show();
            }
        }
    }

App.xaml中
 

 <ContextMenu x:Key="NotifyIconMenu" StaysOpen="False">
                <MenuItem
                    Name="MenuOpen"
                    Click="MenuOpen_Click"
                    Header="新建" />
                <MenuItem
                    Name="MenuExit"
                    Click="MenuExit_Click"
                    Header="退出" />
            </ContextMenu>

问题

 记得在退出的时候要释放使用的图标资源,否则在程序退出之后,系统栏中还会存在小图标,要使用下面这条语句:

SystemTray.Instance.DisposeNotifyIcon();

 

效果

posted @ 2019-01-29 10:56  李正浩  阅读(718)  评论(0编辑  收藏  举报