/// <summary> /// Finds the "main" Cassini window, which will respond to WM_QUERYENDSESION and WM_CLOSE. /// </summary> /// <param name="processId">A Cassini process ID</param> /// <returns>The IntPtr handle to the Cassini window</returns> static IntPtr FindCassiniWindow(int processId) { IntPtr result = IntPtr.Zero; NativeMethods.EnumThreadWindowsCallback callback = (handle, extraP) => { int num; NativeMethods.GetWindowThreadProcessId(handle, out num); if (num == processId) { // Heuristic: Cassini window titles start with "ASP.NET Development Server" int length = NativeMethods.GetWindowTextLength(handle) + 1; StringBuilder b = new StringBuilder(length); NativeMethods.GetWindowText(handle, b, length); if (b.ToString().StartsWith("ASP.NET Development Server")) { result = handle; return false; } } return true; }; // Search through all windows NativeMethods.EnumWindows(callback, IntPtr.Zero); // Reference the callback to stop it being GC'd before this point. GC.KeepAlive(callback); return result; }
public IntPtr FindMainWindow(int processId) { bestHandle = (IntPtr)0; this.processId = processId; NativeMethods.EnumThreadWindowsCallback callback = new NativeMethods.EnumThreadWindowsCallback(this.EnumWindowsCallback); NativeMethods.EnumWindows(callback, IntPtr.Zero); GC.KeepAlive(callback); return bestHandle; }
private IntPtr FindIEServerWindowHandle(IntPtr ieFrameWindowHandle) { this._hWndFromEnumCallback = IntPtr.Zero; if (!ieFrameWindowHandle.Equals(IntPtr.Zero)) { NativeMethods.EnumThreadWindowsCallback callback = null; callback = new NativeMethods.EnumThreadWindowsCallback(this.EnumChildWindowsCallback); NativeMethods.EnumChildWindows(new HandleRef(this, ieFrameWindowHandle), callback, ieFrameWindowHandle); GC.KeepAlive(callback); } return(this._hWndFromEnumCallback); }
/// <summary> /// Returns an enumerable of windows, filtered by specified predicate match. /// </summary> /// <param name="match">The predicate to filter by.</param> public static IEnumerable <Window> GetWindows(Func <Window, bool> match = null) { var windows = new List <Window>(); NativeMethods.EnumThreadWindowsCallback enumWindows = (hwnd, lParam) => { var window = new Window(hwnd); if (match == null || match(window)) { windows.Add(window); } return(true); }; UnsafeNativeMethods.EnumWindows(enumWindows, IntPtr.Zero); return(windows); }
/// <summary> /// Returns an enumerable of child windows, filtered by specified predicate match. /// </summary> /// <param name="parentWindow">The parent window.</param> /// <param name="match">The predicate to filter by.</param> public static IEnumerable <Window> GetChildWindows(Window parentWindow, Func <Window, bool> match = null) { if (!parentWindow.Exists) { return(new Window[0]); } var windows = new List <Window>(); NativeMethods.EnumThreadWindowsCallback enumWindows = (hwnd, lParam) => { var window = new Window(hwnd); if (match == null || match(window)) { windows.Add(window); } return(true); }; UnsafeNativeMethods.EnumChildWindows(new HandleRef(parentWindow, parentWindow.Handle), enumWindows, IntPtr.Zero); return(windows); }
public static extern bool EnumChildWindows( HandleRef hWndParent, NativeMethods.EnumThreadWindowsCallback lpEnumFunc, IntPtr extraData );
public static extern bool EnumWindows( NativeMethods.EnumThreadWindowsCallback callback, IntPtr extraData );