SettingControl.cs 8.4 KB
using ConfigHelper;
using DeviceLibrary;
using OnlineStore.Common;
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 AutoScanAndLabel
{
    public partial class SettingControl : UserControl
    {
        public SettingControl()
        {
            InitializeComponent();
            RobotManage.LoadFinishEvent += RobotManage_LoadFinishEvent;
            chbAutoRun.Checked = Convert.ToBoolean(ConfigAppSettings.GetIntValue(Setting_Init.App_AutoRun));
        }

        private void RobotManage_LoadFinishEvent(bool state, string msg)
        {
            if (!state)
                return;
            cb_printerselect.Items.Clear();
            cb_labelselect.Items.Clear();
            foreach (string sPrint in System.Drawing.Printing.PrinterSettings.InstalledPrinters)//获取所有打印机名称
            {
                cb_printerselect.Items.Add(sPrint);
            }
            if (RobotManage.PrintBean != null)
            {
                foreach (string labelname in RobotManage.PrintBean.GetLabelName())
                {
                    cb_labelselect.Items.Add(labelname);
                }
            }
            string PrintName = ConfigHelper.Config.Get(Setting_Init.PrinterName);
            string labelName = ConfigHelper.Config.Get(Setting_Init.LabelName);
            cb_printerselect.Text = PrintName;
            cb_labelselect.Text = labelName;

            LoadPosList();
        }

        void LoadPosList()
        {
            this.tableLayoutPanel1.RowStyles.Clear();
            
            int r = 0;
            int c = 0;
            int lastSubType = 0;
            Random random = new Random(2);
            Color color = Color.Black;
            foreach (ConfigBase configBase in RobotManage.Config.configList)
            {
                if (configBase.SubType < 30 || configBase.ProType != "PRO")
                    continue;

                if (configBase.SubType != lastSubType)
                {
                    lastSubType = configBase.SubType;
                    color = Color.FromArgb(random.Next(30, 150), random.Next(30, 150), random.Next(30, 150));
                }
                //this.tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 18));
                Label button = new Label();
                button.Anchor = (AnchorStyles)(AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom);
                //button.BackColor = color;
                //button.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
                //button.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                button.Name = configBase.ProName;
                //button.Size = new System.Drawing.Size(225, 23);
                button.Text = configBase.Explain;
                button.AutoSize = false;
                button.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
                button.ForeColor = color;

                TextBox textBox = new TextBox();
                //textBox.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                textBox.Name = configBase.ProName;
                //textBox.Margin = new Padding(4, 8, 0, 0);
                textBox.Size = new System.Drawing.Size(150, 23);
                textBox.Text = configBase.ProValue;
                textBox.KeyPress += TextBox_KeyPress;
                textBox.TextChanged += TextBox_TextChanged;

                PropertyInfo pi = RobotManage.Config.GetType().GetProperty(configBase.ProName);
                if (pi != null)
                {
                    textBox.Tag = pi.PropertyType.Name;
                }

                tableLayoutPanel1.Controls.Add(button, c, r);
                tableLayoutPanel1.Controls.Add(textBox, c + 1, r);
                r++;

                if (r > 15)
                {
                    r = 0;
                    c += 3;
                }
            }
        }

        private void TextBox_TextChanged(object sender, EventArgs e)
        {
            var s = (sender as TextBox);
            if (s.Tag.ToString() == "Int32")
            {
                if (!int.TryParse(s.Text, out _))
                    s.Text = lastvalue;
            }
            else if (s.Tag.ToString() == "Double")
            {
                if (!double.TryParse(s.Text, out _))
                    s.Text = lastvalue;
            }
            s.SelectionStart = lastselectindex;
        }

        string lastvalue = "";
        int lastselectindex = 0;
        private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            var s = (sender as TextBox);
            if (s.Tag.ToString() == "Int32")
            {
                if (int.TryParse(s.Text, out _))
                    lastvalue = s.Text; 
            }
            else if (s.Tag.ToString() == "Double")
            {
                if (double.TryParse(s.Text, out _))
                    lastvalue = s.Text;
            }
            lastselectindex = s.SelectionStart;
        }

        private void SettingControl_Load(object sender, EventArgs e)
        {

        }

        private void cb_printerselect_SelectedIndexChanged(object sender, EventArgs e)
        {
            Config.Set(Setting_Init.PrinterName, (sender as ComboBox).Text);
            Config.SaveChange();
            RobotManage.LoadPrintSetting();
        }

        private void cb_labelselect_SelectedIndexChanged(object sender, EventArgs e)
        {
            Config.Set(Setting_Init.LabelName, (sender as ComboBox).Text);
            Config.SaveChange();
            RobotManage.LoadPrintSetting();
        }

        private void btn_labeledit_Click(object sender, EventArgs e)
        {
            RobotManage.PrintBean.EditLabel();
            RobotManage_LoadFinishEvent(true, "");
            RobotManage.LoadPrintSetting();
        }

        public static void AutoRun(string strName, bool value)
        {
            try
            {
                //创建启动对象 
                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                //设置运行文件
                startInfo.FileName = System.Windows.Forms.Application.StartupPath + "\\AuToRunManager.exe";
                //设置启动参数 
                startInfo.Arguments = String.Join(" ", new string[2] { strName, value.ToString() });
                //设置启动动作,确保以管理员身份运行
                startInfo.Verb = "runas";
                //如果不是管理员,则启动UAC 
                System.Diagnostics.Process.Start(startInfo);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

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

        private void btnSavePos_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < tableLayoutPanel1.Controls.Count; i++)
            {
                if (tableLayoutPanel1.Controls[i].GetType().Name != "TextBox")
                    continue;

                TextBox textBox = (TextBox)tableLayoutPanel1.Controls[i];
                PropertyInfo pi = RobotManage.Config.GetType().GetProperty(textBox.Name);
                if (pi != null)
                {
                    if (pi.PropertyType.Name=="Int32")
                        pi.SetValue(RobotManage.Config, int.Parse(textBox.Text));
                    else if (pi.PropertyType.Name == "Double")
                        pi.SetValue(RobotManage.Config, double.Parse(textBox.Text));
                    else
                        pi.SetValue(RobotManage.Config, textBox.Text);
                }
            }
            CSVConfigReader.SaveConfig(RobotManage.Config.ConfigFilePath, RobotManage.Config);
        }
    }
}