C# Winform 國際化

 

一、在Form的language屬性選擇中文,來製做中文界面this

保存後,設置界面標題會變成以下所示,而且會出現一個zh-CN的資源文件,打開resx文件可看到相應內容spa

二、將Form的language屬性改成英文,其他步驟如第1步,便可生成英文資源文件 .net

三、添加代碼完成切換3d

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

namespace LanguageDemo
{
    public partial class Form1 : Form
    {

        ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));
        // 遍歷控件,並根據資源文件替換相應屬性
        private void ApplyResource()
        {
            foreach (Control ctl in this.Controls)
            {
                resources.ApplyResources(ctl, ctl.Name); 
            }
            this.ResumeLayout(false);
            this.PerformLayout();
            resources.ApplyResources(this, "$this");
        } 

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string s = System.Globalization.CultureInfo.InstalledUICulture.Name; //獲取當前系統的語言
            Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(s);
            ApplyResource();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == 0)
            {
                Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-CN");
            }
            else
            {
                Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-GB");
            }
            ApplyResource();
        }
    }
}

原文:https://blog.csdn.net/jack____wen/article/details/79614712 code

相關文章
相關標籤/搜索