📄 changelanguage.cs
字号:
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using Microsoft.Win32;
using HardwareDistributor.Properties;
using HardwareDistributor.Utilities;
namespace HardwareDistributor.UI
{
public partial class ChangeLanguage : Form
{
#region Members
static private Dictionary<string, int> _languageMappings = new Dictionary<string, int>();
private int _currentLanguageLCID;
private static bool _isMUI = false;
#endregion
#region Constructors
public ChangeLanguage()
{
InitializeComponent();
// We are removing both the Control Box and the Minimize Box, so that we don't
// have to handle these in our code. This is not necessary in Windows Mobile Standard,
// but we are writing this code so that it works on all platforms without any changes
// or any recompiling.
this.ControlBox = false;
this.MinimizeBox = false;
GetLanguages();
DisplayLanguages();
}
#endregion
#region Event Handlers
/// <summary>
/// Language changed
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void menuOk_Click(object sender, EventArgs e)
{
//get the language selected
string selectedLanguage = LanguageComboBox.SelectedItem.ToString();
//lookup the language id for this
int languageId = _languageMappings[selectedLanguage];
//only if the language has changed
if (languageId != _currentLanguageLCID)
{
//check with user if he wants
DialogResult result = MessageBox.Show(
Resources.LanguageChangeMessage,
Resources.LanguageChangeCaption,
MessageBoxButtons.YesNo,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);
//if user confirms action to reset device
if (result == DialogResult.Yes)
{
ResetDevice(languageId);
}
}
//in case no change required, simply shutdown the form
this.Close();
}
/// <summary>
/// cancel the operation
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void menuCancel_Click(object sender, EventArgs e)
{
this.Close();
}
#endregion
#region Methods
public static int LocaleCallback(String s)
{
int languageLCID = int.Parse(s, NumberStyles.AllowHexSpecifier);
string languageName = CultureInfo.GetCultureInfo(languageLCID).NativeName;
if (!_languageMappings.ContainsKey(languageName))
_languageMappings.Add(languageName, languageLCID);
return 1;
}
/// <summary>
/// retrieves the languages available for selection.
/// </summary>
private void GetLanguages()
{
//get the languages available from registry
//if MUI key exists
RegistryKey mui = Registry.LocalMachine.OpenSubKey("MUI");
if (null != mui)
{
// if "Available" Key exists
RegistryKey availableLanguagesKey = mui.OpenSubKey("Available");
if (null != availableLanguagesKey)
{
_isMUI = true;
PopulateMappings(availableLanguagesKey);
}
else
{
// if no MUI, check to see if you can use the locale
Native.LOCALE_ENUMPROC local = new Native.LOCALE_ENUMPROC(LocaleCallback);
PopulateMappings(local);
}
}
}
/// <summary>
/// sets up the mapping of language name against the language LCID
/// </summary>
/// <param name="availableLanguagesKey"></param>
private void PopulateMappings(RegistryKey availableLanguagesKey)
{
//get the entry name
string[] entries = availableLanguagesKey.GetValueNames();
//loop through and create a lookup for the entries
foreach (string languageHex in entries)
{
int languageLCID = int.Parse(languageHex, NumberStyles.AllowHexSpecifier);
string languageName = CultureInfo.GetCultureInfo(languageLCID).NativeName;
_languageMappings.Add(languageName, languageLCID);
}
}
/// <summary>
/// sets up the mapping of language name against the locale LCID
/// </summary>
/// <param name="local"></param>
private void PopulateMappings(Native.LOCALE_ENUMPROC local)
{
//get the entry name, loop through and create a lookup for the entries
Native.EnumSystemLocales(local, Native.LocaleFlags.Supported);
}
/// <summary>
/// loads the available languages into the combo box
/// </summary>
private void DisplayLanguages()
{
LanguageComboBox.Items.Clear();
//load up the languages into the combo box
foreach (string languageName in _languageMappings.Keys)
LanguageComboBox.Items.Add(languageName);
DefaultToCurrentLanguage();
}
/// <summary>
/// display the current language
/// </summary>
private void DefaultToCurrentLanguage()
{
//get and store the system default language
if (_isMUI)
_currentLanguageLCID = (int)Native.GetUserDefaultUILanguage();
else _currentLanguageLCID = (int)Native.GetUserDefaultLCID();
//set the combo box to show the current language
if (_currentLanguageLCID != 0)
{
LanguageComboBox.SelectedItem = CultureInfo.GetCultureInfo(_currentLanguageLCID).NativeName;
}
}
/// <summary>
/// reset the device
/// </summary>
/// <param name="languageId"></param>
private static void ResetDevice(int languageId)
{
try
{
bool updateSuccess;
if (_isMUI)
updateSuccess = Native.SetUserDefaultUILanguage(
Convert.ToInt16(languageId));
else updateSuccess = Native.SetUserDefaultLCID(
Convert.ToInt16(languageId));
//set the language and check the return
if (updateSuccess)
{
//reset the device
Native.ResetDevice();
}
else
{
MessageBox.Show(Resources.LanguageUpdateError, Resources.SystemError);
}
}
catch (Exception ex)
{
Business.Services.LogErrors(ex);
MessageBox.Show(Resources.LanguageUpdateError, Resources.SystemError);
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -