HWND 的 native@0xc41bcc 输出到实际 window ID

native@0xc41bcc output for HWND to actual window ID

嘿,我在尝试让这个 JNA findwindow 工作时遇到问题,它给我 windows ID (HWND) 而不是一些疯狂的输出 (native@0xc41bcc)。

我通过使用 C# 了解到,当使用 findwindow 时,它会给出找到的 window 的 ID,然后我可以将其用于 moving/resizing/etc window .

我的代码:

public String exec(ITestExecutionServices tes, String[] args) {
    try {
        writer = new PrintWriter("c:/temp/the-file-name.txt", "UTF-8");
    } catch (FileNotFoundException | UnsupportedEncodingException e1) {
        e1.printStackTrace();
    }

    try {

        final User32 user32 = User32.INSTANCE;

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        user32.EnumWindows(new WNDENUMPROC() {
            @Override
            public boolean callback(HWND hWnd, Pointer arg1) {
                byte[] windowText = new byte[512];
                user32.GetWindowTextA(hWnd, windowText, 512);
                String wText = Native.toString(windowText);
                wText = (wText.isEmpty()) ? "" : wText;

                if (wText.toLowerCase().contains("- funct test -")) {
                    writer.println("text: " + wText);
                    writer.println("HWND: " + hWnd);
                    //User32.INSTANCE.SetWindowPos(hwnd, new HWND(Pointer.createConstant(HWND_BOTTOM)), 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
                    WinDef.HWND hwndFound = User32.INSTANCE.FindWindow(null, wText);
                    writer.println("hwndFound: " + hwndFound);
                    writer.println("t/f: " + User32.INSTANCE.MoveWindow(hwndFound, 500, 500, 10, 100, true));
                    //User32.INSTANCE.SetWindowPos(hWnd, new HWND(Pointer.createConstant(HWND_BOTTOM)), 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
                }
                return true;
            }
        }, null);
    } catch (Exception f) {
        String tmp = f.toString();

        for (StackTraceElement el : f.getStackTrace()) {
            tmp = tmp + System.getProperty("line.separator") + el.toString();
        }

        JOptionPane.showMessageDialog(null, "ERROR!");

        return "false";
    }
    writer.close();
    return "true";
}

public interface User32 extends StdCallLibrary {
    User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);

    HWND FindWindow(String lpClassName, String lpWindowName);
    HWND FindWindowExA(HWND parent, HWND child, String className, String window);
    HWND GetDesktopWindow();
    HWND SetFocus(HWND hWnd);
    HWND WindowFromPoint(long point);
    HWND ChildWindowFromPointEx(HWND hwndParent, long point, int uFlags);

    HMENU GetMenu(HWND hWnd);
    HMENU GetSystemMenu(HWND hWnd, boolean bRevert);
    HMENU GetSubMenu(HMENU hMenu, int nPos);

    HDC GetWindowDC(HWND hWnd);

    LRESULT CallWindowProc(LONG_PTR proc, HWND hWnd, int uMsg, WPARAM uParam, LPARAM lParam);

    int GetWindowTextA(HWND hWnd, byte[] lpString, int nMaxCount);
    int GetWindowRect(HWND handle, int[] rect);
    int GetClassNameA(HWND in, byte[] lpString, int size);
    int SendMessageA(HWND hWnd, int msg, WPARAM wParam, LPARAM lParam);
    int PostMessageA(HWND hWnd, int msg, WPARAM wParam, LPARAM lParam);
    int ReleaseDC(HWND hWnd, HDC hDC);
    int GetTopWindow(HWND hWnd);
    int GetWindow(HWND hWnd, int flag);
    int GetWindowModuleFileName(HWND hWnd, char[] buffer2, int i);
    int GetWindowThreadProcessId(HWND hWnd, PointerByReference pref);
    int SetWindowLongPtr(HWND hWnd, int nIndex, Callback callback);
    int GetMenuString(HMENU hMenu, int uIDItem, char[] buffer, int nMaxCount, int uFlag);
    int GetMenuItemCount(HMENU hMenu);
    int GetMenuItemID(HMENU hMenu, int nPos);
    int GetDlgCtrlID(HWND hwndCtl);
    int GetDlgItemText(HWND hDlg, int nIDDlgItem, byte[] buffer, int nMaxCount);

    boolean MoveWindow(WinDef.HWND hWnd,int X, int Y, int nWidth, int nHeight, boolean bRepaint);
    boolean InvalidateRect(HWND hWnd, long lpRect, boolean bErase);
    boolean UpdateWindow(HWND hWnd);
    boolean RedrawWindow(HWND hWnd, long lprcUpdate, long hrgnUpdate, int flags);
    boolean EnumWindows(WNDENUMPROC lpEnumFunc, Pointer arg);
    boolean EnumChildWindows(HWND parent, WNDENUMPROC callback, LPARAM info);
    boolean SetForegroundWindow(HWND in);
    boolean EnumWindows(WinUser.WNDENUMPROC lpEnumFunc, Pointer arg);
    boolean DestroyWindow(HWND hWnd);
    boolean IsWindowVisible(HWND hWnd);
    boolean IsWindow(HWND hWnd);
    boolean ShowWindow(HWND hWnd, int nCmdShow);
    boolean GetCursorPos(long[] lpPoint); //use macros POINT_X() and POINT_Y() on long lpPoint[0]
    boolean ClientToScreen(HWND hWnd, long[] lpPoint);//use macros POINT_X() and POINT_Y() on long lpPoint[0]
    boolean ScreenToClient(HWND hWnd, long[] lpPoint);//use macros POINT_X() and POINT_Y() on long lpPoint[0]
    boolean IsMenu(HMENU hMenu);
    boolean TrackPopupMenu(HMENU hMenu, int uFlags, int x, int y, int nReserved, HWND hWnd, long prcRect);
    boolean GetMenuItemRect(HWND hWnd, HMENU hMenu, int uItem, RECT rect);

    void SwitchToThisWindow(HWND hWnd, boolean fAltTab);

    long GetWindowLong(HWND hWnd, int index);

    interface WNDENUMPROC extends StdCallCallback {boolean callback(HWND hWnd, Pointer arg);}
}

我认为这可能有帮助:

import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;

还有这个函数:

public long getWindowId(String title) {
  WinDef.HWND hwnd = User32.INSTANCE.FindWindow(null, title);

  return Pointer.nativeValue(hwnd.getPointer());
}