Example #1
0
        // CaptureElements are regarded equal if their bounds are equal. this should be sufficient.
        public override bool Equals(object obj)
        {
            if (obj == null || GetType() != obj.GetType())
            {
                return(false);
            }
            CaptureElement other = obj as CaptureElement;

            return(other != null && Bounds.Equals(other.Bounds));
        }
        // CaptureElements are regarded equal if their bounds are equal. this should be sufficient.
        public override bool Equals(object obj)
        {
            bool ret = false;

            if (obj != null && GetType() == obj.GetType())
            {
                CaptureElement other = obj as CaptureElement;
                if (other != null && Bounds.Equals(other.Bounds))
                {
                    ret = true;
                }
            }
            return(ret);
        }
        /// <summary>
        /// Recurse into the document tree
        /// </summary>
        /// <param name="parentElement">IHTMLElement we want to recurse into</param>
        /// <returns>List of ICaptureElements with child elements</returns>
        private List<ICaptureElement> RecurseElements(IHTMLElement parentElement)
        {
            List<ICaptureElement> childElements = new List<ICaptureElement>();
            foreach(IHTMLElement element in parentElement.children) {
                string tagName = element.tagName;

                // Skip elements we aren't interested in
                if (!CAPTURE_TAGS.Contains(tagName)) {
                    continue;
                }

                ICaptureElement captureElement = new CaptureElement(tagName);
                captureElement.Children.AddRange(RecurseElements(element));

                // Get Bounds
                IHTMLElement2 element2 = element as IHTMLElement2;
                IHTMLRect htmlRect = element2.getBoundingClientRect();

                int left = htmlRect.left;
                int top = htmlRect.top;
                int right = htmlRect.right;
                int bottom = htmlRect.bottom;

                // Offset
                left += DestinationLocation.X;
                top += DestinationLocation.Y;
                right += DestinationLocation.X;
                bottom += DestinationLocation.Y;

                // Fit to floating children
                foreach(ICaptureElement childElement in captureElement.Children) {
                    //left = Math.Min(left, childElement.Bounds.Left);
                    //top = Math.Min(top, childElement.Bounds.Top);
                    right = Math.Max(right, childElement.Bounds.Right);
                    bottom = Math.Max(bottom, childElement.Bounds.Bottom);
                }
                Rectangle bounds = new Rectangle(left, top, right-left, bottom-top);

                if (bounds.Width > 0 && bounds.Height > 0) {
                    captureElement.Bounds = bounds;
                    childElements.Add(captureElement);
                }
            }
            return childElements;
        }
        /// <summary>
        /// Create a CaptureElement for every element on the page, which can be used by the editor.
        /// </summary>
        /// <returns></returns>
        public CaptureElement CreateCaptureElements(Size documentSize)
        {
            LOG.DebugFormat("CreateCaptureElements for {0}", Name);
            IHTMLElement baseElement = document3.documentElement as IHTMLElement;
            IHTMLElement2 baseElement2 = baseElement as IHTMLElement2;
            IHTMLRect htmlRect = baseElement2.getBoundingClientRect();
            if (Size.Empty.Equals(documentSize)) {
                documentSize = new Size(ScrollWidth, ScrollHeight);
            }
            Rectangle baseElementBounds = new Rectangle(DestinationLocation.X + htmlRect.left, DestinationLocation.Y + htmlRect.top, documentSize.Width, documentSize.Height);
            if (baseElementBounds.Width <= 0 || baseElementBounds.Height <= 0) {
                // not visisble
                return null;
            }

            CaptureElement captureBaseElement = new CaptureElement(name, baseElementBounds);

            foreach(IHTMLElement bodyElement in baseElement.children) {
                if ("BODY".Equals(bodyElement.tagName)) {
                    captureBaseElement.Children.AddRange(RecurseElements(bodyElement));
                }
            }
            return captureBaseElement;
        }
        /// <summary>
        /// Pre-Initialization for CaptureWithFeedback, this will get all the windows before we change anything
        /// </summary>
        private Thread PrepareForCaptureWithFeedback()
        {
            windows = new List<WindowDetails>();

            Thread getWindowDetailsThread = new Thread(delegate()
            {
                // Start Enumeration of "active" windows
                foreach (WindowDetails window in WindowDetails.GetAllWindows())
                {
                    // Window should be visible and not ourselves
                    if (!window.Visible)
                    {
                        continue;
                    }

                    // Skip empty
                    Rectangle windowRectangle = window.WindowRectangle;
                    Size windowSize = windowRectangle.Size;
                    if (windowSize.Width == 0 || windowSize.Height == 0)
                    {
                        continue;
                    }

                    // Make sure the details are retrieved once
                    window.FreezeDetails();

                    // Force children retrieval, sometimes windows close on losing focus and this is solved by caching
                    int goLevelDeep = 3;
                    if (conf.WindowCaptureAllChildLocations)
                    {
                        goLevelDeep = 20;
                    }
                    window.GetChildren(goLevelDeep);
                    lock (windows)
                    {
                        windows.Add(window);
                    }

                    // Get window rectangle as capture Element
                    CaptureElement windowCaptureElement = new CaptureElement(windowRectangle);
                    if (capture == null)
                    {
                        break;
                    }
                    capture.Elements.Add(windowCaptureElement);

                    if (!window.HasParent)
                    {
                        // Get window client rectangle as capture Element, place all the other "children" in there
                        Rectangle clientRectangle = window.ClientRectangle;
                        CaptureElement windowClientCaptureElement = new CaptureElement(clientRectangle);
                        windowCaptureElement.Children.Add(windowClientCaptureElement);
                        AddCaptureElementsForWindow(windowClientCaptureElement, window, goLevelDeep);
                    }
                    else
                    {
                        AddCaptureElementsForWindow(windowCaptureElement, window, goLevelDeep);
                    }
                }
                lock (windows)
                {
                    windows = WindowDetails.SortByZOrder(IntPtr.Zero, windows);
                }
            });
            getWindowDetailsThread.Name = "Retrieve window details";
            getWindowDetailsThread.IsBackground = true;
            getWindowDetailsThread.Start();
            return getWindowDetailsThread;
        }
 private void AddCaptureElementsForWindow(ICaptureElement parentElement, WindowDetails parentWindow, int level)
 {
     foreach (WindowDetails childWindow in parentWindow.Children)
     {
         // Make sure the details are retrieved once
         childWindow.FreezeDetails();
         Rectangle childRectangle = childWindow.WindowRectangle;
         Size s1 = childRectangle.Size;
         childRectangle.Intersect(parentElement.Bounds);
         if (childRectangle.Width > 0 && childRectangle.Height > 0)
         {
             CaptureElement childCaptureElement = new CaptureElement(childRectangle);
             parentElement.Children.Add(childCaptureElement);
             if (level > 0)
             {
                 AddCaptureElementsForWindow(childCaptureElement, childWindow, level - 1);
             }
         }
     }
 }