后台 Window 没有收到通过 PostMessage() 发送的消息
Background Window not getting messages sent through PostMessage()
我正在尝试使用 JNA 将消息发送到后台 window。
然而,没有任何反应。我想也许这个应用程序的行为类似于记事本(你需要找到 child windows 才能真正写入文本),所以我尝试将消息发送给所有 child windows同样,但仍然没有任何反应。
这是我的代码:
User32 user32 = User32.INSTANCE;
HWND hWnd = user32.FindWindow(null, "MyWindow");
int tid = user32.GetWindowThreadProcessId(hWnd, null);
user32.EnumThreadWindows(tid, new aux(), null);
private class aux implements WNDENUMPROC {
@Override
public boolean callback(HWND hwnd, Pointer pointer) {
User32 user32 = User32.INSTANCE;
char[] title = new char[1024];
user32.GetWindowText(hwnd, title, 1024);
System.out.println(new String(title));
user32.PostMessage(hwnd, WM_KEYDOWN, new WPARAM(VK_Q), new LPARAM(0));
user32.PostMessage(hwnd, WM_KEYUP, new WPARAM(VK_Q), new LPARAM(1));
System.out.println("error: " + Kernel32.INSTANCE.GetLastError());
return true;
}
}
输出:
MyChildWindow1 (= MyWindow)
error: 0
MyChildWindow2
error: 0
MyChildWindow3
error: 0
我试过使用 PostMessage、PostThreadMessage 和 SendMessage。 None 其中有效。
如何向此应用程序发送消息?
You can’t simulate keyboard input with PostMessage. It does work with some applications but not everything because it skips the lower level parts of the input system like hooks and things waiting for QS_KEY
. If you still want to hack it this way you could use Spy++ 至少可以判断消息是否已发布到正确的 window。如果是并且它什么都不做,那么您需要寻找其他选项。
如果您正在执行简单的 UI 自动化,那么 SendInput
should be your first choice or MSAA/UIA 如果应用程序不在前台或者您需要更多控制。
我正在尝试使用 JNA 将消息发送到后台 window。
然而,没有任何反应。我想也许这个应用程序的行为类似于记事本(你需要找到 child windows 才能真正写入文本),所以我尝试将消息发送给所有 child windows同样,但仍然没有任何反应。
这是我的代码:
User32 user32 = User32.INSTANCE;
HWND hWnd = user32.FindWindow(null, "MyWindow");
int tid = user32.GetWindowThreadProcessId(hWnd, null);
user32.EnumThreadWindows(tid, new aux(), null);
private class aux implements WNDENUMPROC {
@Override
public boolean callback(HWND hwnd, Pointer pointer) {
User32 user32 = User32.INSTANCE;
char[] title = new char[1024];
user32.GetWindowText(hwnd, title, 1024);
System.out.println(new String(title));
user32.PostMessage(hwnd, WM_KEYDOWN, new WPARAM(VK_Q), new LPARAM(0));
user32.PostMessage(hwnd, WM_KEYUP, new WPARAM(VK_Q), new LPARAM(1));
System.out.println("error: " + Kernel32.INSTANCE.GetLastError());
return true;
}
}
输出:
MyChildWindow1 (= MyWindow)
error: 0
MyChildWindow2
error: 0
MyChildWindow3
error: 0
我试过使用 PostMessage、PostThreadMessage 和 SendMessage。 None 其中有效。
如何向此应用程序发送消息?
You can’t simulate keyboard input with PostMessage. It does work with some applications but not everything because it skips the lower level parts of the input system like hooks and things waiting for QS_KEY
. If you still want to hack it this way you could use Spy++ 至少可以判断消息是否已发布到正确的 window。如果是并且它什么都不做,那么您需要寻找其他选项。
如果您正在执行简单的 UI 自动化,那么 SendInput
should be your first choice or MSAA/UIA 如果应用程序不在前台或者您需要更多控制。