问题描述
使用JNA,我的最终目标是阅读使用Windows NET SEND或MSG.EXE发送的消息,该消息在接收计算机上显示为Windows弹出消息窗口。
Using JNA, my ultimate goal is to read a message that was sent using Windows NET SEND or MSG.EXE, which appears as a Windows pop-up message window on the receiving machine.
我已经能够搜索这个特定的消息窗口,并使用下面的代码获取hWnd句柄。我现在的问题是如何遍历此窗口的所有元素以查找实际的消息文本,阅读消息,还单击确定按钮?
I am already able to search for this specific message window and get the hWnd handle using the code below. My problem now is how to I iterate through all the elements of this window to find the actual message text, read the message, and also click the OK button?
我的研究告诉我,我需要使用FindWindowEx(通过元素)和PostMessage(单击确定按钮),但我正在努力使它工作。
My research tells me I need to use FindWindowEx (to go through the elements) and PostMessage (to click the OK button) but I am struggling to make it work.
package democode;
import com.sun.jna.Pointer;
import com.sun.jna.Native;
import com.sun.jna.win32.StdCallLibrary;
public class JNA_Main {
// Equivalent JNA mappings
public interface User32 extends StdCallLibrary {
User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);
interface WNDENUMPROC extends StdCallCallback {
boolean callback(Pointer hWnd, Pointer arg);
}
boolean EnumWindows(WNDENUMPROC lpEnumFunc, Pointer arg);
boolean PostMessage(Pointer hwndParent, String msg, String wParam, String lParam);
Pointer FindWindowEx(Pointer hwndParent, String hwndChildAfter, String lpszClass, String lpszWindow);
int GetWindowTextA(Pointer hWnd, byte[] lpString, int nMaxCount);
}
public static void main(String[] args) {
final User32 user32 = User32.INSTANCE;
user32.EnumWindows(new User32.WNDENUMPROC() {
int count;
public boolean callback(Pointer hWnd, Pointer userData) {
byte[] windowText = new byte[512];
user32.GetWindowTextA(hWnd, windowText, 512);
String wText = Native.toString(windowText);
wText = (wText.isEmpty()) ? "" : "; text: " + wText;
if (wText.contains("My Window Name")){
System.out.println("Found window " + hWnd + ", total " + ++count + wText);
//**************************************************//
//NEED CODE HERE TO ITERATE THROUGH ELEMENTS OF THIS PARTICULAR WINDOW, READ THE MESSAGE TEXT AND CLICK OK BUTTON.
//**************************************************//
}
return true;
}
}, null);
}
}
推荐答案
通过MSDN的逻辑选择是使用你从上面的回调方法收到的hWnd指针来调用EnumChildWindows。
The logical choice via the MSDN is to call EnumChildWindows using the hWnd pointer that you've received from the callback method above.
这篇关于如何使用JNA迭代窗口元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!