WindowNotifyIconHelper.cs 4.08 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace FLY.AppHelper
{
    /// <summary>
    /// 通知区 图标生成辅助工具
    /// </summary>
    public class WindowNotifyIconHelper
    {

        System.Windows.WindowState ws;
        System.Windows.WindowState wsl;
        System.Windows.Forms.NotifyIcon notifyIcon;
        System.Windows.Window window;
        /// <summary>
        ///  
        /// </summary>
        /// <param name="window">窗体</param>
        /// <param name="text">当鼠标指针停留在通知区域图标上时显示的工具提示文本</param>
潘栩锋's avatar
潘栩锋 committed
25
        public WindowNotifyIconHelper(System.Windows.Window window, string text)
潘栩锋's avatar
潘栩锋 committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
        {
            notifyIcon = new System.Windows.Forms.NotifyIcon();
            //this.notifyIcon.BalloonTipText = "Hello, NotifyIcon!";
            notifyIcon.Text = text;
            notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);
            notifyIcon.Visible = true;

            notifyIcon.MouseDoubleClick += OnNotifyIconDoubleClick;

            //退出菜单项  
            MenuItem exit = new MenuItem("退出");
            exit.Click += new EventHandler(exit_Click);

            //关联托盘控件  
            MenuItem[] childen = new MenuItem[] { exit };
潘栩锋's avatar
潘栩锋 committed
41
            notifyIcon.ContextMenu = new ContextMenu(childen);
潘栩锋's avatar
潘栩锋 committed
42 43 44 45 46 47 48 49


            //this.notifyIcon.ShowBalloonTip(1000);
            this.window = window;

            wsl = this.window.WindowState;
            this.window.StateChanged += new EventHandler(window_StateChanged);
            this.window.Closing += new System.ComponentModel.CancelEventHandler(window_Closing);
潘栩锋's avatar
潘栩锋 committed
50 51 52 53 54 55 56 57
            Application.ApplicationExit += Application_ApplicationExit;

        }

        private void Application_ApplicationExit(object sender, EventArgs e)
        {
            notifyIcon.Dispose();
            //System.Environment.Exit(0); //这个是不会触发 ApplicationExit, 但可以把全部线程清除掉
潘栩锋's avatar
潘栩锋 committed
58
        }
潘栩锋's avatar
潘栩锋 committed
59

潘栩锋's avatar
潘栩锋 committed
60 61 62 63 64 65 66 67 68 69 70 71

        /// <summary>  
        /// 退出选项  
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>  
        private void exit_Click(object sender, EventArgs e)
        {
            System.Windows.MessageBoxResult r = System.Windows.MessageBox.Show("是否真的要退出程序??", "警告", System.Windows.MessageBoxButton.YesNo, System.Windows.MessageBoxImage.Warning, System.Windows.MessageBoxResult.No);
            if (r == System.Windows.MessageBoxResult.Yes)
            {
                //notifyIcon.Visible = false;
潘栩锋's avatar
潘栩锋 committed
72
                //notifyIcon.Dispose();
潘栩锋's avatar
潘栩锋 committed
73
                //System.Windows.Application.Current.Shutdown();
潘栩锋's avatar
潘栩锋 committed
74 75 76
                //Application.Exit();
                notifyIcon.Dispose();
                System.Environment.Exit(0); //这个是不会触发 ApplicationExit, 但可以把全部线程清除掉
潘栩锋's avatar
潘栩锋 committed
77
            }
潘栩锋's avatar
潘栩锋 committed
78
        }
潘栩锋's avatar
潘栩锋 committed
79 80 81 82 83 84 85 86
        void window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            //this.notifyIcon.Visible = false;
            e.Cancel = true;
            this.window.WindowState = System.Windows.WindowState.Minimized;
            //System.Windows.MessageBoxResult r = System.Windows.MessageBox.Show("是否真的要退出程序??", "警告", System.Windows.MessageBoxButton.YesNo, System.Windows.MessageBoxImage.Warning, System.Windows.MessageBoxResult.No);
            //if (r == System.Windows.MessageBoxResult.Yes)
            //{
潘栩锋's avatar
潘栩锋 committed
87

潘栩锋's avatar
潘栩锋 committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
            //}
            //else
            //{
            //    e.Cancel = true;
            //    notifyIcon.Visible = true;
            //}
        }

        void window_StateChanged(object sender, EventArgs e)
        {
            ws = window.WindowState;
            if (ws == System.Windows.WindowState.Minimized)
            {
                window.Hide();
            }
        }
        private void OnNotifyIconDoubleClick(object sender, EventArgs e)
        {
            window.Show();
            window.WindowState = wsl;
            //notifyIcon.Visible = false;
        }


    }
}