如何在 JAVA 中 return HWND

How to return HWND in JAVA

附件代码根据标题搜索window,如果存在则激活

   public static void ActivateWindow() {
  User32.INSTANCE.EnumWindows(new WNDENUMPROC() {

     @Override
     public boolean callback(HWND hWnd, Pointer arg1) {
        char[] windowText = new char[512];
        User32.INSTANCE.GetWindowText(hWnd, windowText, 512);
        String wText = Native.toString(windowText);

        String title = "Chrome";
            if (wText.contains(title))
            {
            User32.INSTANCE.ShowWindow(hWnd, 9); //activeWindow()
            User32.INSTANCE.SetForegroundWindow(hWnd);   //activeWindow()
            }

        return true;   

     }
  }, null);
}

我的目标是拆分 ActivateWindow() 方法。

IsWindowOpen() 将 return HWND object 如果 window 存在,activateWindow() 将激活 HWND。

我找不到在回调中 return HWND 的方法?

由于 HWND 实际上是 "pointer to something",即 void*,您可能希望在 Java 端使用 jna 的 com.sun.jna.Pointer 类型。

使用 jna 的 win32 平台 API 时 com.sun.jna.platform.win32.WinDef.HWND 更合适,如另一个代码示例 Handle external windows using java. Look at How to get list of all window handles in Java (Using JNA)? 的答案所示。

至少有两种方式

使用实例变量:
如果您使方法成为非静态方法,则可以在回调中访问实例变量。
看下面例子中foundWindow的用法:

public class JNA_Test {
    HWND foundWindow = null;

    public static void main(String[] args) {
        JNA_Test jna = new JNA_Test();
        if(jna.isWindowOpen("chrome")){
            jna.activateWindow();
        }
    }

    public void activateWindow() {
        if(foundWindow != null) {
            User32.INSTANCE.ShowWindow(foundWindow, 9);
            User32.INSTANCE.SetForegroundWindow(foundWindow);
        }
    }

    public boolean isWindowOpen(String title) {
        foundWindow = null;
        User32.INSTANCE.EnumWindows(new WNDENUMPROC() {

            @Override
            public boolean callback(HWND hWnd, Pointer arg1) {
                if(foundWindow == null) {
                    char[] windowText = new char[512];
                    User32.INSTANCE.GetWindowText(hWnd, windowText, 512);
                    String wText = Native.toString(windowText);
                    if (wText.contains(title)) {
                        foundWindow = hWnd;
                    }
                }
                return true;

            }
        }, null);

        return foundWindow != null;
    }
}


使用 JNA 指针:
在这个例子中,不管方法是否是静态的。
查看foundWindowPointer的用法:

public class JNA_Test2 {

    public static void main(String[] args) {
        Pointer foundWindowPointer = new Memory(Pointer.SIZE);
        JNA_Test2.isWindowOpen("chrome", foundWindowPointer);
        if (foundWindowPointer.getPointer(0) != null) {
            HWND foundWindow = new HWND(foundWindowPointer.getPointer(0));
            JNA_Test2.activateWindow(foundWindow);
        }
    }

    public static void activateWindow(HWND foundWindow) {
        if (foundWindow != null) {
            User32.INSTANCE.ShowWindow(foundWindow, 9);
            User32.INSTANCE.SetForegroundWindow(foundWindow);
        }
    }

    public static void isWindowOpen(String title, Pointer foundWindowPointer) {
        User32.INSTANCE.EnumWindows(new WNDENUMPROC() {

            @Override
            public boolean callback(HWND hWnd, Pointer foundWindowPointer) {
                if (foundWindowPointer != null) {
                    char[] windowText = new char[512];
                    User32.INSTANCE.GetWindowText(hWnd, windowText, 512);
                    String wText = Native.toString(windowText);
                    if (wText.contains(title)) {
                        foundWindowPointer.setPointer(0, hWnd.getPointer());
                    }
                }
                return true;

            }
        }, foundWindowPointer);
    }
}