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();
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM