• WPF最小到系统托盘


    让WPF应用最小到系统托盘?可以调用System.Windows.Forms.NotifyIcon来实现,下面是示例代码:

    View Code
    public partial class MainWindow : Window
        {
            private NotifyIcon notifyIcon;
            public MainWindow()
            {
                InitializeComponent();
    
                this.notifyIcon = new NotifyIcon();
                this.notifyIcon.BalloonTipText = "系统监控中... ...";
                this.notifyIcon.ShowBalloonTip(2000);
                this.notifyIcon.Text = "系统监控中... ...";
                this.notifyIcon.Icon = new System.Drawing.Icon(@"AppIcon.ico");
                this.notifyIcon.Visible = true;
                //打开菜单项
                System.Windows.Forms.MenuItem open = new System.Windows.Forms.MenuItem("Open");
                open.Click += new EventHandler(Show);
                //退出菜单项
                System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem("Exit");
                exit.Click += new EventHandler(Close);
                //关联托盘控件
                System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] { open, exit };
                notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen);
    
                this.notifyIcon.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler((o, e) =>
                {
                    if (e.Button == MouseButtons.Left) this.Show(o, e);
                });
            }
    
            private void Show(object sender, EventArgs e)
            {
                this.Visibility = System.Windows.Visibility.Visible;
                this.ShowInTaskbar = true;
                this.Activate();
            }
    
            private void Hide(object sender, EventArgs e)
            {
                this.ShowInTaskbar = false;
                this.Visibility = System.Windows.Visibility.Hidden;
            }
    
            private void Close(object sender, EventArgs e)
            {
                System.Windows.Application.Current.Shutdown();
            }
        }

    运行时发现,程序一定要能找到ICON,否则会报错,并且ICON还没包含到程序中,需要一个额外的ICON来做托盘图标。当然这个都是能解决的:

    this.notifyIcon.Icon = new System.Drawing.Icon(@"AppIcon.ico");

    将以上一句替换成下面内容,意思就是读取程序图标,来作为托盘图标

    this.notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);

    运行,搞定,这样你的程序就不用拖着一个ICON文件当累赘了。

  • 相关阅读:
    C++Primer 中间Sales_items.h头文件
    2014最不受欢迎10编程语言种
    解决ubuntu 14.04删ibus导致系统设置项目的损失后,,退出关机问题是不正常的
    会计翻译成英文
    Delphi 使用 Format格式话字符串的用法
    浅谈暂估应付账款的会计处理
    Delphi TcxTreelist 设置scrollbars 不起作用的原因
    Delphi 调试 通过BreakPoint
    折现率”的公式
    考会计证 需要的科目
  • 原文地址:https://www.cnblogs.com/ke10/p/NotifyIcon.html
Copyright © 2020-2023  润新知