private void CorrectFrameLocations(IHTMLElement frameElement) { long x = 0; long y = 0; IHTMLElement element = frameElement; do { x += element.offsetLeft; y += element.offsetTop; element = element.offsetParent; } while (element != null); Point elementLocation = new Point((int)x, (int)y); IHTMLElement2 element2 = (IHTMLElement2)frameElement; IHTMLRect rec = element2.getBoundingClientRect(); Point elementBoundingLocation = new Point(rec.left, rec.top); LOG.DebugFormat("Looking for iframe to correct at {0}", elementBoundingLocation); foreach (DocumentContainer foundFrame in frames) { Point frameLocation = foundFrame.SourceLocation; if (frameLocation.Equals(elementBoundingLocation)) { // Match found, correcting location LOG.DebugFormat("Correcting frame from {0} to {1}", frameLocation, elementLocation); foundFrame.SourceLocation = elementLocation; foundFrame.DestinationLocation = elementLocation; } else { LOG.DebugFormat("{0} != {1}", frameLocation, elementBoundingLocation); } } }
/// <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> /// 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); }
// System drawing point leveraged to get coordinates // using System.Drawing and mshtml /// <summary> /// Method to get Grid Coordinates of an element /// </summary> /// <param name="element"></param> /// <returns></returns> public System.Drawing.Point GetScreenPoint(IEElement element) { IHTMLElement nativeElement = element.AsHtmlElement; IHTMLElement2 offsetElement = (IHTMLElement2)nativeElement; IHTMLRect clientRect = offsetElement.getBoundingClientRect(); IHTMLDocument2 doc = (IHTMLDocument2)nativeElement.document; IHTMLWindow3 window = (IHTMLWindow3)doc.parentWindow; int windowLeft = window.screenLeft; int windowTop = window.screenTop; int elementLeft = clientRect.left; int elementTop = clientRect.top; int width = nativeElement.offsetWidth; int height = nativeElement.offsetHeight; int clickX = windowLeft + elementLeft + (width / 2); int clickY = windowTop + elementTop + (height / 2); return(new System.Drawing.Point(clickX, clickY)); }
/// <summary> /// 得到元素的位置 /// </summary> /// <param name="elem">元素</param> /// <returns></returns> public static Rectangle GetElementRect(IHTMLElement body, IHTMLElement elem) { int x, y, w, h; x = y = w = h = 0; // 计算元素本身的位置 IHTMLElement2 elem2 = elem as IHTMLElement2; IHTMLRect elemRect = elem2.getBoundingClientRect(); x = elemRect.left; y = elemRect.top; w = elemRect.right - elemRect.left; h = elemRect.bottom - elemRect.top; // TODO: 计算顶端htmlElem(docElem)的位置,一般不用计算,其位置应该为(0,0,xx,xx) // 计算父亲iframes if (body.document != elem.document) { List <IHTMLDOMNode> frames = new List <IHTMLDOMNode>(); _getEleParentFrames(body as IHTMLDOMNode, elem as IHTMLDOMNode, frames); foreach (IHTMLDOMNode f in frames) { IHTMLElement2 frame2 = f as IHTMLElement2; IHTMLRect frameRect = frame2.getBoundingClientRect(); x += frameRect.left; y += frameRect.top; } } Rectangle ret = new Rectangle(); ret.X = x; ret.Y = y; ret.Width = w; ret.Height = h; return(ret); }
public static Rectangle GetElementRect(IHTMLElement body, IHTMLElement elem) { int top; int num3; int num4; int left = top = num3 = num4 = 0; IHTMLElement2 element = elem as IHTMLElement2; IHTMLRect o = element.getBoundingClientRect(); elem = element as IHTMLElement; left = o.left; top = o.top; num3 = o.right - o.left; num4 = o.bottom - o.top; if (body.document != elem.document) { List <IHTMLDOMNode> frames = new List <IHTMLDOMNode>(); GetEleParentFrames(body as IHTMLDOMNode, elem as IHTMLDOMNode, frames); foreach (IHTMLDOMNode node in frames) { IHTMLRect rect2 = (node as IHTMLElement2).getBoundingClientRect(); left += rect2.left; top += rect2.top; } } Rectangle rectangle = new Rectangle { X = left, Y = top, Width = num3, Height = num4 }; if (o != null) { Marshal.ReleaseComObject(o); } return(rectangle); }
protected void ClickAreaElement(IHTMLElement2 img, IHTMLElement2 area) { IHTMLRect rectImg = img.getBoundingClientRect(); IHTMLRect rectArea = area.getBoundingClientRect(); MoveToPoint( (rectArea.left + rectArea.right)/2 + img.clientLeft + rectImg.left, (rectArea.top + rectArea.bottom)/2 + img.clientTop + rectImg.top ); WaitMin(); UnsafeNativeMethods.ClickMouse(MouseButtons.Left); Application.DoEvents(); WaitMin(); }
protected void MoveMouseToElement(IHTMLElement2 e2) { IHTMLRect rect = e2.getBoundingClientRect(); MoveToPoint( (rect.left + rect.right)/2, (rect.top + rect.bottom)/2 ); WaitMin(); }