1. 程式人生 > >WPF 學習筆記-在WPF下創建托盤圖標

WPF 學習筆記-在WPF下創建托盤圖標

mage contex zed ima .data tro tar rac form

原文:WPF 學習筆記-在WPF下創建托盤圖標

首先需要在項目中引用System.Windows.Forms,System.Drawing;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;

namespace WpfApplication1
{
    ///
    /// Interaction logic for MainWindow.xaml
    ///
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            InitialTray();
        }
        private System.Windows.Forms.NotifyIcon notifyIcon = null;
        private void InitialTray()
        {

            //設置托盤的各個屬性
            notifyIcon = new System.Windows.Forms.NotifyIcon();
            notifyIcon.BalloonTipText = \"程序開始運行\";
            notifyIcon.Text = \"托盤圖標\";
            notifyIcon.Icon = new System.Drawing.Icon(System.Windows.Forms.Application.StartupPath + \"\\\\wp.ico\");
            notifyIcon.Visible = true;
            notifyIcon.ShowBalloonTip(2000);
            notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(notifyIcon_MouseClick);

            //設置菜單項
            System.Windows.Forms.MenuItem menu1 = new System.Windows.Forms.MenuItem(\"菜單項1\");
            System.Windows.Forms.MenuItem menu2 = new System.Windows.Forms.MenuItem(\"菜單項2\");
            System.Windows.Forms.MenuItem menu = new System.Windows.Forms.MenuItem(\"菜單\", new System.Windows.Forms.MenuItem[] { menu1 , menu2 });

            //退出菜單項
            System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem(\"exit\");
            exit.Click += new EventHandler(exit_Click);

            //關聯托盤控件
            System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] { menu , exit };
            notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen);

            //窗體狀態改變時候觸發
            this.StateChanged += new EventHandler(SysTray_StateChanged);
        }
        ///
        /// 窗體狀態改變時候觸發
        ///
        ///

        ///

        private void SysTray_StateChanged(object sender, EventArgs e)
        {
            if (this.WindowState == WindowState.Minimized)
            {
                this.Visibility = Visibility.Hidden;
            }
        }

        ///
        /// 退出選項
        ///
        ///

        ///

        private void exit_Click(object sender, EventArgs e)
        {
            if (System.Windows.MessageBox.Show(\"確定要關閉嗎?\",
                                               \"退出\",
                                                MessageBoxButton.YesNo,
                                                MessageBoxImage.Question,
                                                MessageBoxResult.No) == MessageBoxResult.Yes)
            {
                notifyIcon.Dispose();
                System.Windows.Application.Current.Shutdown();
            }
        }

        ///
        /// 鼠標單擊
        ///
        ///

        ///

        private void notifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                if (this.Visibility == Visibility.Visible)
                {
                    this.Visibility = Visibility.Hidden;
                }
                else
                {
                    this.Visibility = Visibility.Visible;
                    this.Activate();
                }
            }
        }
    }
}

以上代碼並非用戶控件代碼,只需加在主窗體中即可。

WPF 學習筆記-在WPF下創建托盤圖標