用c#创建支持多语言的WinForm应用程序

本文介绍如何在Winform应用程序中实现多语言支持,包括设置Form的Localizable属性、创建不同语言的资源文件、通过代码动态加载资源文件以及切换语言等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.新建一个 Winform. 应用程序, 新建一 Form. ,名为 Form1,添加一个菜单一个按钮。如图

2. 设置 Form1 的 Localizable 属性为 true, 设置该属性后,.net 将根据不同的语言,为应用程序生成不同的资源文件
3.设置各个控件的文本(系统默认语言下)
4.更改 Form1 的 Language 属性为想要支持的另一种语言,此例中我们选用 English
5.重新设置各个控件的文本
 注:此时.net 将为 Form1 生成另一个资源文件,在本例中名为 Form1.en.resx
当你需要添加新的控件时,需要切换到default语言。
6. 如果有其它的语言要设置,请重复第4,第5步
7.编写代码 (需要消息框多语言支持的话,就用form做消息框吧。同时也做成多语言支持。)

窗体MultiLanguage的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Globalization;
namespace GlobalResource
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Msg msg = new Msg();
            msg.ShowDialog();
        }
        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        private void 中文ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //更改当前线程的 CultureInfo
            //zh-CN 为中文,更多的关于 Culture 的字符串请查 MSDN
            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("zh-CN");
            //对当前窗体应用更改后的资源
            ApplyResource();
        }
        private void 英文ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //更改当前线程的 CultureInfo
            //en 为英文,更多的关于 Culture 的字符串请查 MSDN
            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en");
            //对当前窗体应用更改后的资源
            ApplyResource();
        }
        /// <summary>
        /// 应用资源
        /// ApplyResources 的第一个参数为要设置的控件
        ///                  第二个参数为在资源文件中的ID,默认为控件的名称
        /// </summary>
        private void ApplyResource()
        {
            System.ComponentModel.ComponentResourceManager res = new ComponentResourceManager(typeof(Form1));
            foreach (Control ctl in Controls)
            {
                res.ApplyResources(ctl, ctl.Name);
            }
            //菜单
            foreach (ToolStripMenuItem item in this.menuStrip1.Items)
            {
                res.ApplyResources(item, item.Name);
                foreach (ToolStripMenuItem subItem in item.DropDownItems)
                {
                    res.ApplyResources(subItem, subItem.Name);
                }
            }
            //Caption
            res.ApplyResources(this, "$this");
        }
    }
}

判断操作系统语言的方法:
private void Form1_Load(object sender, EventArgs e)
{
    //不需要判断操作系统的语言,使用资源文件会自动选择。
    //if (System.Globalization.CultureInfo.InstalledUICulture.Name == "zh-CN")
    //{
    //    Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("zh-CN");
    //    //对当前窗体应用更改后的资源
    //    ApplyResource();
    //}
    //else
    //{
    //    Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en");
    //    //对当前窗体应用更改后的资源
    //    ApplyResource();
    //}
}

 

窗体Msg的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace GlobalResource
{
    public partial class Msg : Form
    {
        public Msg()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

第二种加载资源的方法:
using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Threading; /// 线程空间

namespace Liang.LanguageLibrary
{
    /// <summary>
    /// 语言库 class
    /// </summary>
    public class SetLanguage
    {
        /// <summary>
        /// 设置当前程序的界面语言
        /// </summary>
        /// <param name="lang">语言 </param>
        /// <param name="form">窗体</param>
        /// <param name="frmtype">窗体类型</param>
        public static void SetLang(string lang, Form form, Type frmtype)
        {
            Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
            if (form != null)
            {
                ComponentResourceManager resources = new ComponentResourceManager(frmtype);
                resources.ApplyResources(form, "$this");
                AppLang(form, resources);
            }
        }
        #region AppLang for Control
        /// <summary>
        /// 遍历窗体所有控件,针对其设置当前界面语言
        /// </summary>
        /// <param name="contrl"></param>
        /// <param name="resoureces"></param>
        private static void AppLang(Control control, 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>
        /// 遍历菜单
        /// </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)
                {
                    foreach (ToolStripMenuItem c in tsmi.DropDownItems)
                    {
                        //if (tsmi != ToolStripSeparator)
                        //{ }
                        AppLang(c, resources);
                    }
                }
            }
        }
        #endregion
    }
}

以上类为动态加载

调用方法:

SetLanguage.SetLang("zh-CHS", this, typeof(Mainfrm)); ///zh-CHS 中文 this 不用解释,Mainfrm 是当前窗体名

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

johnlxj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值