10

即使我的应用程序不在焦点上,有没有办法检测 windows/os 语言是否发生了变化?
到目前为止,只有当应用程序专注于使用以下内容时,我才能实现我想要的:

string language = "";
System.Windows.Input.InputLanguageManager.Current.InputLanguageChanged +=
new System.Windows.Input.InputLanguageEventHandler((sender, e) =>
{
    language = e.NewLanguage.DisplayName;
    MessageBox.Show(language);
});

但正如你所理解的,这并不是我想要的。

我正在考虑其他解决方案,例如挂钩更改语言的键(例如 alt+shift),但我无法知道当前正在使用什么语言,并且用户可以更改默认热键...

感谢您的帮助。

4

1 回答 1

11

您面临的问题与 WM_INPUTLANGCHANGE 消息的工作方式有关。此消息由操作系统发送给程序,以通知它们有关语言的更改。但是,根据文档,此消息仅发送到“最受影响的窗口”。这意味着您甚至可以调用本机方法GetKeyboardLayout(顺便说一下,它由InputLanguageManager使用)但是如果应用程序未处于活动状态, GetKeyboardLayout将始终返回最后一个已知的过时语言。

考虑到这一点,使用@VDohnal 指出的解决方案可能是一个好主意,即找到当前最顶层的窗口并为其读取键盘布局。这是如何在 WPF 应用程序中执行此操作的快速概念证明。我使用了一个额外的线程,它会定期找到最顶层的窗口并为其准备好键盘布局。该代码远非完美,但它可以工作,它可能会帮助您实现自己的解决方案。

public partial class MainWindow : Window
{
    [DllImport("user32.dll")]
    static extern IntPtr GetKeyboardLayout(uint idThread);
    [DllImport("user32.dll")]
    private static extern IntPtr GetForegroundWindow();
    [DllImport("user32.dll")]
    static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr processId);

    private CultureInfo _currentLanaguge;

    public MainWindow()
    {
        InitializeComponent();

        Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    HandleCurrentLanguage();
                    Thread.Sleep(500);
                }
            });
    }

    private static CultureInfo GetCurrentCulture()
    {
        var l = GetKeyboardLayout(GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero));
        return new CultureInfo((short)l.ToInt64());
    }

    private void HandleCurrentLanguage()
    {
        var currentCulture = GetCurrentCulture();
        if (_currentLanaguge == null || _currentLanaguge.LCID != currentCulture.LCID)
        {
            _currentLanaguge = currentCulture;
            MessageBox.Show(_currentLanaguge.Name);
        }
    }
}
于 2014-11-04T19:06:33.280 回答