java - 获取当前可见窗口标题的 Null 值

标签 java jna

我创建了这个程序来获取当前可见的窗口名称。这给出了系统上打开的所有窗口的名称。

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class WindowNames {
    String s3;
    static int arraySize = 10;
    static int arrayGrowth = 2;
    static String[] m = new String[arraySize];
    static int count = 0;

    public static void main(String[] args) {

        final List<WindowInfo> inflList = new ArrayList<WindowInfo>();
        final List<Integer> order = new ArrayList<Integer>();
        int top = User32.instance.GetTopWindow(0);
        while (top != 0) {
            order.add(top);
            top = User32.instance.GetWindow(top, User32.GW_HWNDNEXT);
        }
        User32.instance.EnumWindows(new WndEnumProc() {
            @Override
            public boolean callback(int hWnd, int lParam) {
                if (User32.instance.IsWindowVisible(hWnd)) {
                    RECT r = new RECT();
                    User32.instance.GetWindowRect(hWnd, r);
                    if (r.left > -32000) { // minimized
                        PointerType hwnd = User32.instance.GetForegroundWindow();
                        byte[] buffer = new byte[1024];
                        User32.instance.GetWindowTextA(hWnd, buffer, buffer.length);
                        String title = Native.toString(buffer);

                        if (m.length == count) {
                            // expand list
                            m = Arrays.copyOf(m, m.length + arrayGrowth);
                        }
                        m[count] = Native.toString(buffer);
                        System.out.println("title====" + m[count]);
                        count++;

                        inflList.add(new WindowInfo(hWnd, r, title));

                    }

                }
                return true;
            }
        }, 0);

        Collections.sort(inflList, new Comparator<WindowInfo>() {
            public int compare(WindowInfo o1, WindowInfo o2) {
                return order.indexOf(o1.hwnd) - order.indexOf(o2.hwnd);
            }
        });
        for (WindowInfo w : inflList) {
            System.out.println(w);
        }

    }

    public static interface WndEnumProc extends StdCallLibrary.StdCallCallback {
        boolean callback(int hWnd, int lParam);
    }

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

        boolean EnumWindows(WndEnumProc wndenumproc, int lParam);

        boolean IsWindowVisible(int hWnd);

        int GetWindowRect(int hWnd, RECT r);

        void GetWindowTextA(int hWnd, byte[] buffer, int buflen);

        int GetTopWindow(int hWnd);

        int GetWindow(int hWnd, int flag);

        HWND GetForegroundWindow();

        final int GW_HWNDNEXT = 2;

    }

    public static class RECT extends Structure {
        public int left, top, right, bottom;
    }

    public static class WindowInfo {
        int hwnd;
        RECT rect;
        String title;

        public WindowInfo(int hwnd, RECT rect, String title) {
            this.hwnd = hwnd;
            this.rect = rect;
            this.title = title;
        }

        @Override
        public String toString() {
            return String.format("(%d,%d)-(%d,%d) : \"%s\"", rect.left, rect.top, rect.right, rect.bottom, title);
        }
    }

    public static void sendGet(String last1, String[] get) throws Exception {

        for (int t = 0; t < get.length; t++) {
            if (get[t] != null) {
                String url = "http://localhost/add_windows.php?username=" + last1 + "&windowname=" + get[t];
                final String USER_AGENT = "Mozilla/5.0";
                URL obj = new URL(url);
                HttpURLConnection con = (HttpURLConnection) obj.openConnection();
                con.setRequestMethod("GET");
                con.setRequestProperty("User-Agent", USER_AGENT);
                int responseCode = con.getResponseCode();
                System.out.println("\nSending 'GET' request to URL : " + url);
                System.out.println("Response Code : " + responseCode);
                BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String inputLine;
                StringBuffer response = new StringBuffer();
                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();
                String r = response.toString();
                System.out.println("String " + r);
            }
        }
    }
}

输出为:

(0,728)-(54,768) : "Start"

(0,728)-(1366,768) : ""

(0,0)-(0,0) : ""

(-8,-8)-(1374,736) : "Comp_Watch - NetBeans IDE 7.1.2"

(-8,-8)-(1374,736) : "PHP Error Handling - Google Chrome"

(-8,-8)-(1374,736) : "knowledge"

(0,0)-(0,0) : "{94F11419-869E-47aa-9563-F48591285CAD}"

(0,0)-(1366,768) : "Program Manager"

在输出的第二行和第三行中,它给出了空值。我已在另一个系统上运行此程序,并且在该系统上这些空值超过 2 个(4-5 个空值)。我不明白哪些进程对应于这些空值。请任何人都可以建议我如何获取窗口标题而不是空值。

最佳答案

要获取具有标题的窗口,请将第 36 行到 46 行的代码替换为以下代码:

if(!(title.equals("")))
{
    if (m.length == count) {
    // expand list
        m = Arrays.copyOf(m, m.length + arrayGrowth);
    }
    m[count] = Native.toString(buffer);
    System.out.println("title====" + m[count]);
    count++;
    inflList.add(new WindowInfo(hWnd, r, title));
}

关于java - 获取当前可见窗口标题的 Null 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24446861/

相关文章:

java - 如何使用 JNA 读取 Windows 安全事件?

java - 可序列化类和 == 运算符

java - 为一个方法编写单元测试,该方法调用java中同一类的其他私有(private)方法。如何模拟私有(private)方法

java - 写入 stderr 时,共享 C 库 (JNI) 在 jetty 下挂起

android - 如何使用JNA调用C的_IOR宏?

java - JNA 标记联合映射

c - JNA 简单函数调用适用于 Linux (x64),但不适用于 Windows (x86)

java - 无法使用 Java Spring Boot 通过 Rest API 在两个 Docker 容器之间进行通信

java - 加工过程中运动平稳

java - 静态工厂 builder 的麻烦