using System; using System.Collections.Generic; using System.Diagnostics; using System.Text; namespace Widow { public static class Helpers { #region Public Methods /// Get the text for the window pointed to by hWnd public static string GetWindowText(IntPtr hWnd) { var size = Natives.GetWindowTextLength(hWnd); if (size > 0) { var builder = new StringBuilder(size + 1); Natives.GetWindowText(hWnd, builder, builder.Capacity); return builder.ToString(); } return string.Empty; } /// Find all windows that match the given filter /// /// A delegate that returns true for windows /// that should be returned and false for windows that should /// not be returned /// public static IEnumerable FindWindows(Natives.EnumWindowsProc filter) { var windows = new List(); Natives.EnumWindows(delegate(IntPtr wnd, IntPtr param) { if (filter(wnd, param)) { // only add the windows that pass the filter windows.Add(wnd); } // but return true here so that we iterate all windows return true; }, IntPtr.Zero); return windows; } public static string GetWindowClass(IntPtr hWnd) { var windowClass = new StringBuilder(256); Natives.GetClassName(hWnd, windowClass, windowClass.Capacity); return windowClass.ToString(); } public static string GetWindowTitle(IntPtr hWnd) { var length = Natives.GetWindowTextLength(hWnd) + 1; var title = new StringBuilder(length); Natives.GetWindowText(hWnd, title, length); return title.ToString(); } public static string GetProcessName(IntPtr hWnd) { Natives.GetWindowThreadProcessId(hWnd, out var pid); try { var process = Process.GetProcessById((int) pid); return process.ProcessName; } catch { return string.Empty; } } #endregion } }