gpt4 book ai didi

c# - 如何使用模态窗口将 WM_INPUTLANGCHANGEREQUEST 发送到应用程序?

转载 作者:行者123 更新时间:2023-11-30 17:29:29 27 4
gpt4 key购买 nike

我写了一个键盘切换器,效果很好,但如果当前应用程序打开了模态窗口,它就会失败。在键盘开关上,我执行以下操作

hwnd = GetForegroundWindow();
PostMessage(hwnd, WM_INPUTLANGCHANGEREQUEST, IntPtr.Zero, handle);

在哪里

[DllImport("User32.dll", EntryPoint = "PostMessage")]
private static extern int PostMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();

但语言没有改变。

我将如何完成这个?


添加 get root owner 改善了情况,但没有完全帮助。

添加对 GetDesktopWindow 的调用没有帮助:

hwnd = GetDesktopWindow();
InputLangChangeRequest(hwnd, language);
hwnd = GetRootOwner();
InputLangChangeRequest(hwnd, language);

代码在这里https://github.com/dims12/NormalKeyboardSwitcher

最佳答案

使用GetAncestor

Retrieves the owned root window by walking the chain of parent and owner windows returned by GetParent.

如果有模态窗口或模态窗口链,这应该返回主 UI 窗口。

hwnd = GetForegroundWindow();
hwnd = GetAncestor(hwnd, GA_ROOTOWNER); //#define GA_ROOTOWNER 3


显然,如果目标本身是基于对话框的应用程序, WM_INPUTLANGCHANGEREQUEST 会失败(我不知道为什么!)要解决此问题,您可以将 WM_INPUTLANGCHANGEREQUEST 消息发布到对话框的后代(另外到对话框本身的 WM_INPUTLANGCHANGEREQUEST 消息)

static bool MyEnumProc(IntPtr hwnd, IntPtr lParam)
{
PostMessage(hwnd, WM_INPUTLANGCHANGEREQUEST, IntPtr.Zero, lParam);
return true;
}

static void Foo()
{
//Greek input for testing:
var hkl = LoadKeyboardLayout("00000408", KLF_ACTIVATE);
var hwnd = GetForegroundWindow();
if (hwnd != null)
{
hwnd = GetAncestor(hwnd, GA_ROOTOWNER);
PostMessage(hwnd, WM_INPUTLANGCHANGEREQUEST, IntPtr.Zero, (IntPtr)hkl);

StringBuilder buf = new StringBuilder(100);
GetClassName(hwnd, buf, 100);

//if this is a dialog class then post message to all descendants
if (buf.ToString() == "#32770")
EnumChildWindows(hwnd, MyEnumProc, (IntPtr)hkl);
}
}

关于c# - 如何使用模态窗口将 WM_INPUTLANGCHANGEREQUEST 发送到应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51117874/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com