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.
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.