前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WPF 获得当前输入法语言区域

WPF 获得当前输入法语言区域

作者头像
林德熙
发布2022-08-04 16:42:34
8650
发布2022-08-04 16:42:34
举报
文章被收录于专栏:林德熙的博客林德熙的博客

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

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

代码语言:javascript
复制
       [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 请看下面

代码语言:javascript
复制
    <Grid>
        <TextBlock x:Name="CeareamearTreseretal" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
    </Grid>

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

代码语言:javascript
复制
        private async void HairjurNalllairmo()
        {
            while (true)
            {
                await Task.Delay(100);
                CeareamearTreseretal.Text = GetCurrentKeyboardLayout().DisplayName;
            }
        }

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

参见 C#: Get current keyboard layout\input language

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档