internal FrameWindow(Interop.IHTMLWindow2 window, MSHTMLSite relatedSite, HtmlEditor htmlEditor) { this._relatedSite = relatedSite; this.window = window; this.doc = (Interop.IHTMLDocument2)window.document; this.url = ((Interop.IHTMLLocation)((Interop.IHTMLWindow2)doc.GetParentWindow()).location).href; this.body = (Interop.IHTMLElement3)doc.GetBody(); this.framebase = ((Interop.IHTMLWindow4)window).frameElement; this.htmlFormatter = new HtmlFormatter(); this.CommandTarget = (Interop.IOleCommandTarget)doc; this.winEvents = new FrameEvents(window, htmlEditor); this.winEvents.Activate += new EventHandler(winEvents_Activate); this.winEvents.DeActivate += new EventHandler(winEvents_DeActivate); this.nativeElement = (FrameElement)htmlEditor.GenericElementFactory.CreateElement(framebase as Interop.IHTMLElement); }
/// <summary> /// Saves the HTML contained in control to a string and return it. /// </summary> /// <returns>string - The HTML in the control</returns> public string SaveHtml() { if (!IsCreated) { throw new Exception("HtmlControl.SaveHtml : No HTML to save!"); } string content = String.Empty; try { OnBeforeSave(); Interop.IHTMLDocument2 document = _site.MSHTMLDocument; if (_fullDocumentMode) { // First save the document to a stream Interop.IPersistStreamInit psi = (Interop.IPersistStreamInit)document; Debug.Assert(psi != null, "Expected IPersistStreamInit"); Interop.IStream stream = null; Interop.CreateStreamOnHGlobal(Interop.NullIntPtr, true, out stream); psi.Save(stream, 1); // Now copy the stream to the string STATSTG stat = new STATSTG(); stream.Stat(stat, 1); int length = (int)stat.cbSize; byte[] bytes = new byte[length]; IntPtr hglobal; Interop.GetHGlobalFromStream(stream, out hglobal); Debug.Assert(hglobal != Interop.NullIntPtr, "Failed in GetHGlobalFromStream"); // First copy the stream to a byte array IntPtr pointer = Interop.GlobalLock(hglobal); if (pointer != Interop.NullIntPtr) { Marshal.Copy(pointer, bytes, 0, length); Interop.GlobalUnlock(hglobal); // Then create the string from the byte array (use a StreamReader to eat the preamble in the UTF8 encoding case) StreamReader streamReader = null; try { streamReader = new StreamReader(new MemoryStream(bytes), Encoding.Default); content = streamReader.ReadToEnd(); } finally { if (streamReader != null) { streamReader.Close(); } } } } else { // Save only the contents of the <body> tag Interop.IHTMLElement bodyElement = document.GetBody(); Debug.Assert(bodyElement != null, "Could not get BODY element from document"); if (bodyElement != null) { content = SavePartialHtml(Element.GetWrapperFor(bodyElement, this)); } } } catch (Exception e) { Debug.Fail("HtmlControl.SaveHtml" + e.ToString()); content = String.Empty; } finally { OnAfterSave(); } if (content == null) { content = String.Empty; } HtmlFormatter formatter = new HtmlFormatter(); StringWriter writer = new StringWriter(); formatter.Format(content, writer, new HtmlFormatterOptions(' ', 4, 80, true)); return(writer.ToString()); }
public Interop.IHTMLRenderStyle GetRenderStyle(Interop.IHTMLDocument2 ActiveDocument) { Interop.IHTMLRenderStyle renderStyle = ((Interop.IHTMLDocument4)ActiveDocument).createRenderStyle(null); renderStyle.defaultTextSelection = IsDefault ? "true" : "false"; renderStyle.renderingPriority = (int)this.priority; switch (lineType) { default: case LineType.None: renderStyle.textDecoration = "none"; renderStyle.textBackgroundColor = textBackgroundColor.ColorName; if (textColor.Type == ColorType.Auto || textColor.Type == ColorType.Inherit) { renderStyle.textColor = ((Interop.IHTMLCurrentStyle)((Interop.IHTMLElement2)ActiveDocument.GetBody()).GetCurrentStyle()).getAttribute("color", 1); } else { renderStyle.textColor = textColor.ColorName; } break; case LineType.Overline: renderStyle.textDecoration = "overline"; renderStyle.textDecorationColor = lineColor.ColorName; renderStyle.textBackgroundColor = textBackgroundColor.ColorName; if (textColor.Type == ColorType.Auto || textColor.Type == ColorType.Inherit) { renderStyle.textColor = ((Interop.IHTMLCurrentStyle)((Interop.IHTMLElement2)ActiveDocument.GetBody()).GetCurrentStyle()).getAttribute("color", 1); } else { renderStyle.textColor = textColor.ColorName; } break; case LineType.Underline: renderStyle.textDecoration = "underline"; renderStyle.textDecorationColor = lineColor.ColorName; renderStyle.textBackgroundColor = textBackgroundColor.ColorName; if (textColor.Type == ColorType.Auto || textColor.Type == ColorType.Inherit) { renderStyle.textColor = ((Interop.IHTMLCurrentStyle)((Interop.IHTMLElement2)ActiveDocument.GetBody()).GetCurrentStyle()).getAttribute("color", 1); } else { renderStyle.textColor = textColor.ColorName; } break; case LineType.LineThrough: renderStyle.textDecoration = "line-through"; renderStyle.textDecorationColor = lineColor.ColorName; renderStyle.textBackgroundColor = textBackgroundColor.ColorName; if (textColor.Type == ColorType.Auto || textColor.Type == ColorType.Inherit) { renderStyle.textColor = ((Interop.IHTMLCurrentStyle)((Interop.IHTMLElement2)ActiveDocument.GetBody()).GetCurrentStyle()).getAttribute("color", 1); } else { renderStyle.textColor = textColor.ColorName; } break; } string u; switch (underlineStyle) { case UnderlineStyle.Single: u = "single"; break; case UnderlineStyle.Double: u = "double"; break; case UnderlineStyle.Words: u = "words"; break; case UnderlineStyle.Dotted: u = "dotted"; break; case UnderlineStyle.Thick: u = "thick"; break; case UnderlineStyle.Dash: u = "dash"; break; case UnderlineStyle.DotDash: u = "dot-dash"; break; case UnderlineStyle.DotDotDash: u = "dot-dot-dash"; break; case UnderlineStyle.Wave: u = "wave"; break; case UnderlineStyle.SingleAccounting: u = "single-accounting"; break; case UnderlineStyle.DoubleAccounting: u = "double-accounting"; break; case UnderlineStyle.ThickDash: u = "thick-dash"; break; default: case UnderlineStyle.Undefined: u = "undefined"; break; } renderStyle.textUnderlineStyle = u; string l; switch (lineThroughStyle) { default: case LineThroughStyle.Undefined: l = "undefined"; break; case LineThroughStyle.Single: l = "single"; break; case LineThroughStyle.Double: l = "double"; break; } renderStyle.textLineThroughStyle = l; return(renderStyle); }