所谓的潇洒

导航

WPF实现通知区图标

MainWindow.xaml

Title="同步工具" Icon="myIcon.ico" Loaded="Window_Loaded" StateChanged="Window_StateChanged" Closed="Window_Closed"

MainWindow.cs

        System.Windows.Forms.NotifyIcon _icon = new System.Windows.Forms.NotifyIcon();

     private void Window_Loaded(object sender, RoutedEventArgs e)
        {
                _icon.Visible = true;
                _icon.Text = "同步工具";
                _icon.Click += _icon_Click;
                if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
                {
                    _icon.Icon = GetImageSource(this.Icon);
                }
        }

        private static System.Drawing.Icon GetImageSource(ImageSource icon)
        {
            if (icon == null)
            {
                return null;
            }
            Uri iconUri = new Uri(icon.ToString());
            return new System.Drawing.Icon(System.Windows.Application.GetResourceStream(iconUri).Stream);
        }

        private void _icon_Click(object sender, EventArgs e)
        {            
            this.WindowState = WindowState.Normal;
        }

        private void Window_StateChanged(object sender, EventArgs e)
        {
            if (this.WindowState == WindowState.Minimized) ShowInTaskbar = false;
            else ShowInTaskbar = true;
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            _icon.Visible = false;
        }

 

posted on 2021-11-12 10:24  所谓的潇洒  阅读(188)  评论(0编辑  收藏  举报