Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在JAVA中返回HWND_Java_Java Native Interface_Jna - Fatal编程技术网

如何在JAVA中返回HWND

如何在JAVA中返回HWND,java,java-native-interface,jna,Java,Java Native Interface,Jna,附加的代码根据窗口标题搜索窗口,并在存在时激活它 public static void ActivateWindow() { User32.INSTANCE.EnumWindows(new WNDENUMPROC() { @Override public boolean callback(HWND hWnd, Pointer arg1) { char[] windowText = new char[512]; User32.INST

附加的代码根据窗口标题搜索窗口,并在存在时激活它

   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()将返回HWND对象,activateWindow()将激活HWND


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

因为
HWND
实际上是一个“指向某物的指针”,即
void*
您可能需要在Java端使用jna的
com.sun.jna.pointer
类型


在使用jna的win32平台API时,更合适的方法是
com.sun.jna.platform.win32.WinDef.HWND
,如问题的答案所示。查看另一个代码示例。

至少有两种方法

使用实例变量:
如果将方法设置为非静态,则可以访问回调中的实例变量。
查看以下示例中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);
    }
}