C# Winform程序中英文显示示例
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication24
{
enum lang { Chinese ,English}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
LoadLanguage(this,lang.Chinese);//这里设置显示的语言
}
private void LoadLanguage(Form aForm, lang language)
{
if (aForm != null)
{
switch (language)
{
case lang.Chinese:
Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CN");
break;
case lang.English:
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");
break;
default:
break;
}
ComponentResourceManager resources = new ComponentResourceManager(aForm.GetType());
resources.ApplyResources(aForm, "$this");
LoadingControls(aForm, resources);
}
}
private void LoadingControls(Control aControl, ComponentResourceManager aResources)
{
if (aControl is MenuStrip)
{
//将资源与控件对应
aResources.ApplyResources(aControl, aControl.Name);
MenuStrip menu = (MenuStrip)aControl;
if (menu.Items.Count > 0)
{
foreach (ToolStripMenuItem item in menu.Items)
{
//遍历菜单
Loading(item, aResources);
}
}
}
foreach (Control ctrl in aControl.Controls)
{
aResources.ApplyResources(ctrl, ctrl.Name);
LoadingControls(ctrl, aResources);
}
}
private void Loading(ToolStripMenuItem aItem, ComponentResourceManager aResources)
{
if (aItem is ToolStripMenuItem)
{
aResources.ApplyResources(aItem, aItem.Name);
if (aItem.DropDownItems.Count > 0)
{
foreach (ToolStripMenuItem item in aItem.DropDownItems)
{
Loading(item, aResources);
}
}
}
}
}
}
需要设置窗体两个属性:Language和Localizable;
运行结果: