码迷,mamicode.com
首页 > 编程语言 > 详细

C#窗体多语言切换(简繁)

时间:2015-11-20 17:35:23      阅读:412      评论:0      收藏:0      [点我收藏+]

标签:

多窗体最好继承一个父窗体,在父窗体体Load事件中执行此方法

添加引用 using Microsoft.VisualBasic;

 

技术分享
  1         #region 语言切换
  2         /// <summary>
  3         /// 语言切换
  4         /// </summary>
  5         public static void ChangeLanguage(Form f)
  6         {
  7             //设置窗体标题
  8             f.Text = Strings.StrConv(f.Text,
  9                     StaticInfo.Language.ToUpper() == "CN"
 10                         ? VbStrConv.TraditionalChinese
 11                         : VbStrConv.SimplifiedChinese, 1);
 12 
 13             //遍历Form上的所有控件
 14             foreach (Control control in f.Controls)
 15             {
 16                 if (control is Panel || control is GroupBox)
 17                 {
 18                     foreach (Control con in control.Controls)
 19                     {
 20                         SetLanguage(con);
 21                     }
 22                 }
 23                 else
 24                 {
 25                     SetLanguage(control);
 26                 }
 27             }
 28         }
 29 
 30         private static void SetLanguage(Control con)
 31         {
 32 
 33             //设置按钮
 34             Button btn = con as Button;
 35             if (btn != null)
 36             {
 37                 btn.Text = Strings.StrConv(btn.Text,
 38                     StaticInfo.Language.ToUpper() == "CN"
 39                         ? VbStrConv.TraditionalChinese
 40                         : VbStrConv.SimplifiedChinese, 1);
 41             }
 42 
 43 
 44 
 45             //设置文本标签
 46             Label lb = con as Label;
 47             if (lb != null)
 48             {
 49                 lb.Text = Strings.StrConv(lb.Text,
 50                     StaticInfo.Language.ToUpper() == "CN"
 51                         ? VbStrConv.TraditionalChinese
 52                         : VbStrConv.SimplifiedChinese, 1);
 53             }
 54 
 55 
 56             //设置复选框
 57             CheckBox cb = con as CheckBox;
 58             if (cb != null)
 59             {
 60                 cb.Text = Strings.StrConv(cb.Text,
 61                     StaticInfo.Language.ToUpper() == "CN"
 62                         ? VbStrConv.TraditionalChinese
 63                         : VbStrConv.SimplifiedChinese, 1);
 64             }
 65 
 66 
 67             //设置菜单栏
 68             MenuStrip ms = con as MenuStrip;
 69             if (ms != null)
 70             {
 71                 foreach (ToolStripMenuItem item in ms.Items)
 72                 {
 73                     if (StaticInfo.Language.ToUpper() == "CN")
 74                     {
 75                         item.Text = Strings.StrConv(item.Text, VbStrConv.TraditionalChinese, 1);
 76                         for (int i = 0; i < item.DropDownItems.Count; i++)
 77                         {
 78                             item.DropDownItems[i].Text = Strings.StrConv(item.DropDownItems[i].Text,
 79                                 VbStrConv.TraditionalChinese, 1);
 80                         }
 81                     }
 82                     else
 83                     {
 84                         item.Text = Strings.StrConv(item.Text, VbStrConv.SimplifiedChinese, 1);
 85                         for (int i = 0; i < item.DropDownItems.Count; i++)
 86                         {
 87                             item.DropDownItems[i].Text = Strings.StrConv(item.DropDownItems[i].Text,
 88                                 VbStrConv.SimplifiedChinese, 1);
 89                         }
 90                     }
 91 
 92                 }
 93             }
 94 
 95 
 96             //设置工具栏
 97             ToolStrip ts = con as ToolStrip;
 98             if (ts != null)
 99             {
100                 for (int i = 0; i < ts.Items.Count; i++)
101                 {
102                     ts.Items[i].Text = Strings.StrConv(ts.Items[i].Text,
103                         StaticInfo.Language.ToUpper() == "CN"
104                             ? VbStrConv.TraditionalChinese
105                             : VbStrConv.SimplifiedChinese, 1);
106                 }
107             }
108 
109 
110             //设置数据表格
111             DataGridView dgv = con as DataGridView;
112             if (dgv != null)
113             {
114                 for (int i = 0; i < dgv.Columns.Count; i++)
115                 {
116                     dgv.Columns[i].HeaderText = Strings.StrConv(dgv.Columns[i].HeaderText,
117                         StaticInfo.Language.ToUpper() == "CN"
118                             ? VbStrConv.TraditionalChinese
119                             : VbStrConv.SimplifiedChinese, 1);
120                 }
121             }
122 
123 
124             //设置选项卡
125             TabControl tc = con as TabControl;
126             if (tc != null)
127             {
128                 tc.Text = Strings.StrConv(tc.Text,
129                         StaticInfo.Language.ToUpper() == "CN"
130                             ? VbStrConv.TraditionalChinese
131                             : VbStrConv.SimplifiedChinese, 1);
132 
133                 for (int i = 0; i < tc.TabPages.Count; i++)
134                 {
135                     tc.TabPages[i].Text = Strings.StrConv(tc.TabPages[i].Text,
136                           StaticInfo.Language.ToUpper() == "CN"
137                               ? VbStrConv.TraditionalChinese
138                               : VbStrConv.SimplifiedChinese, 1);
139 
140                     foreach (Control c in tc.TabPages[i].Controls)
141                     {
142                         SetLanguage(c);
143                     }
144                 }
145             }
146 
147 
148             //设置ListView
149             ListView lv = con as ListView;
150             if (lv != null)
151             {
152                 for (int i = 0; i < lv.Columns.Count; i++)
153                 {
154                     lv.Columns[i].Text = Strings.StrConv(lv.Columns[i].Text,
155                         StaticInfo.Language.ToUpper() == "CN"
156                             ? VbStrConv.TraditionalChinese
157                             : VbStrConv.SimplifiedChinese, 1);
158                 }
159             }
160 
161             //分组框设置
162             GroupBox gb = con as GroupBox;
163             if (gb != null)
164             {
165                 foreach (Control control in gb.Controls)
166                 {
167                     SetLanguage(control);
168                 }
169             }
170 
171             
172         }
173 
174         #endregion
多语言切换代码

 

C#窗体多语言切换(简繁)

标签:

原文地址:http://www.cnblogs.com/codeyou/p/4981243.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!