WinForm多国语言设置


选择窗体属性,把语言改成需要的语言,然后界面也改成相应的语言

之后核心代码在以下


    public class Language
    {
        
        /// <summary>
        /// 设置当前程序的界面语言
        /// </summary>
        /// <param name="lang">language:zh-CN, en-US</param>
        /// <param name="form">窗体实例</param>
        /// <param name="formType">窗体类型</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);
            }
        }

        
        /// <summary>
        /// 遍历窗体所有控件,针对其设置当前界面语言
        /// </summary>
        /// <param name="control"></param>
        /// <param name="resources"></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);
            }
        }
        /// <summary>
        /// 遍历菜单
        /// </summary>
        /// <param name="item"></param>
        /// <param name="resources"></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)
                {
                    for (int i = 0; i < tsmi.DropDownItems.Count; i++)
                    {
                        if (tsmi.DropDownItems[i] is ToolStripMenuItem)
                        {
                            {
                                AppLang((ToolStripMenuItem)tsmi.DropDownItems[i], resources);

                            }
                        }
                    }
                }
            }
        }
    }

以下是方法调用

    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Test();
        }

        static void Test()
        {
            Form1 form = new Form1();
            Language.SetLang("zh-cn", form, typeof(Form1));//zh-cn中文    en英文
            Application.Run(form);
        }

    }

原创来自 http://www.luofenming.com/show.aspx?id=ART2018052100001
转发请保留原创地址,谢谢!

评论