I need to list all the open, visible windows. XP.

K

kamiikoneko

I need to list all the open visible windows, like what shows up in the
taskbar/alt-tab window. Saw that microsoft had an explanation of how
to make an alt-tab window programmatically but left out any code
example for getting the window and deciding if it's task-bar visible
or not. I use this code from an example to get ALL windows, but
there's alot of invisible windows and junk that gets printed out and i
JUST want the windows that appear in the task bar to get printed
out......


const int MAXTITLE = 255;

private static ArrayList mTitlesList;

private delegate bool EnumDelegate(IntPtr hWnd, int lParam);

[DllImport("user32.dll", EntryPoint = "EnumDesktopWindows",
ExactSpelling = false, CharSet = CharSet.Auto, SetLastError =
true)]
private static extern bool _EnumDesktopWindows(IntPtr
hDesktop,
EnumDelegate lpEnumCallbackFunction, IntPtr lParam);

[DllImport("user32.dll", EntryPoint = "GetWindowText",
ExactSpelling = false, CharSet = CharSet.Auto, SetLastError =
true)]
private static extern int _GetWindowText(IntPtr hWnd,
StringBuilder lpWindowText, int nMaxCount);


public TestTracker()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
string[] desktopWindowsCaptions = GetDesktopWindowsCaptions
();
foreach (string caption in desktopWindowsCaptions)
{
Console.WriteLine(caption);
}
}
/// <summary>
/// Returns the caption of all desktop windows.
/// </summary>
public static string[] GetDesktopWindowsCaptions()
{
mTitlesList = new ArrayList();
EnumDelegate enumfunc = new EnumDelegate(EnumWindowsProc);
IntPtr hDesktop = IntPtr.Zero; // current desktop
bool success = _EnumDesktopWindows(hDesktop, enumfunc,
IntPtr.Zero);

if (success)
{
// Copy the result to string array
string[] titles = new string[mTitlesList.Count];
mTitlesList.CopyTo(titles);
return titles;
}
else
{
// Get the last Win32 error code
int errorCode = Marshal.GetLastWin32Error();

string errorMessage = String.Format(
"EnumDesktopWindows failed with code {0}.",
errorCode);
throw new Exception(errorMessage);
}
}

private static bool EnumWindowsProc(IntPtr hWnd, int lParam)
{
string title = GetWindowText(hWnd);
mTitlesList.Add(title);
return true;
}

/// <summary>
/// Returns the caption of a windows by given HWND identifier.
/// </summary>
public static string GetWindowText(IntPtr hWnd)
{
StringBuilder title = new StringBuilder(MAXTITLE);
int titleLength = _GetWindowText(hWnd, title,
title.Capacity + 1);
title.Length = titleLength;

return title.ToString();
}


private static bool EnumWindowsProc(IntPtr hWnd, int lParam)
{
string title = GetWindowText(hWnd);
mTitlesList.Add(title);
return true;
}

/// <summary>
/// Returns the caption of a windows by given HWND identifier.
/// </summary>
public static string GetWindowText(IntPtr hWnd)
{
StringBuilder title = new StringBuilder(MAXTITLE);
int titleLength = _GetWindowText(hWnd, title,
title.Capacity + 1);
title.Length = titleLength;

return title.ToString();
}

Can someone give me that one or two lines of code that will determine
if the window is one that would show up in the task bar?
Also, is there a way to determine which one has focus?

afterwards I can just watch windows messages to see when new windows
are created and when focus is grabbed, but this initial state I am
having trouble with.
 
K

kamiikoneko

I need to list all the open visible windows, like what shows up in the
taskbar/alt-tab window.  Saw that microsoft had an explanation of how
to make an alt-tab window programmatically but left out any code
example for getting the window and deciding if it's task-bar visible
or not.  I use this code from an example to get ALL windows, but
there's alot of invisible windows and junk that gets printed out and i
JUST want the windows that appear in the task bar to get printed
out......

        const int MAXTITLE = 255;

        private static ArrayList mTitlesList;

        private delegate bool EnumDelegate(IntPtr hWnd, int lParam);

        [DllImport("user32.dll", EntryPoint = "EnumDesktopWindows",
         ExactSpelling = false, CharSet = CharSet.Auto, SetLastError =
true)]
        private static extern bool _EnumDesktopWindows(IntPtr
hDesktop,
        EnumDelegate lpEnumCallbackFunction, IntPtr lParam);

        [DllImport("user32.dll", EntryPoint = "GetWindowText",
         ExactSpelling = false, CharSet = CharSet.Auto, SetLastError =
true)]
        private static extern int _GetWindowText(IntPtr hWnd,
        StringBuilder lpWindowText, int nMaxCount);

        public TestTracker()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string[] desktopWindowsCaptions = GetDesktopWindowsCaptions
();
            foreach (string caption in desktopWindowsCaptions)
            {
                Console.WriteLine(caption);
            }
        }
