Получение caption-ов\тайтлов всех запущенных приложений - C#
Формулировка задачи:
Добрый день. Нужно получить тайтлы для всех запущенных приложений, у которых они есть. Сейчас пользуюсь этим :
Process.MainWindowTitle, и все бы прекрасно, только она не выводит тайтлы неактивных окон хрома и ФФ.
Так вот, товарищи, не подскажите, как бы мне это реализовать в консольном приложении? В гугл не отсылайте, был там. Форум тоже изучал(и даже нашел готовое решение для хрома на WinAPI, но оно не работало)
Решение задачи: «Получение caption-ов\тайтлов всех запущенных приложений»
textual
Листинг программы
class Program
{
private static void Main(string[] args)
{
foreach (KeyValuePair<IntPtr, string> window in OpenWindowGetter.GetOpenWindows())
{
IntPtr handle = window.Key;
string title = window.Value;
Console.WriteLine("{0}: {1}", handle, title);
}
Console.ReadLine();
}
}
/// <summary>Contains functionality to get all the open windows.</summary>
public static class OpenWindowGetter
{
/// <summary>Returns a dictionary that contains the handle and title of all the open windows.</summary>
/// <returns>A dictionary that contains the handle and title of all the open windows.</returns>
public static IDictionary<IntPtr, string> GetOpenWindows()
{
IntPtr shellWindow = GetShellWindow();
Dictionary<IntPtr, string> windows = new Dictionary<IntPtr, string>();
EnumWindows(delegate (IntPtr IntPtr, int lParam)
{
if (IntPtr == shellWindow) return true;
if (!IsWindowVisible(IntPtr)) return true;
int length = GetWindowTextLength(IntPtr);
if (length == 0) return true;
StringBuilder builder = new StringBuilder(length);
GetWindowText(IntPtr, builder, length + 1);
windows[IntPtr] = builder.ToString();
return true;
}, 0);
return windows;
}
private delegate bool EnumWindowsProc(IntPtr IntPtr, int lParam);
[DllImport("USER32.DLL")]
private static extern bool EnumWindows(EnumWindowsProc enumFunc, int lParam);
[DllImport("USER32.DLL")]
private static extern int GetWindowText(IntPtr IntPtr, StringBuilder lpString, int nMaxCount);
[DllImport("USER32.DLL")]
private static extern int GetWindowTextLength(IntPtr IntPtr);
[DllImport("USER32.DLL")]
private static extern bool IsWindowVisible(IntPtr IntPtr);
[DllImport("USER32.DLL")]
private static extern IntPtr GetShellWindow();
}