/// <summary> /// Overridden to set the design mode to the last desired design mode and multiple selection flag /// to the last desired multiple selection flag /// </summary> /// <param name="args"></param> protected override internal void OnReadyStateComplete(EventArgs args) { base.OnReadyStateComplete(args); _persistStream = (Interop.IPersistStreamInit)MSHTMLDocument; Selection.SynchronizeSelection(); //Set the mutiple selection mode to the last desired multiple selection mode if (_multipleSelectionDesired) { MultipleSelectionEnabled = _multipleSelectionDesired; } //Set the absolute positioning mode to the last desired absolute position mode if (_absolutePositioningDesired) { AbsolutePositioningEnabled = _absolutePositioningDesired; } //Set the absolute positioning mode to the last desired absolute position mode if (_bordersDesired) { BordersVisible = _bordersDesired; } }
//REVIEW: Add a load method for stream and url /// <summary> /// Loads HTML content from a string into this control identified by the specified URL. /// If MSHTML has not yet been created, the loading is postponed until MSHTML has been created. /// </summary> /// <param name="content"></param> /// <param name="url"></param> public void LoadHtml(string content, string url, string style) { if (content == null) { content = ""; } if (!_isCreated) { _desiredContent = content; _desiredUrl = url; _loadDesired = true; return; } if (_fullDocumentMode == false) { content = CreateHtmlContent(content, style); } OnBeforeLoad(); Interop.IStream stream = null; //First we create a COM stream IntPtr hglobal = Marshal.StringToHGlobalUni(content); Interop.CreateStreamOnHGlobal(hglobal, true, out stream); // Initialize a new document if there is nothing to load if (stream == null) { Interop.IPersistStreamInit psi = (Interop.IPersistStreamInit)_site.MSHTMLDocument; Debug.Assert(psi != null, "Expected IPersistStreamInit"); psi.InitNew(); psi = null; } else { Interop.IHTMLDocument2 document = _site.MSHTMLDocument; //If there is no specified URL load the document from the stream if (url == null) { Interop.IPersistStreamInit psi = (Interop.IPersistStreamInit)document; Debug.Assert(psi != null, "Expected IPersistStreamInit"); psi.Load(stream); psi = null; } else { //Otherwise we create a moniker and load the stream to that moniker } } _url = url; OnAfterLoad(); }
protected internal override void OnReadyStateComplete(EventArgs args) { base.OnReadyStateComplete(args); this._persistStream = (Interop.IPersistStreamInit) base.MSHTMLDocument; this._selection.SynchronizeSelection(); if (this._multipleSelectionDesired) { this.MultipleSelectionEnabled = this._multipleSelectionDesired; } if (this._absolutePositioningDesired) { this.AbsolutePositioningEnabled = this._absolutePositioningDesired; } if (this._gridVisibleDesired) { this.GridVisible = this._gridVisibleDesired; } if (this._glyphsDesired) { this.GlyphsVisible = this._glyphsDesired; } if (this._bordersDesired) { this.BordersVisible = this._bordersDesired; } }
/// <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()); }