C# 界面控件中英切换

作者 : admin 本文共2926个字,预计阅读时间需要8分钟 发布时间: 2024-06-10 共1人阅读

编程软件:VS 2015

需求:界面有两个按钮,点击可以将界面上所有控件进行不同语言的切换。

一共两种方案,个人认为第二种方案使用范围更广(这里以中英文切换为例)。

方案一:如图所示,建立两个资源文件

C# 界面控件中英切换插图

将所需控件的中英文分别填入对应的资源文件中,如下图所示:

C# 界面控件中英切换插图(1)

C# 界面控件中英切换插图(2)

代码如下:

        public void ApplyResource(Control control)
        {
            switch (Thread.CurrentThread.CurrentCulture.Name)
            {
                case "en":
                    crm = new ComponentResourceManager(typeof(Resource.Resource_en));
                    break;
                case "zh":
                    crm = new ComponentResourceManager(typeof(Resource.Resource_zh));
                    break;
                default:
                    crm = new ComponentResourceManager(typeof(Resource.Resource_zh));
                    break;
            }
            applyControl(control.GetType().Name, control);//调用
        }
        //递归应用到控件
        private void applyControl(string topName, Control control)
        {
            foreach (Control ctl in control.Controls)
            {
                crm.ApplyResources(ctl, topName + "." + ctl.Name, Thread.CurrentThread.CurrentCulture);
                if (ctl.HasChildren)
                {
                    applyControl(topName, ctl);
                }
            }
        }

创建两个按钮,分别是中文和英文,将方法引用就可以了。

        private void 中文ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            中文按钮是否触发 = true;
            英文按钮是否触发 = false;
            Thread.CurrentThread.CurrentCulture = new CultureInfo("zh");//中文
            ApplyResource(注册界面对象);//传入当前的界面
        }

        private void 英文ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            英文按钮是否触发 = true;
            中文按钮是否触发 = false;
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en");//英文
        }

该方法的缺点在于:界面布局不能自适应,如果英文名和中文名长度不一致就会导致布局发生变化,控件相互遮挡的情况。

 方案二:如图所示,将界面的Language改为英语

C# 界面控件中英切换插图(3)

这样的操作就有有两个注册界面,一个中文的,一个英文的,可以各自调整各自的布局。切换不同的语言,显示不同的界面(界面上的控件翻译自己来)

方案一和二完全不掺和,方案二不需要建立资源文件!

C# 界面控件中英切换插图(4)

C# 界面控件中英切换插图(5)

代码(可以在界面加载的时候从新定义界面大小):

这个代码适用于控件嵌套控件

        public void 英文()
        {
            this.Text = "Registration interface";
            this.Size = new Size(1001, 669);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("en"); //英文是en

            遍历界面控件(this);
        }
        public void 中文()
        {
            this.Text = "注册界面";
            this.Size = new Size(1001, 669);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh"); //英文是en

            遍历界面控件(this);
        }

        private void 遍历界面控件(Control fatherControl)
        {
            //结果:能获取到绝大多数控件
            //问题:Timer、ContextMenuStrip等控件获取不到
            ComponentResourceManager resources = new ComponentResourceManager(typeof(注册界面));
            Control.ControlCollection sonControls = fatherControl.Controls;
            foreach (Control control in sonControls)
            {
                if (control.Controls != null)
                {
                    遍历界面控件(control);
                    resources.ApplyResources(control, control.Name);
                }
            }
        }

如果各个控件之间不存在嵌套关系,可以用如下代码:

        public void 英文()
        {
            this.Text = "Input settings";
            this.Size = new Size(444, 340);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("en"); //英文是en
            ComponentResourceManager resources = new ComponentResourceManager(typeof(注册界面));

            foreach (Control ct in this.Controls)//循环当前界面所有的控件(但是遍历不到控件中包含控件的控件)
            {
                resources.ApplyResources(ct, ct.Name);
                if (ct.HasChildren)
                {
                    resources.ApplyResources(ct, ct.Name);
                }
            }
        }
        public void 中文()
        {
            this.Text = "输入设置";
            this.Size = new Size(297, 343);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh"); //英文是en
            ComponentResourceManager resources = new ComponentResourceManager(typeof(注册界面));

            foreach (Control ct in this.Controls)//循环当前界面所有的控件
            {
                resources.ApplyResources(ct, ct.Name);
                if (ct.HasChildren)
                {
                    resources.ApplyResources(ct, ct.Name);
                }
            }

        }

创建两个按钮,分别是中文和英文,将方法引用就可以了。

        private void zhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            中文按钮是否触发 = true;
            英文按钮是否触发 = false;
            注册界面对象.中文();
        }

        private void enToolStripMenuItem_Click(object sender, EventArgs e)
        {
            英文按钮是否触发 = true;
            中文按钮是否触发 = false;
            注册界面对象.英文();
        }

 

本站无任何商业行为
个人在线分享 » C# 界面控件中英切换
E-->