using System.Runtime.InteropServices; using System.Text; namespace DummyCursor.Spy { internal class WndFinder { [DllImport("user32.dll", CharSet = CharSet.Unicode)] private static extern int GetWindowText(IntPtr hWnd, StringBuilder strText, int maxCount); [DllImport("user32.dll", CharSet = CharSet.Unicode)] private static extern int GetWindowTextLength(IntPtr hWnd); [DllImport("user32.dll")] private static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam); // Delegate to filter which windows to include public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam); /// Get the text for the window pointed to by hWnd public static string GetWindowText(IntPtr hWnd) { int size = GetWindowTextLength(hWnd); if (size > 0) { var builder = new StringBuilder(size + 1); 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(EnumWindowsProc filter) { List windows = new List(); void EnumChild(IntPtr hWnd, IntPtr lParam) { EnumChildWindows(hWnd, delegate (IntPtr hWndChild, IntPtr lParamChild) { if (filter(hWndChild, lParam)) { // only add the windows that pass the filter windows.Add(hWndChild); } EnumChild(hWndChild, lParamChild); return true; }, IntPtr.Zero); } EnumWindows(delegate (IntPtr wnd, IntPtr param) { if (filter(wnd, param)) { // only add the windows that pass the filter windows.Add(wnd); } EnumChild(wnd, param); // but return true here so that we iterate all windows return true; }, IntPtr.Zero); return windows; } /// Find all windows that contain the given title text /// The text that the window title must contain. public static IEnumerable FindWindowsWithText(string titleText) { return FindWindows(delegate (IntPtr wnd, IntPtr param) { return GetWindowText(wnd).Contains(titleText); }); } } }