当前位置:首页 > Windows程序 > 正文

Winfrom下的中英文翻译

2021-03-25 Windows程序

using System.Collections.Generic;

using System.Linq;

using System.Reflection;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace CameraTest_Net

{

    public class LanguageHelper

    {

 

        #region SetAllLang

        /// <summary>

        /// Set language

        /// </summary>

        /// <param>language:zh-CN, en-US</param>

        private static void SetAllLang(string lang)

        {

            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);

            Form frm = null;

 

            string name = "MainForm";

 

            frm = (Form)Assembly.Load("CameraTest").CreateInstance(name);

 

            if (frm != null)

            {

                System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager();

                resources.ApplyResources(frm, "$this");

                AppLang(frm, resources);

            }

        }

        #endregion

 

        #region SetLang

        /// <summary>

        ///

        /// </summary>

        /// <param>language:zh-CN, en-US</param>

        /// <param>the form you need to set</param>

        /// <param>the type of the form </param>

        public static void SetLang(string lang, Form form, Type formType)

        {

            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);

            if (form != null)

            {

                System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(formType);

                resources.ApplyResources(form, "$this");

                AppLang(form, resources);

            }

        }

        #endregion

 

        #region AppLang for control

        /// <summary>

        ///  loop set the propery of the control

        /// </summary>

        /// <param></param>

        /// <param></param>

        private static void AppLang(Control control, System.ComponentModel.ComponentResourceManager resources)

        {

            if (control is MenuStrip)

            {

                resources.ApplyResources(control, control.Name);

                MenuStrip ms = (MenuStrip)control;

                if (ms.Items.Count > 0)

                {

                    foreach (ToolStripMenuItem c in ms.Items)

                    {

                        AppLang(c, resources);

                    }

                }

            }

 

            foreach (Control c in control.Controls)

            {

                resources.ApplyResources(c, c.Name);

                AppLang(c, resources);

            }

        }

        #endregion

 

        #region AppLang for menuitem

        /// <summary>

        /// set the toolscript

        /// </summary>

        /// <param></param>

        /// <param></param>

        private static void AppLang(ToolStripMenuItem item, System.ComponentModel.ComponentResourceManager resources)

        {

            if (item is ToolStripMenuItem)

            {

                resources.ApplyResources(item, item.Name);

                ToolStripMenuItem tsmi = (ToolStripMenuItem)item;

                if (tsmi.DropDownItems.Count > 0)

                {

                    foreach (ToolStripMenuItem c in tsmi.DropDownItems)

                    {

                        AppLang(c, resources);

                    }

                }

            }

        }

        #endregion

    }

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/67410.html