Knowledge Base Nr: 00292 enumwin.cs - http://www.swe-kaiser.de

c#: enumerate active windows

  
[DllImport("user32")]
public static extern int EnumWindows(CallBack x, int y);
[DllImport("user32")]
public static extern int GetWindowText(int hwnd, StringBuilder title, int maxLen);
[DllImport("user32")]
public static extern bool IsWindowVisible(int hwnd);

public delegate bool CallBack(int hwnd, int lParam);

public static bool Report(int hwnd, int lParam)
{
StringBuilder title = new StringBuilder(2000);

int i = GetWindowText(hwnd, title, 2000);
bool b = IsWindowVisible(hwnd);

if (b && (title.ToString() != ""))
{
Console.Write("Window handle is #" + title + "#" + hwnd);
}
return true;
}

private void buttonWndList_Click(object sender, EventArgs e)
{
CallBack myCallBack = new CallBack(Report);
EnumWindows(myCallBack, 0);
}