以前在winForm下使用過NotifyIcon,到wpf找不到了,在wpf下還是直接用WinForm里的那個NotifyIcon實現最小到系統托盤
定義一個NotifyIcon成員 :
NotifyIcon notifyIcon = null;
WindowState ws; //記錄窗體狀態
加載窗體時初始化NotifyIcon:
this.notifyIcon = new NotifyIcon(); this.notifyIcon.BalloonTipText = "Hello, 文件監視器"; //設置程序啟動時顯示的文本 this.notifyIcon.Text = "文件監視器";//最小化到托盤時,鼠標點擊時顯示的文本 this.notifyIcon.Icon = new System.Drawing.Icon(@"b.ico");//程序圖標 this.notifyIcon.Visible = true; notifyIcon.MouseDoubleClick += OnNotifyIconDoubleClick; this.notifyIcon.ShowBalloonTip(1000);
同時添加右鍵菜單:
System.Windows.Forms.MenuItem m1 = new System.Windows.Forms.MenuItem("open"); m1.Click += m1_Click; System.Windows.Forms.MenuItem m2 = new System.Windows.Forms.MenuItem("close"); m2.Click += m2_Click; System.Windows.Forms.MenuItem[] m = new System.Windows.Forms.MenuItem[] { m1, m2 }; this.notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(m);
事件為:
void m2_Click(object sender, EventArgs e) { if (System.Windows.MessageBox.Show("sure to exit?", "application", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes) { System.Windows.Application.Current.Shutdown(); } } void m1_Click(object sender, EventArgs e) { this.Show(); this.Activate(); }
對窗體添加事件:
this.SizeChanged += MainWindow_SizeChanged; this.Closing += MainWindow_Closing;
void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e) { if (ws == WindowState.Minimized) { this.Hide(); this.notifyIcon.Visible = true; } } void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) { e.Cancel = true; this.WindowState = WindowState.Minimized; ws = WindowState.Minimized; this.notifyIcon.Visible = true; this.notifyIcon.ShowBalloonTip(30, "注意", "大家好,這是一個事例", ToolTipIcon.Info); }
雙擊拖盤彈出窗體
private void OnNotifyIconDoubleClick(object sender, EventArgs e) { if (ws == WindowState.Minimized) { this.WindowState = WindowState.Normal; this.notifyIcon.Visible = false; } }
好,大概這些知識夠用了,最后來一個完整的代碼:
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); icon(); //保證窗體顯示在上方。 wsl = WindowState; this.SizeChanged += MainWindow_SizeChanged; this.Closing += MainWindow_Closing; } void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e) { if (ws == WindowState.Minimized) { this.Hide(); this.notifyIcon.Visible = true; } } void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) { e.Cancel = true; this.WindowState = WindowState.Minimized; ws = WindowState.Minimized; this.notifyIcon.Visible = true; this.notifyIcon.ShowBalloonTip(30, "注意", "大家好,這是一個事例", ToolTipIcon.Info); } WindowState ws; WindowState wsl; NotifyIcon notifyIcon = null; private void icon() { this.notifyIcon = new NotifyIcon(); this.notifyIcon.BalloonTipText = "Hello, 文件監視器"; //設置程序啟動時顯示的文本 this.notifyIcon.Text = "文件監視器";//最小化到托盤時,鼠標點擊時顯示的文本 this.notifyIcon.Icon = new System.Drawing.Icon(@"b.ico");//程序圖標 this.notifyIcon.Visible = true; notifyIcon.MouseDoubleClick += OnNotifyIconDoubleClick; this.notifyIcon.ShowBalloonTip(1000); System.Windows.Forms.MenuItem m1 = new System.Windows.Forms.MenuItem("open"); m1.Click += m1_Click; System.Windows.Forms.MenuItem m2 = new System.Windows.Forms.MenuItem("close"); m2.Click += m2_Click; System.Windows.Forms.MenuItem[] m = new System.Windows.Forms.MenuItem[] { m1, m2 }; this.notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(m); } void m2_Click(object sender, EventArgs e) { if (System.Windows.MessageBox.Show("sure to exit?", "application", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes) { System.Windows.Application.Current.Shutdown(); } } void m1_Click(object sender, EventArgs e) { this.Show(); this.Activate(); } private void OnNotifyIconDoubleClick(object sender, EventArgs e) { if (ws == WindowState.Minimized) { this.WindowState = WindowState.Normal; this.notifyIcon.Visible = false; } } private void Window_StateChanged(object sender, EventArgs e) { ws = WindowState; if (ws == WindowState.Minimized) { this.notifyIcon.Visible = true; } } private void Button_Click(object sender, RoutedEventArgs e) { this.notifyIcon.BalloonTipText = "有新信息"; } }