FormBase.cs
3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using System.Reflection;
using System.Threading;
using System.Globalization;
namespace testSoftForContaminationExplorer
{
public partial class FormBase : Form
{
public FormBase()
{
InitializeComponent();
}
#region 换语言
private string formName;
private ResourceManager rmCulture;
private ResourceManager rmBitmap;
string str_LResourse = "zh-CN"; //默认为中文
public void SetCMSLanguage(string formName, ContextMenuStrip cms)
{
for (int i = 0; i < cms.Items.Count; i++)
{
cms.Items[i].Text = getMsg(formName + cms.Items[i].Name);
}
}
/// <summary>
/// 获取字符串相关的资源文件
/// </summary>
public ResourceManager GetCurrentCulture
{
get
{
if (rmCulture == null)
rmCulture = new ResourceManager("App.Properties.LResource", Assembly.GetExecutingAssembly());//从指定的资源文件中获取资源
return rmCulture;
}
}
/// <summary>
/// 获取图像文件相关的资源文件
/// </summary>
public ResourceManager GetCurrentCultureForBitmap
{
get
{
if (rmBitmap == null)
rmBitmap = new ResourceManager("MyCMMI.App.Properties.Resources", Assembly.GetExecutingAssembly());
return rmBitmap;
}
}
/// <summary>
///根据传来的图像控件的名字返回该控件在指定语言区域的图像
/// </summary>
/// <param name="strObjectId"></param>
/// <returns></returns>
public System.Drawing.Bitmap GetImage(string strObjectId)
{
ResourceManager rm = this.GetCurrentCultureForBitmap;//获取指定图像资源文件
object obj = rm.GetObject(strObjectId);
return (System.Drawing.Bitmap)obj;
}
/// <summary>
/// 根据传入的控件名字返回该控件在指定语言区域的TEXT
/// </summary>
/// <param name="strId"></param>
/// <returns></returns>
public string getMsg(string strId)
{
string currentLanguage = "";
try
{
ResourceManager rm = this.GetCurrentCulture;//得到当前语言区域的指定资源
//
//通过语言种类变量设置当前线程的语言区域,若是注释该行,则根据操作系统自身区域语言设置显示值
//
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(str_LResourse);
CultureInfo ci = Thread.CurrentThread.CurrentCulture;//获取当前线程所设置的语言区域
currentLanguage = rm.GetString(strId, ci);//通过所选语言区域匹配资源文件,根据传入控件名称返回该控件相应的TEXT
}
catch (Exception ex)
{
currentLanguage = "";//未找到匹配字符时返回默认的字符
}
return currentLanguage;
}
#endregion
}
}