Search code examples
javajna

How get all titles of sub-windows?


I have a window with text. When I find this window in program Microsoft Spy++, this text is displayed as a title of its sub-windows. enter image description here I have this code:

interface User32 extends StdCallLibrary {
    User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);
    int WM_SETTEXT = 0x000c;
    int WM_GETTEXT = 0x000D;

    HWND FindWindowA(String lpClassName, String lpWindowName);
    HWND FindWindowExA(HWND hwndParent, HWND hwndChildAfter, String lpClassName,
                       String lpWindowName);
    LRESULT SendMessageA(HWND paramHWND, int paramInt, WPARAM paramWPARAM, LPARAM paramLPARAM);
    LRESULT SendMessageA(HWND editHwnd, int wmGettext, long l, byte[] lParamStr);
    int GetClassNameA(HWND hWnd, byte[] lpString, int maxCount);
}

public static void main(String[] args) {
    User32 user32 = User32.INSTANCE;

    HWND zavhwnd = com.sun.jna.platform.win32.User32.INSTANCE.FindWindow
            (null, "ZAV - 1.0.05");

    HWND editHwnd = user32.FindWindowExA(zavhwnd, null, 
            "WindowsForms10.RichEdit20W.app.0.2004eee", null);

    byte[] lParamStr = new byte[512];
    LRESULT resultBool = user32.SendMessageA(editHwnd, User32.WM_GETTEXT, 512, lParamStr);

    System.out.println("string: "+Native.toString(lParamStr));
}

It found that window (zavhwnd), because it isn't null, but I still cannot read text, that I want. And it would be better if it retrieve all titles of the sub-windows, because the class "WindowsForms10.RichEdit20W.app.0.2004eee" may not always be the same. Can someone help me with it?


Solution

  • I have already found how to do this:

    interface ExtendedStdLib extends StdCallLibrary {
        ExtendedStdLib INSTANCE = (ExtendedStdLib) Native.load("user32", ExtendedStdLib.class);
        int WM_SETTEXT = 0x000c;
        int WM_GETTEXT = 0x000D;
    
        HWND FindWindowA(String lpClassName, String lpWindowName);
        HWND FindWindowExA(HWND hwndParent, HWND hwndChildAfter, String lpClassName,
                           String lpWindowName);
        LRESULT SendMessageA(HWND paramHWND, int paramInt, WPARAM paramWPARAM, LPARAM paramLPARAM);
        LRESULT SendMessageA(HWND editHwnd, int wmGettext, long l, byte[] lParamStr);
        int GetClassNameA(HWND hWnd, byte[] lpString, int maxCount);
    }
    
    public void getSelectableText() {
        ExtendedStdLib EXT_INSTANCE = ExtendedStdLib.INSTANCE;
    
        HWND zavhwnd = User32.INSTANCE.FindWindow(null, "{Window Title}");
        if (zavhwnd == null) {
            log.error("Window wasn't found");
            return;
        }
    
        User32.INSTANCE.EnumChildWindows(zavhwnd, (hwnd, pntr) -> {
            char[] textBuffer = new char[512];
            char[] textBuffer2 = new char[512];
            User32.INSTANCE.GetClassName(hwnd, textBuffer, 512);
            User32.INSTANCE.GetWindowText(hwnd, textBuffer2, 512);
            String wText = Native.toString(textBuffer);
            String wText2 = Native.toString(textBuffer2);
            if (wText.contains("RichEdit")) {
                byte[] lParamStr = new byte[2048];
                LRESULT resultBool = EXT_INSTANCE.SendMessageA(hwnd, ExtendedStdLib.WM_GETTEXT, 2048, lParamStr);
                String text = Native.toString(lParamStr, "windows-1250");
            }
            return true;
        }, null);
    }