FrmBase.cs 3.3 KB
using Common;
using System;
using System.Windows.Forms;

namespace UIControl.Forms
{
    public partial class FrmBase : Form
    {
        public FrmBase(string name = "App")
        {
            InitializeComponent();
            InitMiniIcon(name);
        }
        public FrmBase()
        {
            InitializeComponent();
        }
        #region 托盘
        private bool exit = false;
        private NotifyIcon notify;
        private ContextMenuStrip notifyMenu;
        private void InitMiniIcon(string name)
        {
            //托盘菜单
            notifyMenu = new ContextMenuStrip();
            ToolStripMenuItem itemShow = new ToolStripMenuItem("显示(&S)");
            itemShow.Click += ItemShow_Click;
            ToolStripMenuItem itemExit = new ToolStripMenuItem("退出(&X)");
            itemExit.Click += ItemExit_Click;
            notifyMenu.Items.Add(itemShow);
            notifyMenu.Items.Add(itemExit);

            //托盘控件
            notify = new NotifyIcon { Icon = Icon, Visible = true, ContextMenuStrip = notifyMenu, Text = name };
            notify.MouseDoubleClick += Notify_MouseDoubleClick; ;
            Opacity = 100;
            Visible = false;
            notify.Visible = true;
            ShowInTaskbar = false;
            Cursor = Cursors.Default;
        }

        private void Notify_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            //Show();
            show();
            if (WindowState == FormWindowState.Minimized)
                WindowState = FormWindowState.Normal;
        }

        private void ItemExit_Click(object sender, EventArgs e)
        {
            //notify.Dispose();
            //exit = true;
            //Close();
            ExitApp();
        }

        private void ItemShow_Click(object sender, EventArgs e)
        {
            show();
        }
        private void show()
        {
            Opacity = 100;
            Visible = true;
            notify.Visible = false;
            ShowInTaskbar = true;
        }
        #endregion
        /// <summary>
        /// 关闭窗体
        /// </summary>
        protected void FrmClose()
        {
            Close();
        }
        /// <summary>
        /// 关闭所有线程资源
        /// </summary>
        protected virtual void CloseAllThreads()
        {

        }
        /// <summary>
        /// 隐藏窗体
        /// </summary>
        protected void HideForm()
        {
            Opacity = 0;
            Visible = false;
            notify.Visible = true;
            ShowInTaskbar = false;
            GC.Collect();
        }
        /// <summary>
        /// 退出软件
        /// </summary>
        private void ExitApp()
        {
            LogUtil.Info("点击:退出系统");
            DialogResult result = MessageBox.Show("是否确定退出客户端?", "提示", MessageBoxButtons.YesNo);
            if (result.Equals(DialogResult.Yes))
            {
                try
                {
                    CloseAllThreads();
                    System.Diagnostics.Process.GetCurrentProcess().Kill();
                    Close();
                    Environment.Exit(Environment.ExitCode);
                }
                catch (Exception ex)
                {
                    LogUtil.Error("退出软件出错:", ex);
                }
            }
        }
    }
}