• WPF:通知区域托盘显示且不显示任务栏图标


    using System.Windows.Forms;
    Demo

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        InitialTray();
    }
    
    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        WSCommFunc.WriteLog(_logFile, string.Format("Window_Closing...."));
        if (_notifyIcon != null)
        {
            _notifyIcon.Visible = false;
            _notifyIcon.Dispose();
            _notifyIcon = null;
        }
        System.Environment.Exit(0);
    }
    

    NotifyIcon _notifyIcon;
    private void InitialTray()
    {
        _notifyIcon = new NotifyIcon();
        // 当光标放在上面的提示文本
        _notifyIcon.Text = "自动升级工具";
        // 通知区域是否显示图标
        _notifyIcon.Visible = true;
        // 不显示任务栏按钮
        ShowInTaskbar = false;
        // 读取程序图标,来作为托盘图标
        _notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);
    
        MenuItem open = new MenuItem("显示");
        open.Click += Open_Click;
        MenuItem hide = new MenuItem("隐藏");
        hide.Click += Hide_Click;
        _notifyIcon.ContextMenu = new ContextMenu(new MenuItem[] { open, hide });
        _notifyIcon.MouseDoubleClick += _notifyIcon_MouseDoubleClick;
    }
    
    private void _notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            this.Open_Click(sender, e);
        }
    }
    
    private void Hide_Click(object sender, EventArgs e)
    {
        this.ShowInTaskbar = false;
        this.Visibility = Visibility.Hidden;
    }
    
    private void Open_Click(object sender, EventArgs e)
    {
        this.Visibility = Visibility.Visible;
        this.ShowInTaskbar = true;
        this.Activate();
    }
    
  • 相关阅读:
    NOIP2018 模拟赛(二十二)雅礼NOI
    浅谈左偏树在OI中的应用
    HDU3062&&HDU1814
    2-SAT超入门讲解
    bitset常用用法&&简单题分析
    NOIp2014提高组初赛错题简析
    2018十月刷题列表
    BZOJ 4804: 欧拉心算
    Luogu P2568 GCD
    Luogu P4137 Rmq Problem / mex
  • 原文地址:https://www.cnblogs.com/wesson2019-blog/p/12524964.html
Copyright © 2020-2023  润新知