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;
}
}这时修改语言区域就可以看到变化
博客园博客只做备份,博客发布就不再更新,如果想看最新博客,请访问 https://blog.lindexi.com/
如图片看不见,请在浏览器开启不安全http内容兼容

本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名[林德熙](https://www.cnblogs.com/lindexi)(包含链接:https://www.cnblogs.com/lindexi ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我[联系](mailto:lindexi_gd@163.com)。

浙公网安备 33010602011771号