1. 程式人生 > >WPF應用最小化至系統托盤運行

WPF應用最小化至系統托盤運行

ray code pla view chan http link dde left

原文:WPF應用最小化至系統托盤運行

版權聲明:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/lordwish/article/details/51788897

WPF應用要實現最小化至系統托盤運行需要使用WinForm中的方法。


using System.Windows.Forms;
namespace ServerApp
{
    /// <summary>
    /// MainWindow.xaml 的交互邏輯
    /// </summary>
    public partial class
MainWindow :
Window { private NotifyIcon _notifyIcon = null; public MainWindow() { InitializeComponent(); InitialTray(); //最小化至托盤 } #region 最小化系統托盤 private void InitialTray() { //隱藏主窗體 this.Visibility = Visibility.Hidden; //設置托盤的各個屬性 _notifyIcon = new NotifyIcon(); _notifyIcon.BalloonTipText = "服務運行中..."
;//托盤氣泡顯示內容 _notifyIcon.Text = "ServerApp"; _notifyIcon.Visible = true;//托盤按鈕是否可見 _notifyIcon.Icon = new Icon(@"logo.ico");//托盤中顯示的圖標 _notifyIcon.ShowBalloonTip(2000);//托盤氣泡顯示時間 _notifyIcon.MouseDoubleClick += notifyIcon_MouseDoubleClick; //窗體狀態改變時觸發 this.StateChanged += MainWindow_StateChanged; } #endregion
#region 窗口狀態改變 private void MainWindow_StateChanged(object sender, EventArgs e) { if (this.WindowState == WindowState.Minimized) { this.Visibility = Visibility.Hidden; } } #endregion #region 托盤圖標鼠標單擊事件 private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { if (this.Visibility == Visibility.Visible) { this.Visibility = Visibility.Hidden; } else { this.Visibility = Visibility.Visible; this.Activate(); } } } #endregion } }

WPF應用最小化至系統托盤運行