如何防止机器上安装的语言发生变化

时间:2012-08-14 10:04:49

标签: c# winforms currentuiculture

我需要使用文本框的alt + shift键禁用语言更改。我有更改currentCulture的功能,但按Alt + Shift总是切换到下一语言,即使我在keydown或keyup事件上设置它。

1 个答案:

答案 0 :(得分:0)

您必须检测Windows操作系统输入语言更改,并在其具有焦点时将其更改为您的应用程序所需的语言。 因此,请阅读以下文章以检测Windows操作系统输入语言更改 1. http://msdn.microsoft.com/en-us/library/ms644990(VS.85).aspx 2. http://www.codeproject.com/KB/system/TrayMe.aspx 然后使用以下代码实现您的目标:

/// <summary>
/// Changing Current Input Language to a next installed language
/// </summary>
public void ChangeInputLanguage()
{
    // Nothing to do if there is only one Input Language supported:
    if (InputLanguage.InstalledInputLanguages.Count == 1)
        return;
    // Get index of current Input Language
    int currentLang = InputLanguage.InstalledInputLanguages.IndexOf(InputLanguage.CurrentInputLanguage);
    // Calculate next Input Language
    InputLanguage nextLang = ++currentLang == InputLanguage.InstalledInputLanguages.Count ? 
    InputLanguage.InstalledInputLanguages[0] : InputLanguage.InstalledInputLanguages[currentLang];
    // Change current Language to the calculated:
    ChangeInputLanguage(nextLang);
}

/// <summary>
/// Changing current Input Language to a new one passed in the param
/// </summary>
/// <param name="ISOLang">ISO Culture name string code e.g. "en" for English</param>
public void ChangeInputLanguage(string ISOLang)
{
    // Convert ISO Culture name to InputLanguage object. Be aware: if ISO is not supported
    // ArgumentException will be invoked here
    InputLanguage nextLang = InputLanguage.FromCulture(new System.Globalization.CultureInfo(ISOLang));
    ChangeInputLanguage(nextLang);
}

/// <summary>
/// Changing current Input Language to a new one passed in the param
/// </summary>
/// <param name="LangID">Integer Culture code e.g. 1033 for English</param>
public void ChangeInputLanguage(int LangID)
{
    // Convert Integer Culture code to InputLanguage object. Be aware: if Culture code is not supported
    // ArgumentException will be invoked here
    InputLanguage nextLang = InputLanguage.FromCulture(new System.Globalization.CultureInfo(LangID));
    ChangeInputLanguage(nextLang);
}

/// <summary>
/// Changing current Input Language to a new one passed in the param
/// </summary>
/// <param name="InputLang">New Input Language as InputLanguage object</param>
public void ChangeInputLanguage(InputLanguage InputLang)
{
    // Check is this Language really installed. Raise exception to warn if it is not:
    if (InputLanguage.InstalledInputLanguages.IndexOf(InputLang) == -1)
        throw new ArgumentOutOfRangeException();

    // InputLAnguage changes here:
    InputLanguage.CurrentInputLanguage = InputLang;
}