wpf系统动态托盘

13年的代码,先看效果:

wpf系统动态托盘

代码已上传: https://download.csdn.net/download/henrymoore/5411085

今天下载源码,用vs2019正常编译,运行效果就是上面的gif效果,再贴代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SysIco
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private NotifyIcon notifyIcon = null;
        private Thread th = null;
        private bool IsRunning = false;
        private int oOne = 1;
        public MainWindow()
        {
            InitializeComponent();

            notifyIcon = new NotifyIcon();
            this.notifyIcon.BalloonTipText = "测试窗体\r\n使用人: 刘世全\r\n使用时间:2013-5-19";
            this.notifyIcon.ShowBalloonTip(2000);
            this.notifyIcon.Text = "测试窗体\r\n使用人: 刘世全\r\n使用时间:2013-5-19";
            this.notifyIcon.Icon = new System.Drawing.Icon(@"Images\1.ico");
            this.notifyIcon.Visible = true;

            //打开菜单项
            System.Windows.Forms.MenuItem open = new System.Windows.Forms.MenuItem("Open");
            open.Click += open_Click;

            //退出菜单项
            System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem("Exit");
            exit.Click += exit_Click;

            //关联托盘控件
            System.Windows.Forms.MenuItem[] children = new System.Windows.Forms.MenuItem[] { open, exit };
            notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(children);

            this.notifyIcon.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler((o, e) =>
            {
                if (e.Button == MouseButtons.Left)
                {
                    this.open_Click(o, e);
                }
            });
        }

        void open_Click(object sender, EventArgs e)
        {
            this.Visibility = System.Windows.Visibility.Visible;
            this.ShowInTaskbar = true;
            this.Activate();
        }

        void exit_Click(object sender, EventArgs e)
        {
            //this.showintaskbar = false;
            //this.visibility = system.windows.visibility.hidden;
            System.Windows.Application.Current.Shutdown();
        }
        /// <summary>
        /// 转换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            //this.notifyIcon.ShowBalloonTip(2000, "测试窗体\r\n使用人: 刘世全\r\n使用时间:2013-5-19", "测试窗体\r\n使用人: 刘世全\r\n使用时间:2013-5-19", ToolTipIcon.Info);
            if (th == null)
            {
                th = new Thread(ChangeIcon);
                th.IsBackground = true;
                th.Start();
            }
            else
            {
                th.Join(0);
                th.Abort();
                th = null;
            }
            IsRunning = !IsRunning;
        }
        private void ChangeIcon()
        {
            while (IsRunning == true)
            {
                this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new Action(delegate
                {
                    if (this.oOne == 1)
                    {
                        this.notifyIcon.Icon = new System.Drawing.Icon(@"Images\2.ico");
                        this.oOne = 0;
                    }
                    else
                    {
                        this.notifyIcon.Icon = new System.Drawing.Icon(@"Images\1.ico");
                        this.oOne = 1;
                    }
                }));
                System.Threading.Thread.Sleep(100);
            }
        }
    }
}

除非注明,文章均由 Dotnet9 整理发布,欢迎转载。

转载请注明:
作者:Dotnet9
链接:https://dotnet9.com/3077.html
来源:Dotnet9
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

发表评论

登录后才能评论