如何使用 JNA 迭代窗口元素?

发布于 2024-11-29 11:31:57 字数 1925 浏览 1 评论 0原文

使用 JNA,我的最终目标是读取使用 Windows NET SEND 或 MSG.EXE 发送的消息,该消息在接收计算机上显示为 Windows 弹出消息窗口。

我已经能够使用下面的代码搜索这个特定的消息窗口并获取 hWnd 句柄。我现在的问题是如何迭代该窗口的所有元素以查找实际的消息文本、阅读消息并单击“确定”按钮?

我的研究告诉我,我需要使用 FindWindowEx (浏览元素)和 PostMessage (单击“确定”按钮),但我正在努力使其工作。

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);
    }
}

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.

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?

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);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗喜 2024-12-06 11:31:58

通过 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文