WindowEnumerator

  • Thread starter Thread starter carlmanaster
  • Start date Start date
C

carlmanaster

My application has regular document windows and floating windows above
them. I want to iterate through all windows in z-order. I would have
thought this capability would be built-in to the .net framework, but
everything I can find on the archives says no - you need to use
GetWindow(), thus:

[DllImport("user32", EntryPoint="GetWindow")]
private static extern int GetWindow(int hwnd, int wFlag);

And the most logical thing seems to me to be to create a class that
implements the IEnumerator interface. I suspect I need an additional
external function - maybe GetTopWindow? to get a starting point; call
that in .Reset() and GetWindow() in .MoveNext(). Am I on the right
track? Can anyone provide examples of how to call these functions, eg
what arguments to pass to them, how to coerce their result types into
System.Windows.Forms, how to tell when I've reached the end? Thanks in
advance.

Peace,
--Carl
 
carlmanaster said:
My application has regular document windows and floating windows above
them. I want to iterate through all windows in z-order. I would have
thought this capability would be built-in to the .net framework, but
everything I can find on the archives says no - you need to use
GetWindow(), thus:

[DllImport("user32", EntryPoint="GetWindow")]
private static extern int GetWindow(int hwnd, int wFlag);

And the most logical thing seems to me to be to create a class that
implements the IEnumerator interface. I suspect I need an additional
external function - maybe GetTopWindow? to get a starting point; call
that in .Reset() and GetWindow() in .MoveNext(). Am I on the right
track? Can anyone provide examples of how to call these functions, eg
what arguments to pass to them, how to coerce their result types into
System.Windows.Forms, how to tell when I've reached the end? Thanks in
advance.

Peace,
--Carl

I'd start from looking at one of the examples on the inet, like this:

http://www.codeproject.com/csharp/popupkiller.asp

It shows working with different windows ona popup killer example.

Andrey
 
I've got a class that seems to do the trick. I started with Taylor
Wood's class, here:

<http://www.thecodeproject.com/csharp/windowhider.asp>

and pared it down to just what I wanted; it seems to be working fine.
Enjoy.

using System;
using System.Runtime.InteropServices;
using System.Collections;
using System.Windows.Forms;

namespace Whatever
{
/// <summary>
/// Collection of Forms visible in this application,
/// in current z-order (front to back)
/// </summary>
/// <example>
/// foreach (Form form in new WindowList())
/// {
/// // process forms as needed
/// }
/// </example>

public class WindowList : IEnumerable, IEnumerator
{
[DllImport("user32.dll")]
private static extern int EnumWindows(ewProc ewp, int lParam);

public delegate bool ewProc(int hWnd, int lParam);

private int currentIndex = -1;
private ArrayList windows = new ArrayList();

public WindowList()
{
EnumWindows(new ewProc(AddWindow), 0);
}

private bool AddWindow(int hWnd, int lParam)
{
Control form = Form.FromHandle((IntPtr)hWnd);
if (form != null && form.Visible)
windows.Add(form);
return true;
}

public IEnumerator GetEnumerator()
{
return (IEnumerator)this;
}
public bool MoveNext()
{
currentIndex++;
return currentIndex < windows.Count;
}
public void Reset()
{
currentIndex = -1;
}
public object Current
{
get
{
return windows[currentIndex];
}
}
}
}
 
Back
Top