/// <summary> /// Refresh the list of <see cref="Window"/> /// </summary> /// <param name="refreshDesktopWindowOnly">Defines whether we must only refresh the desktop window information.</param> internal void RefreshWindows(bool refreshDesktopWindowOnly = false) { if (!refreshDesktopWindowOnly) { WindowsList.Clear(); WindowsListChanged?.Invoke(this, new EventArgs()); } _refreshDesktopWindowOnly = refreshDesktopWindowOnly; Requires.VerifySucceeded(NativeMethods.EnumWindows(EnumWindowsCallback, 0)); }
/// <summary> /// Callback used everytime a window is detected when we call EnumWindows /// </summary> /// <param name="windowHandle">The handle of the window</param> /// <param name="lParam">The application-defined value given in EnumWindows or EnumDesktopWindows.</param> /// <returns>True to continue to search for windows</returns> private bool EnumWindowsCallback(IntPtr windowHandle, int lParam) { // Check if the window is visible, has a border or not, has a title, is the main window of the app (so we ignore the child windows and dialog box) var windowStyle = NativeMethods.GetWindowLongA(windowHandle, Consts.GWL_STYLE); var isApplicationWindow = (windowStyle & Consts.TARGETWINDOW) == Consts.TARGETWINDOW; var isDesktopWindow = windowHandle == NativeMethods.GetShellWindow(); var isVisible = NativeMethods.IsWindowVisible(windowHandle) && NativeMethods.IsWindow(windowHandle); var isChainVisible = IsWindowChainVisible(windowHandle); var isInIgnoreList = _windowHandlesToIgnore.Contains(windowHandle); if (!isVisible || !isChainVisible || isInIgnoreList || (!isApplicationWindow && !isDesktopWindow) || (_refreshDesktopWindowOnly && !isDesktopWindow)) { return(true); //continue enumeration } if (isDesktopWindow) { DesktopWindow = new Window(windowHandle, string.Empty, null, string.Empty, null, false); return(true); //continue enumeration } var stringBuilder = new StringBuilder(256); NativeMethods.GetWindowText(windowHandle, stringBuilder, stringBuilder.Capacity); if (string.IsNullOrEmpty(stringBuilder.ToString())) { return(true); //continue enumeration } var window = GetWindowInformation(windowHandle); if (window != null) { WindowsList.Add(window); WindowsListChanged?.Invoke(this, new EventArgs()); } return(true); //continue enumeration }