FrmBase.cs 8.6 KB
using MetroFramework.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TSA_V.Common;

namespace TSA_V
{
    public partial class FrmBase : Form
    {
        internal static string GetVersion(bool IsShow = false)
        {
            string str = "";
            string version = "";
            DateTime newData = DateTime.Parse("2000-01-01");
            try
            {
                System.Reflection.AssemblyName assemblyName = System.Reflection.Assembly.GetExecutingAssembly().GetName();
                version = assemblyName.Version.ToString();
                string[] strArray = version.Split('.');
                if (strArray.Length.Equals(4))
                {
                    int days = Convert.ToInt32(strArray[2]);
                    int seconds = Convert.ToInt32(strArray[3]);
                    DateTime d1 = DateTime.Parse("2000-01-01");
                    newData = d1.AddDays(days);
                    newData = newData.AddSeconds(seconds * 2);
                    str = newData.ToString("yyyy-MM-dd HH:mm");
                }
            }
            catch (Exception ex)
            {
                str = version;
                LogUtil.error("解析版本号【" + str + "】出错:"+ex.ToString());
            }
            if (IsShow)
            {
                LogUtil.info("版本号[" + version + "][" + str + "][Smart-Workstation][" + GetCodeNum() + "]");
            }
            return str;
        }
        internal static string GetCodeNum(string codeName = "Smart-Workstation")
        {
            byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(codeName);
            string result = "";
            result = AcSerialBean.ByteToString(byteArray);
            return result;
        }
        public string CurrLanguage = "";
        public string ClassName
        {
            get
            {
                return this.GetType().Name;
            }
            set
            {
            }
        }
        public FrmBase()
        {
            InitializeComponent();
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
           // SetScreen();
            //this.Resizable = false;
        }

        private void FrmBase_Load(object sender, EventArgs e)
        {
        }
        
        /// <summary>
        /// 引用user32.dll动态链接库(windows api),
        /// 使用库中定义 API:SetCursorPos 
        /// </summary>
        [DllImport("user32.dll")]
        private static extern int SetCursorPos(int x, int y);
        /// <summary>
        /// 移动鼠标到指定的坐标点
        /// </summary>
        public void MoveMouseToPoint(Point p)
        {
            SetCursorPos(p.X, p.Y);
        }
        /// <summary>
        /// 设置鼠标的移动范围
        /// </summary>
        public void SetMouseRectangle(Rectangle rectangle)
        {
            System.Windows.Forms.Cursor.Clip = rectangle;
        }
        /// <summary>
        /// 设置鼠标位于屏幕中心
        /// </summary>
        public void SetMouseAtCenterScreen()
        {
            int winHeight = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
            int winWidth = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
            Point centerP = new Point(winWidth / 2, winHeight / 2);
            MoveMouseToPoint(centerP);
        }
        public void SetSkin(FrmBase preCon)
        { 
        } 

        public void LanguageProcess()
        {
            if (CurrLanguage.Equals(ResourceCulture.CurrLanguage))
            {
                return;
            }
            string className = this.ClassName;
            CurrLanguage = ResourceCulture.CurrLanguage;
            this.Text = ResourceCulture.GetString(ResourceCulture.GetTextIdStr(className), this.Text);
            if (this is FrmMenu)
            {
                FrmMenu frm = (FrmMenu)this;
                frm.LoadBackImage();
            }
            else if (this is FrmWorkMenu)
            {
                FrmWorkMenu frm = (FrmWorkMenu)this;
                frm.LoadBackImage();

            }
            else
            {
                foreach (Control con in this.Controls)
                {
                    if (con is Label || con is Button || con is RadioButton || con is CheckBox)
                    {
                        string newStr = ResourceCulture.GetString(ResourceCulture.GetTextIdStr(className, con.Name), con.Text);
                        con.Text = newStr;
                        con.Tag = newStr;
                        if (con is Button && (!(this is FrmMenu)) && (!(this is FrmMaintenance)) && (!(this is FrmWorkMenu)))
                        //if (con is Button && (this is FrmWork))
                        {
                            if (ResourceCulture.CurrLanguage.Equals(ResourceCulture.English))
                            {
                                con.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

                            }
                            else
                            {
                                con.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                            }
                        }
                        // con.Refresh();
                    }
                    else if (con.Controls.Count > 0)
                    {
                        PreControlLanaguage(con);
                    }
                }
            }
        }
        private void PreControlLanaguage(Control partentControl)
        {
            string className = this.ClassName;
            if(partentControl is NumericUpDown)
            {
                return;
            }
            partentControl.Text = ResourceCulture.GetString(ResourceCulture.GetTextIdStr(className, partentControl.Name), partentControl.Text);
            foreach (Control con in partentControl.Controls)
            {
                if (con is Label || con is Button || con is RadioButton || con is CheckBox)
                {
                    string newStr = ResourceCulture.GetString(ResourceCulture.GetTextIdStr(className, con.Name), con.Text);
                    con.Text = newStr;
                    if (con is Button && (!(this is FrmMenu)) && (!(this is FrmMaintenance)) && (!(this is FrmWorkMenu)) && (!(this is FrmIoManager)))
                    {
                        if (ResourceCulture.CurrLanguage.Equals(ResourceCulture.English))
                        {
                            con.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

                        }
                        else
                        {
                            con.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                        }
                    }
                    //     con.Refresh();
                }
                else if (con.Controls.Count > 0)
                {
                    PreControlLanaguage(con);
                }
            }
        }

        private void FrmBase_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible.Equals(true))
            {
                LanguageProcess();
            }
        }
        protected void SetScreen(int index = -1)
        {
            Screen[] sc = Screen.AllScreens;

            this.StartPosition = FormStartPosition.Manual;
            if (sc.Count() > index)
            {
                if (index < 0 || index >= sc.Length)
                {
                    int i = 0;
                    foreach (Screen s in sc)
                    {
                        if (s.Primary)
                        {
                            index = i;
                            break;
                        }
                        i++;
                    }
                }
                Screen screen = sc[index];
                int x = (screen.Bounds.Width - this.Width) / 2;
                int y = (screen.Bounds.Height - this.Height) / 2;
                this.Location = new Point(x, y);
                this.StartPosition = FormStartPosition.CenterParent;
                //this.SetScreen(index);
                //this.CenterToScreen();
            }
        }
    }
}