[HWND的本地@ 0xc41bcc输出到实际窗口ID

问题描述 投票:0回答:1

嘿,我在尝试使此JNA findwindow正常工作时遇到了问题,该问题给了我Windows ID(HWND)而不是一些疯狂的输出(native @ 0xc41bcc)。

我从使用C#的工作中知道,当使用findwindow时,它将给出该找到的窗口的ID,然后我可以将其用于移动/调整/调整该窗口的大小。

我的代码:

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);}
    }
java jna user32 hwnd findwindow
1个回答
0
投票

我认为这可能有所帮助:

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());
}
© www.soinside.com 2019 - 2024. All rights reserved.