/// <summary>
        /// Returns the caption of all desktop windows.
        /// </summary>
        public static string[] GetDesktopWindowsCaptions()
        {
            mTitlesList = new ArrayList();
            EnumDelegate enumfunc = new EnumDelegate(EnumWindowsProc);
            IntPtr hDesktop = IntPtr.Zero; // current desktop
            bool success = _EnumDesktopWindows(hDesktop, enumfunc,
IntPtr.Zero);

            if (success)
            {
                // Copy the result to string array
                string[] titles = new string[mTitlesList.Count];
                mTitlesList.CopyTo(titles);
                return titles;
            }
            else
            {
                // Get the last Win32 error code
                int errorCode = Marshal.GetLastWin32Error();

                string errorMessage = String.Format(
                "EnumDesktopWindows failed with code {0}.",
errorCode);
                throw new Exception(errorMessage);
            }
        }

        private static bool EnumWindowsProc(IntPtr hWnd, int lParam)
        {
            string title = GetWindowText(hWnd);
            mTitlesList.Add(title);
            return true;
        }

        /// <summary>
        /// Returns the caption of a windows by given HWND identifier.
        /// </summary>
        public static string GetWindowText(IntPtr hWnd)
        {
            StringBuilder title = new StringBuilder(MAXTITLE);
            int titleLength = _GetWindowText(hWnd, title,
title.Capacity + 1);
            title.Length = titleLength;

            return title.ToString();
        }

       private static bool EnumWindowsProc(IntPtr hWnd, int lParam)
        {
            string title = GetWindowText(hWnd);
            mTitlesList.Add(title);
            return true;
        }

        /// <summary>
        /// Returns the caption of a windows by given HWND identifier.
        /// </summary>
        public static string GetWindowText(IntPtr hWnd)
        {
            StringBuilder title = new StringBuilder(MAXTITLE);
            int titleLength = _GetWindowText(hWnd, title,
title.Capacity + 1);
            title.Length = titleLength;

            return title.ToString();
        }

Can someone give me that one or two lines of code that will determine
if the window is one that would show up in the task bar?
Also, is there a way to determine which one has focus?

afterwards I can just watch windows messages to see when new windows
are created and when focus is grabbed, but this initial state I am
having trouble with.

Also, C# is preferred.
 
F

Family Tree Mike

I need to list all the open visible windows, like what shows up in the
taskbar/alt-tab window. Saw that microsoft had an explanation of how
to make an alt-tab window programmatically but left out any code
example for getting the window and deciding if it's task-bar visible
or not. I use this code from an example to get ALL windows, but
there's alot of invisible windows and junk that gets printed out and i
JUST want the windows that appear in the task bar to get printed
out......


How about the following, which does not need to use p/invoke:

using System;
using System.Diagnostics;

public class WinList
{

public static void Main()
{
foreach (Process p in Process.GetProcesses())
{
string title = p.MainWindowTitle;
if (title != string.Empty) Console.Out.WriteLine(title);
}

Console.Out.WriteLine("Done");
Console.In.ReadLine();
}
}
 
K

kamiikoneko

How about the following, which does not need to use p/invoke:

using System;
using System.Diagnostics;

public class WinList
{

    public static void Main()
    {
        foreach (Process p in Process.GetProcesses())
        {
            string title = p.MainWindowTitle;
            if (title != string.Empty) Console.Out.WriteLine(title);
        }

        Console.Out.WriteLine("Done");
        Console.In.ReadLine();
    }

}

That works to a degree, but I need to get all the child windows as
well. Say if Steam has 8 windows open, I need to know. I also need
to be notified when an application gains focus. If there is a way to
get all windows for a process with the method you just gave me, then
it might be useful...otherwise...still need an answer

Thanks for your answer Mike,
Clayton
 
F

Family Tree Mike

That works to a degree, but I need to get all the child windows as
well. Say if Steam has 8 windows open, I need to know. I also need
to be notified when an application gains focus. If there is a way to
get all windows for a process with the method you just gave me, then
it might be useful...otherwise...still need an answer

Thanks for your answer Mike,
Clayton


Remember though that some children windows of a main form do not necessarily
have a name. Also, some windows are toolbars and panels that are parts of
the window itself. I am using windows mail to answer this, and when I run
the following, there are several windows listed for windows mail, including
toolbars and search boxes. The following code may get you closer to what
you are after though.

As to changing focus windows, I'm not sure how to go about that.

This code heavily borrows from examples at http://www.pinvoke.net/

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Text;

public class WinList
{
private delegate bool EnumWindowsProc(IntPtr hWnd,
IntPtr lParam);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool EnumChildWindows
(IntPtr hwndParent, EnumWindowsProc lpEnumFunc,
IntPtr lParam);

public static List<IntPtr> GetChildWindows(IntPtr parent)
{
List<IntPtr> result = new List<IntPtr>();
GCHandle listHandle = GCHandle.Alloc(result);
try
{
EnumWindowsProc childProc =
new EnumWindowsProc(EnumWindow);
EnumChildWindows(parent, childProc,
GCHandle.ToIntPtr(listHandle));
}
finally
{
if (listHandle.IsAllocated) listHandle.Free();
}
return result;
}

private static bool EnumWindow(IntPtr handle, IntPtr pointer)
{
GCHandle gch = GCHandle.FromIntPtr(pointer);
List<IntPtr> list = gch.Target as List<IntPtr>;
if (list == null)
{
throw new InvalidCastException
("GCHandle Target could not be cast as List<IntPtr>");
}
list.Add(handle);
return true;
}

[DllImport("user32.dll", EntryPoint = "GetWindowText",
ExactSpelling = false, CharSet = CharSet.Auto,
SetLastError = true)]
public static extern int _GetWindowText
(IntPtr hWnd, StringBuilder lpWindowText, int nMaxCount);


public static void Main()
{
foreach (Process p in Process.GetProcesses())
{
IntPtr hwnd = p.MainWindowHandle;
string title = p.MainWindowTitle;

if (title != string.Empty)
{
Console.Out.WriteLine(title);
List<IntPtr> children = WinList.GetChildWindows(hwnd);
foreach (IntPtr child in children)
{
StringBuilder sb = new StringBuilder(255);
WinList._GetWindowText(child, sb, 255);
Console.Out.WriteLine
(string.Format(" Child hwnd: {0} - [{1}]",
child, sb.ToString()));
}
}
}

Console.Out.WriteLine("Done");
Console.In.ReadLine();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top