private static Hashtable htFrmData = null;
private static IFaxServer faxServer = null;
public BaseForm(IFaxServer iFaxServer)
: base()
{
InitializeComponent();
faxServer = iFaxServer;
}
public BaseForm()
: base()
{
InitializeComponent();
}
public void BaseForm_Load(object sender, EventArgs e)
{
if (!this.DesignMode)
{
if (!File.Exists(BasicInfo.ConfigFile))
{
//对语言的种类进行赋值(取操作系统的语言)
BasicInfo.LanguageInfo.LanguageCode = System.Globalization.CultureInfo.CurrentUICulture.LCID;
BasicInfo.CreateLanguageVersion(CommonLanguage.BasicInfo.LanguageInfo.LanguageCode.ToString());
}
else
{
//如果存在的法,则读取语言配置文件
CommonLanguage.BasicInfo.LanguageInfo.LanguageCode = Convert.ToInt32(BasicInfo.ReadLanguageVersion());
}
SetLanguageForm(this);
}
}
#region 设置窗体的语言
/// <summary>
/// 设置当前系统所使用的语言版本(窗体)
/// </summary>
/// <param name="currentLanguage">当前语言的版本,如(EN-英文,CH-中文)</param>
/// <param name="form">应用的窗体</param>
private void SetLanguageForm(Form form)
{
htFrmData = faxServer.GetDictFrmInfo(BasicInfo.LanguageInfo.LanguageCode, form.Name);
Control.ControlCollection controls = form.Controls;
string controlText = "";
try
{
#region 窗体的标题
controlText = GetNodeAttributeValue(form.Name);
if (!string.IsNullOrEmpty(controlText))
{
form.Text = controlText;
}
#endregion
//调用控件
SetSubControls(controls);
form.Refresh();
}
catch (Exception EX)
{
MessageBox.Show("语言切换发生错误,您选择的语言将不能应用!/n/n" + EX.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
#endregion
#region 设置子控件语言
/// <summary>
/// 包含在容器中的控件
/// </summary>
/// <param name="controls"></param>
private void SetSubControls(Control.ControlCollection controls)
{
try
{
string controlText = null;
foreach (Control control in controls)
{
if (control.GetType() == typeof(DevExpress.XtraEditors.SimpleButton)) //翻译button
{
DevExpress.XtraEditors.SimpleButton buttonControl = (control as DevExpress.XtraEditors.SimpleButton);
controlText = GetNodeAttributeValue(buttonControl.Name);
if (!string.IsNullOrEmpty(controlText))
{
buttonControl.Text = controlText;
}
}
else if (control.GetType() == typeof(DevExpress.XtraEditors.LabelControl))
{
DevExpress.XtraEditors.LabelControl lableControl = (control as DevExpress.XtraEditors.LabelControl);
controlText = GetNodeAttributeValue(lableControl.Name);
if (!string.IsNullOrEmpty(controlText))
{
lableControl.Text = controlText;
}
}
else if (control.GetType() == typeof(RadioButton))
{
RadioButton radioBtn = (control as RadioButton);
controlText = GetNodeAttributeValue(radioBtn.Name);
if (!string.IsNullOrEmpty(controlText))
{
radioBtn.Text = controlText;
}
}
else if (control.GetType() == typeof(DevExpress.XtraTab.XtraTabControl))
{
DevExpress.XtraTab.XtraTabControl tabControl = (control as DevExpress.XtraTab.XtraTabControl);
DevExpress.XtraTab.XtraTabPage tabPage = null;
for (int i = 0; i < tabControl.TabPages.Count; i++)
{
tabPage = tabControl.TabPages[i];
controlText = GetNodeAttributeValue(tabPage.Name);
if (!string.IsNullOrEmpty(controlText))
{
tabPage.Text = controlText;
}
}
}
else if (control.GetType() == typeof(DevExpress.XtraEditors.GroupControl))
{
DevExpress.XtraEditors.GroupControl groupBox = (control as DevExpress.XtraEditors.GroupControl);
controlText = GetNodeAttributeValue(groupBox.Name);
if (!string.IsNullOrEmpty(controlText))
{
groupBox.Text = controlText;
}
}
else if (control.GetType() == typeof(DevExpress.XtraEditors.CheckEdit))
{
DevExpress.XtraEditors.CheckEdit checkBox = (control as DevExpress.XtraEditors.CheckEdit);
controlText = GetNodeAttributeValue(checkBox.Name);
if (!string.IsNullOrEmpty(controlText))
{
checkBox.Text = controlText;
}
}
else if (control.GetType() == typeof(DevExpress.XtraBars.Ribbon.RibbonControl))
{
DevExpress.XtraBars.Ribbon.RibbonControl ribControl = (control as DevExpress.XtraBars.Ribbon.RibbonControl);
DevExpress.XtraBars.Ribbon.RibbonPage ribPage = null;
DevExpress.XtraBars.BarButtonItem btnItem = null;
DevExpress.XtraBars.BarStaticItem staticItem = null;
DevExpress.XtraBars.Ribbon.RibbonPageGroup ribGroup = null;
for (int i = 0; i < ribControl.Pages.Count; i++)
{
ribPage = ribControl.Pages[i];
controlText = GetNodeAttributeValue(ribPage.Name);
if (!string.IsNullOrEmpty(controlText))
{
ribPage.Text = controlText;
}
for (int j = 0; j < ribPage.Groups.Count; j++)
{
ribGroup = ribPage.Groups[j];
controlText = GetNodeAttributeValue(ribGroup.Name);
if (!string.IsNullOrEmpty(controlText))
{
ribGroup.Text = controlText;
}
}
}
for (int k = 0; k < ribControl.Items.Count; k++)
{
if (ribControl.Items[k].GetType() == typeof(DevExpress.XtraBars.BarButtonItem))
{
btnItem = (DevExpress.XtraBars.BarButtonItem)ribControl.Items[k];
controlText = GetNodeAttributeValue(btnItem.Name);
if (!string.IsNullOrEmpty(controlText))
{
btnItem.Caption = controlText;
}
}
else if(ribControl.Items[k].GetType() == typeof(DevExpress.XtraBars.BarStaticItem))
{
staticItem = (DevExpress.XtraBars.BarStaticItem)ribControl.Items[k];
controlText = GetNodeAttributeValue(staticItem.Name);
if (!string.IsNullOrEmpty(controlText))
{
staticItem.Caption = controlText;
}
}
}
}
else if (control.GetType() == typeof(DevExpress.XtraGrid.GridControl))
{
DevExpress.XtraGrid.GridControl gridControl = (control as DevExpress.XtraGrid.GridControl);
DevExpress.XtraGrid.Views.Grid.GridView gridView = (DevExpress.XtraGrid.Views.Grid.GridView)gridControl.MainView;
DevExpress.XtraGrid.Columns.GridColumnCollection colCollection = gridView.Columns;
for (int i = 0; i < colCollection.Count; i++)
{
string controText = GetNodeAttributeValue(colCollection[i].Name);
if (controText != null && controText != string.Empty)
{
colCollection[i].Caption = controText;
}
}
}
else if (control.GetType() == typeof(DevExpress.XtraBars.BarDockControl))
{
DevExpress.XtraBars.BarDockControl barDock = (control as DevExpress.XtraBars.BarDockControl);
if (barDock.Dock == DockStyle.Top)
{
DevExpress.XtraBars.BarManager barManager = barDock.Manager;
DevExpress.XtraBars.BarItem barItem = null;
for (int i = 0; i < barManager.Items.Count; i++)
{
barItem = barManager.Items[i];
controlText = GetNodeAttributeValue(barItem.Name);
if (controlText != null && controlText != string.Empty)
{
barItem.Caption = controlText;
}
}
}
}
//递归调用
SetSubControls(control.Controls);
}
}
catch (Exception EX)
{
MessageBox.Show("语言切换发生错误,您选择的语言将不能应用!/n/n" + EX.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
#endregion
#region 获取节点的值
private static string GetNodeAttributeValue(string tagName)
{
try
{
Object obControlsText = htFrmData[tagName];
if (obControlsText != null)
return obControlsText.ToString();
}
catch { }
return "";
}
#endregion