简体   繁体   English

如何在Wndproc中专门在C#Winform中获取IME布局?

[英]How to get the IME layout in C# Winform specially in Wndproc?

I have a winform application, I want to get the information of current typing language in C#. 我有一个winform应用程序,我想在C#中获取当前输入语言的信息。 I have windows 10 machine, I have selected Korean language. 我有windows 10机器,我选择了韩语。 There is one toggle button in taskbar to change input language as English or Korean once I select Korean language. 在选择韩语后,任务栏中有一个切换按钮可将输入语言更改为英语或韩语。

Following code always gives Korean language, but it should give English when I select input in English. 以下代码总是提供韩语,但是当我选择英语输入时它应该提供英语。

    [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 Form1()
    {
        InitializeComponent();
        IntPtr foregroundWindow = GetForegroundWindow();
        uint foregroundProcess = GetWindowThreadProcessId(foregroundWindow, IntPtr.Zero);
        int keyboardLayout = GetKeyboardLayout(foregroundProcess).ToInt32() & 0xFFFF;
        CultureInfo info = new CultureInfo(keyboardLayout);
        int keyboardLayoutId = info.KeyboardLayoutId;
        string name = info.Name;
    }

Is there any other way to get the input language information. 有没有其他方法来获取输入语言信息。

You can find the attached image for the same. 您可以找到相同的附加图像。 I have highlighted A as english input. 我强调A作为英语输入。 TaskBar屏幕截图英文输入

TaskBar截图韩文输入

I want this information in WndProc method. 我想在WndProc方法中获取此信息。

Problem Summry: So I want to handle the case when User switched language to type by toggle button, I want any wndproc message or window side event to get the language information, when User switches language by toggle button. 问题总结:所以我想处理当用户切换语言通过切换按钮键入的情况时,我想要任何wndproc消息或窗口边事件来获取语言信息,当用户通过切换按钮切换语言时。

Your question is a result of a confusion of IME mode and the input layout. 您的问题是IME模式和输入布局混淆的结果。 Your input layout is Korean in both cases - what changes is your IME mode. 在这两种情况下,您的输入布局都是韩语 - 您的IME模式会发生什么变化。

You can find the IME mode of an input control using Control.ImeMode (and check for a change using Control.ImeModeChanged ). 你可以找到使用输入控件的IME模式 Control.ImeMode (并检查使用的变化 Control.ImeModeChanged )。 This will tell you that you're either in Korean - Hangul, or Korean - Alpha. 这将告诉你,你要么是韩语 - 韩文,要么是韩语 - 阿尔法。 EDIT: Actually, this only allows you to force a given setting, not read the user-specified IME mode, and apparently shouldn't be used. 编辑:实际上,这只允许您强制给定设置,而不是读取用户指定的IME模式,显然不应该使用。

Further complication is if you have global input enabled (available since Windows 8), where the input configuration is no longer stored per-thread, but rather is global. 更复杂的是,如果您启用了全局输入(从Windows 8开始可用),其中输入配置不再按线程存储,而是全局。 In that case ImeMode doesn't work, and is completely ignored. 在这种情况下, ImeMode不起作用,完全被忽略。 I don't think there's anything you can do about it from the application side - the setting is no longer yours to see or change. 我不认为从应用程序方面你可以做任何事情 - 设置不再是你的看到或改变。 According to MSDN, the proper replacement would be the ImmGetConversionStatus function, though only for desktop applications. 根据MSDN,正确的替换将是ImmGetConversionStatus函数,但仅适用于桌面应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM