如何在JAVA中返回HWND [英] How to return HWND in JAVA

查看:72
本文介绍了如何在JAVA中返回HWND的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

附带的代码根据其标题搜索窗口,如果存在则将其激活.

The attached code search for a window according to its title and activate it if exist.

   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()方法.

My goal is to split ActivateWindow() method .

如果窗口存在,IsWindowOpen()将返回HWND对象,而activateWindow()将激活HWND.

IsWindowOpen() will return HWND object if window exist, and activateWindow() will activate the HWND.

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

I cannot find a way to return HWND within a callback?

推荐答案

至少有两种方法

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

There are at least two ways

Using instance variables:
If you make your methods non-static, you can access instance variables inside your callback.
Look at the usages of foundWindow in the following example:

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的用法:


Using JNA Pointer:
In this example it is no matter whether the methods are static or not.
Look at the usages of 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);
    }
}

这篇关于如何在JAVA中返回HWND的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