private DocumentContainer(IHTMLWindow2 frameWindow, WindowDetails contentWindow, DocumentContainer parent) { //IWebBrowser2 webBrowser2 = frame as IWebBrowser2; //IHTMLDocument2 document2 = webBrowser2.Document as IHTMLDocument2; IHTMLDocument2 document2 = GetDocumentFromWindow(frameWindow); try { LOG.DebugFormat("frameWindow.name {0}", frameWindow.name); name = frameWindow.name; } catch { } try { LOG.DebugFormat("document2.url {0}", document2.url); } catch { } try { LOG.DebugFormat("document2.title {0}", document2.title); } catch { } this.parent = parent; // Calculate startLocation for the frames IHTMLWindow2 window2 = document2.parentWindow; IHTMLWindow3 window3 = (IHTMLWindow3)window2; Point contentWindowLocation = contentWindow.WindowRectangle.Location; int x = window3.screenLeft - contentWindowLocation.X; int y = window3.screenTop - contentWindowLocation.Y; // Release IHTMLWindow 2+3 com objects releaseCom(window2); releaseCom(window3); startLocation = new Point(x, y); Init(document2, contentWindow); }
/// <summary> /// Load confort+ toolbar /// </summary> /// <remarks> /// Execute confort+ javascript and register to onmessage event /// </remarks> private void LoadToolbar() { IHTMLDocument2 document = this.webBrowser.Document as IHTMLDocument2; IHTMLWindow2 window = document.parentWindow; this.LogInfo("5. load the toolbar"); StringBuilder sb = new StringBuilder(); sb.Append(Tools.GetStringFromRessource("js/hebergement.js")); sb.Append(Tools.GetStringFromRessource("js/ToolbarStrings.js")); sb.Append(Tools.GetStringFromRessource("js/UciUserPref.js")); sb.Append(Tools.GetStringFromRessource("js/UciSimpleStorage.js")); sb.Append(Tools.GetStringFromRessource("language/en.js")); sb.Append(Tools.GetStringFromRessource("language/es.js")); sb.Append(Tools.GetStringFromRessource("language/fr.js")); sb.Append(Tools.GetStringFromRessource("language/pl.js")); sb.Append(Tools.GetStringFromRessource("js/UciAideMotrice.js")); sb.Append(Tools.GetStringFromRessource("js/UciCouleur.js")); sb.Append(Tools.GetStringFromRessource("js/UciApparence.js")); sb.Append(Tools.GetStringFromRessource("js/UciSettings.js")); sb.Append(Tools.GetStringFromRessource("js/UciTypographie.js")); sb.Append(Tools.GetStringFromRessource("js/UciHelp.js")); sb.Append(Tools.GetStringFromRessource("js/UciValidation.js")); sb.Append(Tools.GetStringFromRessource("js/UciProfile.js")); sb.Append(Tools.GetStringFromRessource("js/UciIhm.js")); sb.Append(Tools.GetStringFromRessource("js/mask.js")); sb.Append(Tools.GetStringFromRessource("js/toolbar.js")); window.execScript(sb.ToString()); IHTMLWindow3 window3 = document.parentWindow as IHTMLWindow3; if (window3 != null) { // front events subscription bool res = window3.attachEvent("onmessage", new ObjectEventHandler(this.OnMessage)); } else { this.LogInfo("window3 is null"); } }
// 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)); }
private DocumentContainer(IHTMLWindow2 frameWindow, WindowDetails contentWindow, DocumentContainer parent) { //IWebBrowser2 webBrowser2 = frame as IWebBrowser2; //IHTMLDocument2 document2 = webBrowser2.Document as IHTMLDocument2; IHTMLDocument2 document2 = GetDocumentFromWindow(frameWindow); try { LOG.DebugFormat("frameWindow.name {0}", frameWindow.name); name = frameWindow.name; } catch { } try { LOG.DebugFormat("document2.url {0}", document2.url); } catch { } try { LOG.DebugFormat("document2.title {0}", document2.title); } catch { } this.parent = parent; // Calculate startLocation for the frames IHTMLWindow3 window3 = (IHTMLWindow3)document2.parentWindow; // IHTMLElement element = window2.document.body; // long x = 0; // long y = 0; // do { // x += element.offsetLeft; // y += element.offsetTop; // element = element.offsetParent; // } while (element != null); // startLocation = new Point((int)x, (int)y); Point contentWindowLocation = contentWindow.WindowRectangle.Location; int x = window3.screenLeft - contentWindowLocation.X; int y = window3.screenTop - contentWindowLocation.Y; startLocation = new Point(x, y); Init(document2, contentWindow); }
/// <summary> /// Private helper method for the constructors /// </summary> /// <param name="document2">IHTMLDocument2</param> /// <param name="contentWindow">WindowDetails</param> private void Init(IHTMLDocument2 document2, WindowDetails contentWindow) { this.document2 = document2; this.contentWindow = contentWindow; this.document3 = document2 as IHTMLDocument3; // Check what access method is needed for the document IHTMLDocument5 document5 = (IHTMLDocument5)document2; //compatibility mode affects how height is computed if ((document3.documentElement != null) && (!document5.compatMode.Equals("BackCompat"))) { isDTD = true; } else { isDTD = false; } Rectangle clientRectangle = contentWindow.WindowRectangle; try { IHTMLWindow3 window3 = (IHTMLWindow3)document2.parentWindow; IHTMLWindow2 window2 = (IHTMLWindow2)document2.parentWindow; IHTMLScreen2 screen2 = (IHTMLScreen2)window2.screen; IHTMLScreen screen = window2.screen; if (parent != null) { // Copy parent values zoomLevelX = parent.zoomLevelX; zoomLevelY = parent.zoomLevelY; viewportRectangle = parent.viewportRectangle; } else { //DisableScrollbars(document2); // Calculate zoom level zoomLevelX = (double)screen2.deviceXDPI / (double)screen2.logicalXDPI; zoomLevelY = (double)screen2.deviceYDPI / (double)screen2.logicalYDPI; // Calculate the viewport rectangle, needed if there is a frame around the html window LOG.DebugFormat("Screen {0}x{1}", ScaleX(screen.width), ScaleY(screen.height)); LOG.DebugFormat("Screen location {0},{1}", window3.screenLeft, window3.screenTop); LOG.DebugFormat("Window rectangle {0}", clientRectangle); LOG.DebugFormat("Client size {0}x{1}", ClientWidth, ClientHeight); int diffX = clientRectangle.Width - ClientWidth; int 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)) { Point viewportOffset = new Point(2, 2); Size viewportSize = new Size(ClientWidth, ClientHeight); viewportRectangle = new Rectangle(viewportOffset, viewportSize); LOG.DebugFormat("viewportRect {0}", viewportRectangle); } } LOG.DebugFormat("Zoomlevel {0}, {1}", zoomLevelX, zoomLevelY); } catch (Exception e) { LOG.Warn("Can't get certain properties for documents, using default. due to: ", e); } LOG.DebugFormat("Calculated location {0} for {1}", startLocation, document2.title); sourceLocation = new Point(ScaleX((int)startLocation.X), ScaleY((int)startLocation.Y)); destinationLocation = new Point(ScaleX((int)startLocation.X), ScaleY((int)startLocation.Y)); try { if (name == null) { name = document2.title; } } catch { } try { url = document2.url; } catch { } if (parent != null) { return; } IHTMLFramesCollection2 frameCollection = (IHTMLFramesCollection2)document2.frames; for (int frame = 0; frame < frameCollection.length; frame++) { IHTMLWindow2 frameWindow = frameCollection.item(frame); try { DocumentContainer frameData = new DocumentContainer(frameWindow, contentWindow, this); // check if frame is hidden if (!frameData.isHidden) { LOG.DebugFormat("Creating DocumentContainer for Frame {0} found in window with rectangle {1}", frameData.name, frameData.SourceRectangle); frames.Add(frameData); } else { LOG.DebugFormat("Skipping frame {0}", frameData.Name); } } catch (Exception e) { LOG.Warn("Problem while trying to get information from a frame, skipping the frame!", e); } } // Correct iframe locations foreach (IHTMLElement frameElement in document3.getElementsByTagName("IFRAME")) { try { CorrectFrameLocations(frameElement); } catch (Exception e) { LOG.Warn("Problem while trying to get information from an iframe, skipping the frame!", e); } } }