FrmStore.cs 12.7 KB
using log4net;
using OnlineStore.Common;
using OnlineStore.DeviceLibrary;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OnlineStore.ACPackingStore
{
    internal partial class FrmStore : Form
    {

        internal static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public static PackingStoreBean store = null;
        private List<TabPage> tabPageList = new List<TabPage>();
        private bool LoadOk = false;
        private System.Timers.Timer startTimer = null;
        internal FrmStore()
        {
            InitializeComponent();
            LogUtil.logBox = this.logBox;
            startTimer = new System.Timers.Timer();
            startTimer.Interval = 1000;
            startTimer.Enabled = false;
            startTimer.AutoReset = false;
        }

      
        private void FrmMain_Load(object sender, EventArgs e)
        {
           FrmBase. GetVersion();
            store = StoreManager.InitStore();
            if (store == null)
            {
                this.Close();
            }
            else
            {
                foreach (AC_BOX_Bean box in store.BoxMap.Values)
                {
                    FrmBox frm = new FrmBox(); 
                    frm.BoxBean = box;
                    AddForm(box.Name, frm); 
                }
            }
            this.Text = ConfigAppSettings.GetValue(Setting_Init.App_Title);
            chbAutoRun.Checked = ConfigAppSettings.GetValue(Setting_Init.App_AutoRun).Equals(1);
            HideForm();
            LoadOk = true;
        }


        private void AddForm(string text, Form form)
        {
            text = text.PadLeft(10, ' ');
            TabPage lineTabPage = new TabPage(text);
            lineTabPage.AutoScroll = true;
           // lineTabPage.Tag = lineBean;
            Panel linePan = new Panel();
            linePan.Dock = DockStyle.Fill;
            linePan.AutoScroll = true;
            lineTabPage.Controls.Add(linePan);
            form.FormBorderStyle = FormBorderStyle.None;
            form.TopLevel = false;
            linePan.Controls.Add(form);
            form.Dock = DockStyle.Fill;
            linePan.Anchor = ((AnchorStyles)((AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Left)));
            form.Anchor = ((AnchorStyles)((AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Left)));
            form.Show();
            tabPageList.Add(lineTabPage);
            tabControl1.Controls.Add(lineTabPage);
        }
        /// <summary>
        /// 隐藏窗口
        /// </summary>
        private void HideForm()
        {
            this.Opacity = 0;
            this.ShowInTaskbar = false;
            this.notifyIcon1.Visible = true;
            this.Hide();
            GC.Collect();
        }
    
        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                this.contextMenuStrip1.Show();
            }
            if (e.Button == MouseButtons.Left)
            {
                this.Visible = true;
                this.WindowState = FormWindowState.Maximized;
                this.ShowInTaskbar = true;
            }
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)//当用户点击窗体右上角X按钮或(Alt + F4)时 发生           
            {
                e.Cancel = true;
                HideForm();
            }
        } 

        private void ExitApp()
        {
            DialogResult result = MessageBox.Show("是否确定退出包装料料仓客户端?", "提示", MessageBoxButtons.YesNo);
            if (result.Equals(DialogResult.Yes))
            {
                //如果料仓还在运行状态,先关闭料仓
                if (!store.storeRunStatus.Equals(StoreRunStatus.Wait))
                {
                    LogUtil.info(LOGGER, "退出程序之前先停止运行 ");
                    store.StopRun();
                }

                IOManager.instance.CloseAllDO();
                IOManager.instance.CloseAllConnection();
                //AIManager.CloseConnect();
                ACServerManager.CloseAllPort();
                //this.Close();
                System.Environment.Exit(System.Environment.ExitCode);
            }
        }
        private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                FrmPwd fw = new FrmPwd(10);
                DialogResult result = fw.ShowDialog();
                if (!result.Equals(DialogResult.OK))
                {
                    LogUtil.info("切换界面显示时,没有正确输入密码");
                    return;
                }

                this.Opacity = 100;
                this.Visible = true;
                this.WindowState = FormWindowState.Maximized;
                this.notifyIcon1.Visible = false;
                this.ShowInTaskbar = true;
            }
            catch (Exception ex)
            {
                LogUtil.error("显示界面出错:" + ex.ToString());
            }
        }

        private void 启动所有料仓AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (store.storeRunStatus != StoreRunStatus.Wait)
            {
                MessageBox.Show(store.Name + "当前状态:" + store.storeRunStatus + ",不能启动!");
                return;
            }
            LogUtil.info(LOGGER, "开始启动");
            startTimer.Interval = 1;
            startTimer.Elapsed += timer_Elapsed;
            startTimer.Enabled = true;
            this.timer1.Start();
        }
        private delegate void ShowFormDelegate();
        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            startTimer.Enabled = false;
            if (store.StartRun())
            {
                BeginInvoke(new ShowFormDelegate(ShowStatus));
            }
        }
        private void ShowStatus()
        {
            formLineStatus(true);
        }

        private void formLineStatus(bool isStart)
        {
            foreach (TabPage tab in tabPageList)
            {
                bool find = false;
                foreach (Control control in tab.Controls)
                {
                    if (control is Panel)
                    {
                        foreach (Control con in control.Controls)
                        {
                            if (con is FrmBox)
                            {
                                FrmBox frm = (FrmBox)con;
                                frm.StoreOpenStatus(isStart);
                                find = true;
                                break;
                            }

                        }
                    }
                    if (find)
                    {
                        break;
                    }
                }
            }
            启动AToolStripMenuItem.Enabled = !isStart;
            停止TToolStripMenuItem.Enabled = isStart;

        }
        private void 停止所有料仓TToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (store != null)
            {
                if (store.storeRunStatus.Equals(StoreRunStatus.Wait))
                {
                    MessageBox.Show(store.Name + "包装料料仓未启动,不需要停止");
                    return;
                }
                if (store != null)
                {
                    store.StopRun();
                }
                formLineStatus(false);
            }
        }

        private void 复位RToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (store.storeRunStatus.Equals(StoreRunStatus.Wait))
            {
                MessageBox.Show(store.Name + "包装料料仓未启动,无法复位");
                return;
            }
            store.Reset();
        }
        private void FrmLineStore_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (停止TToolStripMenuItem.Enabled)
            {
                停止所有料仓TToolStripMenuItem_Click(null, null);
            }
        }
        private void btnClearLog_Click(object sender, EventArgs e)
        {
            logBox.Clear(); 
        }

        private void debug_enable_checkBox_CheckedChanged(object sender, EventArgs e)
        {
            LogUtil.debug_opened = chkDebug.Checked;
        }

        private void btnCopyLog_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(logBox.Text);
            MessageBox.Show("已复制日志到粘贴板!");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (!this.Visible)
            {
                return;
            }
          
        }
 
        private void chbAutoRun_CheckedChanged(object sender, EventArgs e)
        {
            if (!LoadOk)
            {
                return;
            }
            if (chbAutoRun.Checked)
            {
                ConfigAppSettings.SaveValue(Setting_Init.App_AutoRun, 1);
                ManagerUtil.AutoRun(Application.ExecutablePath, true);
            }
            else
            {
                ConfigAppSettings.SaveValue(Setting_Init.App_AutoRun, 0);
                ManagerUtil.AutoRun(Application.ExecutablePath, false);
            }
        }

        private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
        {
            Font fntTab;
            Brush bshBack;
            Brush bshFore;
            if (e.Index == this.tabControl1.SelectedIndex)
            {
                fntTab = new Font(e.Font, FontStyle.Regular);
                bshBack = new SolidBrush(Color.DodgerBlue);
                bshFore = Brushes.Black;
            }
            else
            {
                fntTab = e.Font;
                bshBack = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, Color.White, Color.White, System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal);
                bshFore = new SolidBrush(Color.Black);
            }
            string tabName = this.tabControl1.TabPages[e.Index].Text;
            StringFormat sftTab = new StringFormat();
            e.Graphics.FillRectangle(bshBack, e.Bounds);
            Rectangle recTab = e.Bounds;
            recTab = new Rectangle(recTab.X, recTab.Y + 4, recTab.Width, recTab.Height - 4);
            e.Graphics.DrawString(tabName, fntTab, bshFore, recTab, sftTab);
        }

        private void 版本号ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmAbout about = new FrmAbout();
            about.ShowDialog();
        }
         
        private void 二维码学习ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CodeLibrary.FrmCodeDecode frm = new CodeLibrary.FrmCodeDecode();
            frm.ShowDialog();
        }

     
        private void 退出ToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            ExitApp();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            ExitApp();
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            if (toolStripMenuItem2.Text .Equals("启用调试"))
            {
                DebugOpen(true );
            }
            else
            {
                DebugOpen(false);
            }
        }
        private void DebugOpen(bool isopen)
        { 
            foreach (TabPage tab in tabPageList)
            {
                bool find = false;
                foreach (Control control in tab.Controls)
                {
                    if (control is Panel)
                    {
                        foreach (Control con in control.Controls)
                        {
                            if (con is FrmBox)
                            {
                                FrmBox frm = (FrmBox)con;
                                frm.DebugStatus(isopen);
                                find = true;
                                break;
                            } 
                        }
                    }
                    if (find)
                    {
                        break;
                    }
                }
            }

            if (isopen)
            {
                toolStripMenuItem2.Text = "禁用调试";
            }
            else
            {
                toolStripMenuItem2.Text = "启用调试";
            }
        }
    }
}