如何获得当前最大化的窗口标题意味着打开?

时间:2014-06-24 05:39:27

标签: java

我正在创建一个应用程序来跟踪PC上当前打开的窗口的记录。我从堆栈溢出中复制了这段代码。我实现了它并使用threads每15秒执行一次。我正在创建一个应用程序,我想只得到顶部窗口标题意味着当前最大化的窗口标题。      ================================================== ===========================

 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()
 {
  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) 
    {      
        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);
 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;
 }
public String toString()
{
 return String.format("(%d,%d)-(%d,%d) : \"%s\"",
 rect.left ,rect.top,rect.right,rect.bottom,title);
}
}

 OUTPUT IS :

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

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

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

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

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

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

In this output, it gives the current maximized means opened window in 4th line of output i.e. 
Netbeans. How to get name of only that window not all.

**What these two block are doing??

  while (top!=0)
  {
  order.add(top);
  top = User32.instance.GetWindow(top, User32.GW_HWNDNEXT);
  }

** AND ** 
 User32.instance.EnumWindows(new WndEnumProc()
{
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
        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);
------------------------------------------------------------------

0 个答案:

没有答案