/// <summary> /// Move the specified window to a new location /// </summary> /// <param name="interopWindow">IInteropWindow</param> /// <param name="location">NativePoint with the offset</param> /// <returns>IInteropWindow for fluent calls</returns> public static IInteropWindow MoveTo(this IInteropWindow interopWindow, NativePoint location) { User32Api.SetWindowPos(interopWindow.Handle, IntPtr.Zero, location.X, location.Y, 0, 0, WindowPos.SWP_NOSIZE | WindowPos.SWP_SHOWWINDOW | WindowPos.SWP_NOACTIVATE | WindowPos.SWP_NOZORDER); interopWindow.Info = null; return(interopWindow); }
/// <summary> /// Set the WindowStyle /// </summary> /// <param name="interopWindow">InteropWindow</param> /// <param name="windowStyleFlags">WindowStyleFlags</param> /// <returns>IInteropWindow for fluent calls</returns> public static IInteropWindow SetStyle(this IInteropWindow interopWindow, WindowStyleFlags windowStyleFlags) { User32Api.SetWindowLongWrapper(interopWindow.Handle, WindowLongIndex.GWL_STYLE, new IntPtr((uint)windowStyleFlags)); interopWindow.Info = null; return(interopWindow); }
/// <summary> /// Set the WindowPlacement /// </summary> /// <param name="interopWindow">InteropWindow</param> /// <param name="placement">WindowPlacement</param> /// <returns>IInteropWindow for fluent calls</returns> public static IInteropWindow SetPlacement(this IInteropWindow interopWindow, WindowPlacement placement) { User32Api.SetWindowPlacement(interopWindow.Handle, ref placement); interopWindow.Placement = placement; return(interopWindow); }
/// <summary> /// Minimize the Window /// </summary> /// <param name="interopWindow">InteropWindow</param> /// <returns>IInteropWindow for fluent calls</returns> public static IInteropWindow Minimize(this IInteropWindow interopWindow) { User32Api.ShowWindow(interopWindow.Handle, ShowWindowCommands.Minimize); interopWindow.IsMinimized = true; return(interopWindow); }
/// <summary> /// Fill ALL the information of the InteropWindow /// </summary> /// <param name="interopWindow">InteropWindow</param> /// <param name="retrieveSettings">InteropWindowRetrieveSettings to specify which information is retrieved and what not</param> /// <returns>IInteropWindow for fluent calls</returns> public static IInteropWindow Fill(this IInteropWindow interopWindow, InteropWindowRetrieveSettings retrieveSettings = InteropWindowRetrieveSettings.CacheAllAutoCorrect) { if ((retrieveSettings & InteropWindowRetrieveSettings.Children) != 0 && (retrieveSettings & InteropWindowRetrieveSettings.ZOrderedChildren) != 0) { throw new ArgumentException("Can't have both Children & ZOrderedChildren", nameof(retrieveSettings)); } var forceUpdate = (retrieveSettings | InteropWindowRetrieveSettings.ForceUpdate) != 0; var autoCorrect = (retrieveSettings | InteropWindowRetrieveSettings.AutoCorrectValues) != 0; if ((retrieveSettings & InteropWindowRetrieveSettings.Info) != 0) { interopWindow.GetInfo(forceUpdate, autoCorrect); } if ((retrieveSettings & InteropWindowRetrieveSettings.Caption) != 0) { interopWindow.GetCaption(forceUpdate); } if ((retrieveSettings & InteropWindowRetrieveSettings.Classname) != 0) { interopWindow.GetClassname(forceUpdate); } if ((retrieveSettings & InteropWindowRetrieveSettings.ProcessId) != 0) { interopWindow.GetProcessId(forceUpdate); } if ((retrieveSettings & InteropWindowRetrieveSettings.Parent) != 0) { interopWindow.GetParent(forceUpdate); } if ((retrieveSettings & InteropWindowRetrieveSettings.Visible) != 0) { interopWindow.IsVisible(forceUpdate); } if ((retrieveSettings | InteropWindowRetrieveSettings.Maximized) != 0) { interopWindow.IsMaximized(forceUpdate); } if ((retrieveSettings & InteropWindowRetrieveSettings.Minimized) != 0) { interopWindow.IsMinimized(forceUpdate); } if ((retrieveSettings & InteropWindowRetrieveSettings.ScrollInfo) != 0) { interopWindow.GetWindowScroller(forceUpdate: forceUpdate); } if ((retrieveSettings & InteropWindowRetrieveSettings.Children) != 0) { interopWindow.GetChildren(forceUpdate); } if ((retrieveSettings & InteropWindowRetrieveSettings.ZOrderedChildren) != 0) { interopWindow.GetZOrderedChildren(forceUpdate); } if ((retrieveSettings & InteropWindowRetrieveSettings.Placement) != 0) { interopWindow.GetPlacement(forceUpdate); } if ((retrieveSettings & InteropWindowRetrieveSettings.Text) != 0) { interopWindow.GetText(forceUpdate); } return(interopWindow); }
/// <summary> /// Set the Extended WindowStyle /// </summary> /// <param name="interopWindow">InteropWindow</param> /// <param name="extendedWindowStyleFlags">ExtendedWindowStyleFlags</param> public static void SetExtendedStyle(this IInteropWindow interopWindow, ExtendedWindowStyleFlags extendedWindowStyleFlags) { User32Api.SetWindowLongWrapper(interopWindow.Handle, WindowLongIndex.GWL_EXSTYLE, new IntPtr((uint)extendedWindowStyleFlags)); interopWindow.Info = null; }
/// <summary> /// Test if the window is owned by the current thread /// </summary> /// <param name="interopWindow">InteropWindow</param> /// <returns>bool true if the window is owned by the current thread</returns> public static bool IsOwnedByCurrentThread(this IInteropWindow interopWindow) { // Although the process id is returned, the following also reads the Thread-ID interopWindow.GetProcessId(); return(Kernel32Api.GetCurrentThreadId() == interopWindow.ThreadId); }
/// <summary> /// Check the Classname of the IInteropWindow against a list of know classes which can be ignored. /// </summary> /// <param name="interopWindow">IInteropWindow</param> /// <returns>bool</returns> public static bool CanIgnoreClass(this IInteropWindow interopWindow) { return(IgnoreClasses.Contains(interopWindow.GetClassname())); }
/// <summary> /// Tests if the interopWindow still exists /// </summary> /// <param name="interopWindow">IInteropWindow</param> /// <returns>True if it's still there. /// Because window handles are recycled the handle could point to a different window! /// </returns> public static bool Exists(this IInteropWindow interopWindow) { return(User32Api.IsWindow(interopWindow.Handle)); }
/// <summary> /// Test if this window is for the App-Launcher /// </summary> public static bool IsAppLauncher(this IInteropWindow interopWindow) { return(ApplauncherClass.Equals(interopWindow.GetClassname())); }
/// <summary> /// Check if the window is the metro gutter (sizeable separator) /// </summary> public static bool IsGutter(this IInteropWindow interopWindow) { return(GutterClass.Equals(interopWindow.GetClassname())); }
/// <summary> /// Private helper method for the constructors /// </summary> /// <param name="document2">IHTMLDocument2</param> /// <param name="contentWindow">IInteropWindow</param> private void Init(IHTMLDocument2 document2, IInteropWindow contentWindow) { _document2 = document2; ContentWindow = contentWindow; _document3 = document2 as IHTMLDocument3; // Check what access method is needed for the document var document5 = (IHTMLDocument5)document2; //compatibility mode affects how height is computed _isDtd = false; try { if (_document3?.documentElement != null && !document5.compatMode.Equals("BackCompat")) { _isDtd = true; } } catch (Exception ex) { Log.Error().WriteLine(null, "Error checking the compatibility mode:"); Log.Error().WriteLine(ex); } // Do not release IHTMLDocument5 com object, as this also gives problems with the document2! //Marshal.ReleaseComObject(document5); NativeRect clientRectangle = contentWindow.GetInfo().Bounds; try { var window2 = document2.parentWindow; //IHTMLWindow3 window3 = (IHTMLWindow3)document2.parentWindow; var screen = window2.screen; var screen2 = (IHTMLScreen2)screen; if (Parent != null) { // Copy parent values _zoomLevelX = Parent._zoomLevelX; _zoomLevelY = Parent._zoomLevelY; ViewportRectangle = Parent.ViewportRectangle; } else { //DisableScrollbars(document2); // Calculate zoom level _zoomLevelX = screen2.deviceXDPI / (double)screen2.logicalXDPI; _zoomLevelY = screen2.deviceYDPI / (double)screen2.logicalYDPI; // Calculate the viewport rectangle, needed if there is a frame around the html window Log.Debug().WriteLine("Screen {0}x{1}", ScaleX(screen.width), ScaleY(screen.height)); //Log.Debug().WriteLine("Screen location {0},{1}", window3.screenLeft, window3.screenTop); Log.Debug().WriteLine("Window rectangle {0}", clientRectangle); Log.Debug().WriteLine("Client size {0}x{1}", ClientWidth, ClientHeight); var diffX = clientRectangle.Width - ClientWidth; var diffY = clientRectangle.Height - ClientHeight; // If there is a border around the inner window, the diff == 4 // If there is a border AND a scrollbar the diff == 20 if ((diffX == 4 || diffX >= 20) && (diffY == 4 || diffY >= 20)) { var viewportOffset = new NativePoint(2, 2); var viewportSize = new Size(ClientWidth, ClientHeight); ViewportRectangle = new NativeRect(viewportOffset, viewportSize); Log.Debug().WriteLine("viewportRect {0}", ViewportRectangle); } } Log.Debug().WriteLine("Zoomlevel {0}, {1}", _zoomLevelX, _zoomLevelY); // Release com objects ReleaseCom(window2); ReleaseCom(screen); ReleaseCom(screen2); } catch (Exception e) { Log.Warn().WriteLine(e, "Can't get certain properties for documents, using default. Due to: "); } try { Log.Debug().WriteLine("Calculated location {0} for {1}", _startLocation, document2.title); if (Name == null) { Name = document2.title; } } catch (Exception e) { Log.Warn().WriteLine(e, "Problem while trying to get document title!"); } try { Url = document2.url; } catch (Exception e) { Log.Warn().WriteLine(e, "Problem while trying to get document url!"); } SourceLocation = new NativePoint(ScaleX(_startLocation.X), ScaleY(_startLocation.Y)); _destinationLocation = new NativePoint(ScaleX(_startLocation.X), ScaleY(_startLocation.Y)); if (Parent != null) { return; } try { var frameCollection = (IHTMLFramesCollection2)document2.frames; for (var frame = 0; frame < frameCollection.length; frame++) { try { IHTMLWindow2 frameWindow = frameCollection.item(frame); var frameData = new DocumentContainer(frameWindow, contentWindow, this); // check if frame is hidden if (!frameData.IsHidden) { Log.Debug().WriteLine("Creating DocumentContainer for Frame {0} found in window with rectangle {1}", frameData.Name, frameData.SourceRectangle); Frames.Add(frameData); } else { Log.Debug().WriteLine("Skipping frame {0}", frameData.Name); } // Clean up frameWindow ReleaseCom(frameWindow); } catch (Exception e) { Log.Warn().WriteLine(e, "Problem while trying to get information from a frame, skipping the frame!"); } } // Clean up collection ReleaseCom(frameCollection); } catch (Exception ex) { Log.Warn().WriteLine(ex, "Problem while trying to get the frames, skipping!"); } try { // Correct iframe locations foreach (IHTMLElement frameElement in _document3.getElementsByTagName("IFRAME")) { try { CorrectFrameLocations(frameElement); // Clean up frameElement ReleaseCom(frameElement); } catch (Exception e) { Log.Warn().WriteLine(e, "Problem while trying to get information from an iframe, skipping the frame!"); } } } catch (Exception ex) { Log.Warn().WriteLine(ex, "Problem while trying to get the iframes, skipping!"); } }
public DocumentContainer(IHTMLDocument2 document2, IInteropWindow contentWindow) { Init(document2, contentWindow); Log.Debug().WriteLine("Creating DocumentContainer for Document {0} found in window with rectangle {1}", Name, SourceRectangle); }
/// <summary> /// Fill ALL the information of the InteropWindow /// </summary> /// <param name="interopWindow">InteropWindow</param> /// <param name="cacheFlags">InteropWindowCacheFlags to specify which information is retrieved and what not</param> public static IInteropWindow Fill(this IInteropWindow interopWindow, InteropWindowCacheFlags cacheFlags = InteropWindowCacheFlags.CacheAll) { if (cacheFlags.HasFlag(InteropWindowCacheFlags.Children) && cacheFlags.HasFlag(InteropWindowCacheFlags.ZOrderedChildren)) { throw new ArgumentException("Can't have both Children & ZOrderedChildren", nameof(cacheFlags)); } var forceUpdate = cacheFlags.HasFlag(InteropWindowCacheFlags.ForceUpdate); if (cacheFlags.HasFlag(InteropWindowCacheFlags.Info)) { interopWindow.GetInfo(forceUpdate); } if (cacheFlags.HasFlag(InteropWindowCacheFlags.Caption)) { interopWindow.GetCaption(forceUpdate); } if (cacheFlags.HasFlag(InteropWindowCacheFlags.Classname)) { interopWindow.GetClassname(forceUpdate); } if (cacheFlags.HasFlag(InteropWindowCacheFlags.ProcessId)) { interopWindow.GetProcessId(forceUpdate); } if (cacheFlags.HasFlag(InteropWindowCacheFlags.Parent)) { interopWindow.GetParent(forceUpdate); } if (cacheFlags.HasFlag(InteropWindowCacheFlags.Visible)) { interopWindow.IsVisible(forceUpdate); } if (cacheFlags.HasFlag(InteropWindowCacheFlags.Maximized)) { interopWindow.IsMaximized(forceUpdate); } if (cacheFlags.HasFlag(InteropWindowCacheFlags.Minimized)) { interopWindow.IsMinimized(forceUpdate); } if (cacheFlags.HasFlag(InteropWindowCacheFlags.ScrollInfo)) { interopWindow.GetWindowScroller(forceUpdate: forceUpdate); } if (cacheFlags.HasFlag(InteropWindowCacheFlags.Children)) { interopWindow.GetChildren(forceUpdate); } if (cacheFlags.HasFlag(InteropWindowCacheFlags.ZOrderedChildren)) { interopWindow.GetZOrderedChildren(forceUpdate); } if (cacheFlags.HasFlag(InteropWindowCacheFlags.Placement)) { interopWindow.GetPlacement(forceUpdate); } if (cacheFlags.HasFlag(InteropWindowCacheFlags.Text)) { interopWindow.GetText(forceUpdate); } return(interopWindow); }
/// <summary> /// Get all the other windows belonging to the process which owns the specified window /// </summary> /// <param name="windowToLinkTo">IInteropWindow</param> /// <returns>IEnumerable of IInteropWindow</returns> public static IEnumerable <IInteropWindow> GetLinkedWindows(this IInteropWindow windowToLinkTo) { int processIdSelectedWindow = windowToLinkTo.GetProcessId(); return(InteropWindowQuery.GetTopLevelWindows().Where(window => window.Handle != windowToLinkTo.Handle && window.GetProcessId() == processIdSelectedWindow)); }
/// <summary> /// Test if the window is owned by the current process /// </summary> /// <param name="interopWindow">InteropWindow</param> /// <returns>bool true if the window is owned by the current process</returns> public static bool IsOwnedByCurrentProcess(this IInteropWindow interopWindow) { return(Kernel32Api.GetCurrentProcessId() == interopWindow.GetProcessId()); }
/// <summary> /// Returns if the IInteropWindow is docked to the left of the other IInteropWindow /// </summary> /// <param name="window1">IInteropWindow</param> /// <param name="window2">IInteropWindow</param> /// <param name="retrieveBoundsFunc">Function which returns the bounds for the IInteropWindow</param> /// <returns>bool true if docked</returns> public static bool IsDockedToLeftOf(this IInteropWindow window1, IInteropWindow window2, Func <IInteropWindow, NativeRect> retrieveBoundsFunc = null) { retrieveBoundsFunc = retrieveBoundsFunc ?? (window => window.GetInfo().Bounds); return(retrieveBoundsFunc(window1).IsDockedToLeftOf(retrieveBoundsFunc(window2))); }
/// <summary> /// Restore (Un-Minimize/Maximize) the Window /// </summary> /// <param name="interopWindow">InteropWindow</param> public static void Restore(this IInteropWindow interopWindow) { User32Api.ShowWindow(interopWindow.Handle, ShowWindowCommands.Restore); interopWindow.IsMinimized = false; interopWindow.IsMaximized = false; }