2014-06-27 38 views
1

我已創建此程序以獲取當前可見窗口名稱。這給出了在系統上打開的所有窗口的名稱。獲取當前可見窗口標題的空值

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空值)。我不明白哪些進程對應於這些空值。請任何人都可以建議我如何獲得窗口標題,而不是空值。

+0

你看了[GetWindowText函數](http://msdn.microsoft.com/en- us/library/windows/desktop/ms633520%28v = vs.85%29.aspx)文檔? 它說:「將指定窗口的標題欄(如果有的話)的文本複製到緩衝區中。如果指定的窗口是控件,控件的文本將被複制。但是,GetWindowText無法檢索文本控制另一個應用程序「。 因此,不是所有的窗口都有標題,即使一個窗口有標題,您可能無法檢索它... – DavidPostill

+0

請問您能否告訴我GetWindowText和GetWindowTextA是一樣的? – JavaFresher

+0

爲什麼不在我給你的鏈接閱讀docuentation?它還指出「Unicode和ANSI名稱 \t GetWindowTextW(Unicode)和GetWindowTextA(ANSI)」 – DavidPostill

回答

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)); 
} 
+0

謝謝alott..It的作品。 – JavaFresher