public void TestIcon_GetIcon_Null() { var window = InteropWindowQuery.GetDesktopWindow(); var icon = window.GetIcon <BitmapSource>(); Assert.Null(icon); }
public void AddCaptureWindowMenuItems(ToolStripMenuItem menuItem, EventHandler eventHandler) { menuItem.DropDownItems.Clear(); // check if thumbnailPreview is enabled and DWM is enabled var thumbnailPreview = _coreConfiguration.ThumnailPreview && Dwm.IsDwmEnabled; foreach (var window in InteropWindowQuery.GetTopLevelWindows().Concat(AppQuery.WindowsStoreApps.ToList())) { var title = window.GetCaption(); if (title == null) { continue; } if (title.Length > _coreConfiguration.MaxMenuItemLength) { title = title.Substring(0, Math.Min(title.Length, _coreConfiguration.MaxMenuItemLength)); } var captureWindowItem = menuItem.DropDownItems.Add(title); captureWindowItem.Tag = window; captureWindowItem.Image = window.GetDisplayIcon(ContextMenuDpiHandler.Dpi > DpiHandler.DefaultScreenDpi); captureWindowItem.Click += eventHandler; // Only show preview when enabled if (thumbnailPreview) { captureWindowItem.MouseEnter += ShowThumbnailOnEnter; captureWindowItem.MouseLeave += HideThumbnailOnLeave; } } }
public void TestIcon_GetIcon() { var window = InteropWindowQuery.GetActiveWindow(); var icon = window.GetIcon <BitmapSource>(); Assert.NotNull(icon); }
public void TestGetClassname() { var desktopHandle = InteropWindowQuery.GetDesktopWindow(); var classname = User32Api.GetClassname(desktopHandle.Handle); Assert.Equal("#32769", classname); }
private void ButtonCapture_Click(object sender, EventArgs e) { var screenbounds = WindowCapture.GetScreenBounds(); var capture = new Capture(); capture.CaptureDetails.CaptureMode = CaptureMode.Region; WindowCapture.CaptureRectangleFromDesktopScreen(capture, screenbounds); var windows = InteropWindowQuery.GetTopLevelWindows().ToList(); var form = new CaptureForm(CoreConfiguration.Instance, capture, windows); form.ShowDialog(); }
/// <summary> /// Test some of the InteropWindowQuery logic by finding the taskbar and the clock on it. /// </summary> /// <returns></returns> //[Fact] public void TestTaskbarInfo() { var systray = InteropWindowQuery.GetTopWindows().FirstOrDefault(window => window.GetClassname() == "Shell_TrayWnd"); Assert.NotNull(systray); var clock = systray.GetChildren().FirstOrDefault(window => window.GetClassname() == "TrayClockWClass"); Assert.NotNull(clock); var info = clock.GetInfo(); Assert.True(info.ClientBounds.Width * info.ClientBounds.Height > 0); }
private void TestGetTopLevelWindows() { var foundWindow = false; foreach (var window in InteropWindowQuery.GetTopWindows().Where(window => window.IsVisible())) { foundWindow = true; Log.Debug().WriteLine("{0}", window.Dump()); break; } Assert.True(foundWindow); }
/// <summary> /// Get the icon for a hWnd /// </summary> /// <typeparam name="TIcon">The return type for the icon, can be Icon, Bitmap or BitmapSource</typeparam> /// <param name="window">IInteropWindow</param> /// <param name="useLargeIcons">true to try to get a big icon first</param> /// <returns>TIcon</returns> public static TIcon GetIcon <TIcon>(this IInteropWindow window, bool useLargeIcons = false) where TIcon : class { if (window.IsApp()) { return(IconHelper.GetAppLogo <TIcon>(window)); } var icon = GetIconFromWindow <TIcon>(window, useLargeIcons); if (icon != null) { return(icon); } var processId = window.GetProcessId(); // Try to get the icon from the process file itself, if we can query the path var processPath = Kernel32Api.GetProcessPath(processId); if (processPath != null) { return(IconHelper.ExtractAssociatedIcon <TIcon>(processPath, useLargeIcon: useLargeIcons)); } // Look at the windows of the other similar named processes using (var process = Process.GetProcessById(processId)) { var processName = process.ProcessName; foreach (var possibleParentProcess in Process.GetProcessesByName(processName)) { var parentProcessWindow = InteropWindowFactory.CreateFor(possibleParentProcess.MainWindowHandle); icon = GetIconFromWindow <TIcon>(parentProcessWindow, useLargeIcons); if (icon != null) { return(icon); } possibleParentProcess.Dispose(); } } // Try to find another window, which belongs to the same process, and get the icon from there foreach (var otherWindow in InteropWindowQuery.GetTopWindows().Where(interopWindow => interopWindow.GetProcessId() == processId)) { if (otherWindow.Handle == window.Handle) { continue; } icon = GetIconFromWindow <TIcon>(otherWindow, useLargeIcons); if (icon != null) { return(icon); } } // Nothing found, REALLY! return(default(TIcon)); }
/// <summary> /// Return an IEnumerable with the currently opened IE urls /// </summary> /// <returns></returns> public static IEnumerable <string> GetIEUrls() { // Find the IE window foreach (var ieWindow in InteropWindowQuery.GetTopWindows().Where(window => window.GetClassname() == "IEFrame")) { var directUiWd = GetDirectUi(ieWindow); if (directUiWd == null) { continue; } var ieAccessible = new Accessible(directUiWd.Handle); foreach (var url in ieAccessible.IETabUrls) { yield return(url); } } }
public void TestIcon_GetIcon() { // Start a process to test against using (var process = Process.Start("notepad.exe")) { // Make sure it's started Assert.NotNull(process); // Wait until the process started it's message pump (listening for input) process.WaitForInputIdle(); User32Api.SetWindowText(process.MainWindowHandle, "TestIcon_GetIcon"); var window = InteropWindowQuery.GetTopLevelWindows().First(); var icon = window.GetIcon <BitmapSource>(); Assert.NotNull(icon); // Kill the process process.Kill(); } }
/// <summary> /// This takes the height and width of the Popup and will return the location where it should be displayed /// </summary> /// <param name="actualPopupWidth">double</param> /// <param name="actualPopupHeight">double</param> /// <returns>Point</returns> public Point GetPosition(double actualPopupWidth, double actualPopupHeight) { var taskbar = Shell32Api.TaskbarPosition; var taskbarBounds = taskbar.Bounds; var actualSize = new NativeSize((int)actualPopupWidth, (int)actualPopupHeight); // Use the DPI of the desktop var dpi = NativeDpiMethods.GetDpi(InteropWindowQuery.GetDesktopWindow().Handle); actualSize = DpiHandler.ScaleWithDpi(actualSize, dpi); int x, y; // Define the new position switch (taskbar.AppBarEdge) { case AppBarEdges.Left: x = taskbarBounds.Right + _xOffset; y = taskbarBounds.Bottom - _yOffset - actualSize.Height; break; case AppBarEdges.Top: x = taskbarBounds.Right - _xOffset - actualSize.Width; y = taskbarBounds.Bottom + _yOffset; break; case AppBarEdges.Right: x = taskbarBounds.Left - _xOffset - actualSize.Width; y = taskbarBounds.Bottom - _yOffset - actualSize.Height; break; case AppBarEdges.Bottom: default: x = taskbarBounds.Right - _xOffset - actualSize.Width; y = taskbarBounds.Top - _yOffset - actualSize.Height; break; } var position = DpiHandler.UnscaleWithDpi(new NativePoint(x, y), dpi); Log.Debug().WriteLine("Taskbar location {0} at {1}, calculate popup position: {2}", taskbar.AppBarEdge, taskbarBounds, position); return(position); }
/// <summary> /// Test GetWindow /// </summary> /// <returns></returns> //[Fact] private void TestDetectChanges() { var foundWindow = false; IList <IInteropWindow> initialWindows = InteropWindowQuery.GetTopWindows().Where(window => window.IsVisible()).ToList(); while (true) { Thread.Sleep(1000); var newWindow = InteropWindowQuery.GetTopWindows().FirstOrDefault(window => window.IsVisible() && !initialWindows.Contains(window)); if (newWindow != null) { foundWindow = true; Log.Debug().WriteLine("{0}", newWindow.Dump()); break; } } Assert.True(foundWindow); }
public void Startup() { var uiSynchronizationContext = SynchronizationContext.Current; var keyHandler = new KeyCombinationHandler(_pipConfiguration.HotKey) { CanRepeat = false, IsPassThrough = false }; KeyboardHook.KeyboardEvents .Where(keyHandler) .SubscribeOn(uiSynchronizationContext) .ObserveOn(uiSynchronizationContext) .Subscribe(keyboardHookEventArgs => { // Get the current active window var pipSource = InteropWindowQuery.GetForegroundWindow(); while (pipSource.GetParent() != IntPtr.Zero) { pipSource = InteropWindowFactory.CreateFor(pipSource.GetParent()); } // If there is already a form, close it and remove it from the dictionary if (_thumbnailForms.TryGetValue(pipSource.Handle, out var thumbnailForm)) { thumbnailForm.Close(); _thumbnailForms.Remove(pipSource.Handle); return; } // Check if we have a location available if (!_locationPool.HasAvailable) { return; } thumbnailForm = new ThumbnailForm(_pipConfiguration, _locationPool, pipSource.Handle, uiSynchronizationContext); _thumbnailForms[pipSource.Handle] = thumbnailForm; thumbnailForm.Show(); }); }
public void TestApp_TopLevel() { var topLevelWindows = InteropWindowQuery.GetTopLevelWindows().ToList(); Assert.DoesNotContain(topLevelWindows, window => window.IsApp()); }
protected AbstractAdorner(IDrawableContainer owner) { _size.Width = _size.Height = DpiHandler.ScaleWithDpi(5, NativeDpiMethods.GetDpi(InteropWindowQuery.GetDesktopWindow().Handle)); Owner = owner; }
/// <summary> /// Here the logic for capturing the IE Content is located /// </summary> /// <param name="capture">ICapture where the capture needs to be stored</param> /// <param name="windowToCapture">window to use</param> /// <returns>ICapture with the content (if any)</returns> public static ICapture CaptureIe(ICapture capture, IInteropWindow windowToCapture = null) { if (windowToCapture == null) { windowToCapture = InteropWindowQuery.GetActiveWindow(); } // Show backgroundform after retrieving the active window.. var backgroundForm = new BackgroundForm(Language.GetString(LangKey.contextmenu_captureie), Language.GetString(LangKey.wait_ie_capture)); backgroundForm.Show(); //BackgroundForm backgroundForm = BackgroundForm.ShowAndWait(language.GetString(LangKey.contextmenu_captureie), language.GetString(LangKey.wait_ie_capture)); try { //Get IHTMLDocument2 for the current active window var documentContainer = CreateDocumentContainer(windowToCapture); // Nothing found if (documentContainer == null) { Log.Debug().WriteLine("Nothing to capture found"); return(null); } try { Log.Debug().WriteLine("Window class {0}", documentContainer.ContentWindow.GetClassname()); Log.Debug().WriteLine("Window location {0}", documentContainer.ContentWindow.GetInfo().Bounds.Location); } catch (Exception ex) { Log.Warn().WriteLine(ex, "Error while logging information."); } // bitmap to return Bitmap returnBitmap = null; try { var pageSize = PrepareCapture(documentContainer, capture); returnBitmap = CapturePage(documentContainer, pageSize); } catch (Exception captureException) { Log.Error().WriteLine(captureException, "Exception found, ignoring and returning nothing! Error was: "); } // TODO: Enable when the elements are usable again. // Capture the element on the page //try { // if (CoreConfig.IEFieldCapture && capture.CaptureDetails.HasDestination("Editor")) { // // clear the current elements, as they are for the window itself // capture.Elements.Clear(); // CaptureElement documentCaptureElement = documentContainer.CreateCaptureElements(pageSize); // foreach(DocumentContainer frameDocument in documentContainer.Frames) { // try { // CaptureElement frameCaptureElement = frameDocument.CreateCaptureElements(Size.Empty); // if (frameCaptureElement != null) { // documentCaptureElement.Children.Add(frameCaptureElement); // } // } catch (Exception ex) { // Log.Warn().WriteLine("An error occurred while creating the capture elements: ", ex); // } // } // capture.AddElement(documentCaptureElement); // // Offset the elements, as they are "back offseted" later... // NativePoint windowLocation = documentContainer.ContentWindow.WindowRectangle.Location; // capture.MoveElements(-(capture.ScreenBounds.Location.X-windowLocation.X), -(capture.ScreenBounds.Location.Y-windowLocation.Y)); // } //} catch (Exception elementsException) { // Log.Warn().WriteLine("An error occurred while creating the capture elements: ", elementsException); //} if (returnBitmap == null) { return(null); } // Store the bitmap for further processing capture.Bitmap = returnBitmap; try { // Store the location of the window capture.Location = documentContainer.ContentWindow.GetInfo().Bounds.Location; // The URL is available unter "document2.url" and can be used to enhance the meta-data etc. capture.CaptureDetails.AddMetaData("url", documentContainer.Url); // Store the title of the page capture.CaptureDetails.Title = documentContainer.Name ?? windowToCapture.Text; } catch (Exception ex) { Log.Warn().WriteLine(ex, "Problems getting some attributes..."); } // Store the URL of the page if (documentContainer.Url != null) { try { var uri = new Uri(documentContainer.Url); capture.CaptureDetails.AddMetaData("URL", uri.OriginalString); // As the URL can hardly be used in a filename, the following can be used if (!string.IsNullOrEmpty(uri.Scheme)) { capture.CaptureDetails.AddMetaData("URL_SCHEME", uri.Scheme); } if (!string.IsNullOrEmpty(uri.DnsSafeHost)) { capture.CaptureDetails.AddMetaData("URL_HOSTNAME", uri.DnsSafeHost); } if (!string.IsNullOrEmpty(uri.AbsolutePath)) { capture.CaptureDetails.AddMetaData("URL_PATH", uri.AbsolutePath); } if (!string.IsNullOrEmpty(uri.Query)) { capture.CaptureDetails.AddMetaData("URL_QUERY", uri.Query); } if (!string.IsNullOrEmpty(uri.UserInfo)) { capture.CaptureDetails.AddMetaData("URL_USER", uri.UserInfo); } capture.CaptureDetails.AddMetaData("URL_PORT", uri.Port.ToString()); } catch (Exception e) { Log.Warn().WriteLine(e, "Exception when trying to use url in metadata " + documentContainer.Url); } } try { // Only move the mouse to correct for the capture offset capture.MoveMouseLocation(-documentContainer.ViewportRectangle.X, -documentContainer.ViewportRectangle.Y); // Used to be: capture.MoveMouseLocation(-(capture.Location.X + documentContainer.CaptureOffset.X), -(capture.Location.Y + documentContainer.CaptureOffset.Y)); } catch (Exception ex) { Log.Warn().WriteLine(ex, "Error while correcting the mouse offset."); } } finally { // Always close the background form backgroundForm.CloseDialog(); } return(capture); }
public void TestApp_TopLevel() { var topLevelWindows = InteropWindowQuery.GetTopLevelWindows().ToList(); Assert.False(topLevelWindows.Any(window => window.IsApp())); }