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>
        public WindowNotifyIconHelper(System.Windows.Window window, string text)
        {
            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 };
            notifyIcon.ContextMenu = new ContextMenu(childen);


            //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);
            Application.ApplicationExit += Application_ApplicationExit;

        }

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


        /// <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;
                notifyIcon.Dispose();
                //System.Windows.Application.Current.Shutdown();
                //Application.Exit();
                System.Environment.Exit(0); //这个是不会触发 ApplicationExit, 但可以把全部线程清除掉
            }
        }
        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)
            //{

            //}
            //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;
        }


    }
}
