Wednesday, January 12, 2005

You should be aware that it's not feasible to call EnumWindows API in the CF, since the usage of the delegates as a native callbacks is not supported in the v1 (it will be supported in CF v2 though). But there's a way to emulate functionality of this API by using GetWindow and GetWindowText:

public static Window[] EnumerateTopWindows()

{

      ArrayList winList = new ArrayList();

      IntPtr hwnd = IntPtr.Zero;

      Window window = null;

      StringBuilder sb = null;

                 

      // Get the first window

      hwnd = GetActiveWindow();

      hwnd = GetWindow(hwnd, GW_HWNDFIRST);

     

      while(hwnd != IntPtr.Zero)

      {    

            IntPtr parentWin = GetParent(hwnd);

            // Make sure that the window doesn't have a parent

            if ((parentWin == IntPtr.Zero))

            {

                  int length       = GetWindowTextLength(hwnd);

                  // Check if it has caption text

                  if (length > 0)

                  {

                        window = new Window();

                        window.Handle = hwnd;

                        sb = new StringBuilder(length + 1);

                        GetWindowText(hwnd, sb, sb.Capacity);

                        window.Caption = sb.ToString();

                        winList.Add(window);

                  }

            }

                 

            hwnd = GetWindow(hwnd, GW_HWNDNEXT);

      }

      return (Window[])winList.ToArray(typeof(Window));

}

 Download the sample code from here.

EnumerateWindowsProj.zip (5.82 KB)
1/12/2005 5:03:23 PM (GMT Standard Time, UTC+00:00)  #     |