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

时间:2014-06-27 08:02:10

标签: 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"

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

1 个答案:

答案 0 :(得分:1)

要获得具有标题的窗口,请使用以下代码替换第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));
}