• 2019-6-23-WPF-获得当前输入法语言区域


    title author date CreateTime categories
    WPF 获得当前输入法语言区域
    lindexi
    2019-06-23 11:51:21 +0800
    2018-10-12 11:5:41 +0800
    WPF

    本文告诉大家如何获得 WPF 输入法的语言区域

    需要使用 user32 的方法,很简单,请看下面

           [DllImport("user32.dll")] static extern IntPtr GetForegroundWindow();
            [DllImport("user32.dll")] static extern uint GetWindowThreadProcessId(IntPtr hwnd, IntPtr proccess);
            [DllImport("user32.dll")] static extern IntPtr GetKeyboardLayout(uint thread);
    
            public CultureInfo GetCurrentKeyboardLayout()
            {
                try
                {
                    IntPtr foregroundWindow = GetForegroundWindow();
                    uint foregroundProcess = GetWindowThreadProcessId(foregroundWindow, IntPtr.Zero);
                    int keyboardLayout = GetKeyboardLayout(foregroundProcess).ToInt32() & 0xFFFF;
                    return new CultureInfo(keyboardLayout);
                }
                catch (Exception)
                {
                    return new CultureInfo(1033); // Assume English if something went wrong.
                }
            }

    因为没有提供语言区域变化的事件,所以需要自己写一个循环来获取

    在界面添加一个 TextBlock 请看下面

        <Grid>
            <TextBlock x:Name="CeareamearTreseretal" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
        </Grid>

    这样可以在后台代码给一个值

            private async void HairjurNalllairmo()
            {
                while (true)
                {
                    await Task.Delay(100);
                    CeareamearTreseretal.Text = GetCurrentKeyboardLayout().DisplayName;
                }
            }

    这时修改语言区域就可以看到变化

    参见 C#: Get current keyboard layoutinput language

  • 相关阅读:
    [ARC080D] Prime Flip
    硬币游戏
    点分治
    OneInDark对众数的爱
    [CF838D] Airplane Arrangements
    网络流总结(转载)
    二分图最大权完美匹配(KM)
    网络流
    FWT
    FFT & NTT
  • 原文地址:https://www.cnblogs.com/lindexi/p/12085870.html
Copyright © 2020-2023  润新知