zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    _MSC_VER
    git之撤销修改
    vi/vim如何添加或删除多行注释
    C++ 读取文件数据和输出数据到文件
    git上传本地单独修改的文件
    Git学习笔记
    C++中类的声明
    linux中ldconfig的使用介绍
    #define 和 typedef 中的##
    find、xargs、grep基本用法
  • 原文地址:https://www.cnblogs.com/lindexi/p/12085870.html
Copyright © 2011-2022 走看看