/////////////////////////////////////////////////////////////////////////// // Implementation /// <include file='doc\MSHTMLHost.uex' path='docs/doc[@for="TridentSite.CreateDocument"]/*' /> /// <devdoc> /// Creates a new instance of mshtml and initializes it as a new document /// using its IPersistStreamInit. /// </devdoc> protected void CreateDocument() { try { // Create an instance of Trident tridentDocument = (NativeMethods.IHTMLDocument2) new NativeMethods.HTMLDocument(); tridentOleObject = (NativeMethods.IOleObject)tridentDocument; // Initialize its client site tridentOleObject.SetClientSite((NativeMethods.IOleClientSite) this); // Initialize it NativeMethods.IPersistStreamInit psi = (NativeMethods.IPersistStreamInit)tridentDocument; psi.InitNew(); } catch (Exception e) { Debug.Fail(e.ToString()); throw e; } }
/// <summary>Called when the control has just become ready.</summary> /// <param name="e"></param> protected internal virtual void OnReadyStateComplete(EventArgs e) { this.isReady = true; EventHandler handler = (EventHandler)this.Events[readyStateCompleteEvent]; if (handler != null) { handler(this, e); } if (this.focusDesired) { this.focusDesired = false; this.site.ActivateHtml(); this.site.SetFocus(); } // HtmlEditor this.persistStream = (NativeMethods.IPersistStreamInit) this.HtmlDocument; }
/// <include file='doc\MSHTMLHost.uex' path='docs/doc[@for="TridentSite.CreateDocument"]/*' /> /// <devdoc> /// Creates a new instance of mshtml and initializes it as a new document /// using its IPersistStreamInit. /// </devdoc> protected void CreateDocument() { Debug.WriteLineIf(StyleBuilder.StyleBuilderSwitch.TraceVerbose, "In TridentSite::CreateTrident"); try { // Create an instance of Trident tridentDocument = (Microsoft.VisualStudio.Interop.Trident.IHTMLDocument2) new HTMLDocument(); tridentOleObject = (NativeMethods.IOleObject)tridentDocument; // Initialize its client site Debug.WriteLineIf(StyleBuilder.StyleBuilderSwitch.TraceVerbose, "Setting Trident's IOleClientSite"); tridentOleObject.SetClientSite((NativeMethods.IOleClientSite) this); // Initialize it Debug.WriteLineIf(StyleBuilder.StyleBuilderSwitch.TraceVerbose, "Initializing Trident through IPersistStreamInit"); NativeMethods.IPersistStreamInit psi = (NativeMethods.IPersistStreamInit)tridentDocument; psi.InitNew(); } catch (Exception e) { Debug.Fail(e.ToString()); throw e; } }
/// <summary>Called when the control has just become ready.</summary> /// <param name="e"></param> protected internal virtual void OnReadyStateComplete(EventArgs e) { this.isReady = true; EventHandler handler = (EventHandler)this.Events[readyStateCompleteEvent]; if (handler != null) { handler(this, e); } if (this.focusDesired) { this.focusDesired = false; this.site.ActivateHtml(); this.site.SetFocus(); } // HtmlEditor this.persistStream = (NativeMethods.IPersistStreamInit) this.HtmlDocument; this.Selection.SynchronizeSelection(); // Set the mutiple selection mode to the last desired multiple selection mode if (this.multipleSelectionDesired) { this.MultipleSelectionEnabled = this.multipleSelectionDesired; } // Set the absolute positioning mode to the last desired absolute position mode if (this.absolutePositioningDesired) { this.AbsolutePositioningEnabled = this.absolutePositioningDesired; } }
/// <summary>Called when the control has just become ready.</summary> /// <param name="e"></param> protected internal virtual void OnReadyStateComplete(EventArgs e) { this.isReady = true; EventHandler handler = (EventHandler)this.Events[readyStateCompleteEvent]; if (handler != null) { handler(this, e); } if (this.focusDesired) { this.focusDesired = false; this.site.ActivateHtml(); this.site.SetFocus(); } // HtmlEditor this.persistStream = (NativeMethods.IPersistStreamInit)this.HtmlDocument; this.Selection.SynchronizeSelection(); // Set the mutiple selection mode to the last desired multiple selection mode if (this.multipleSelectionDesired) { this.MultipleSelectionEnabled = this.multipleSelectionDesired; } // Set the absolute positioning mode to the last desired absolute position mode if (this.absolutePositioningDesired) { this.AbsolutePositioningEnabled = this.absolutePositioningDesired; } }
/// <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 (!this.IsCreated) { throw new Exception("HtmlControl.SaveHtml : No HTML to save!"); } string content = String.Empty; try { NativeMethods.IHTMLDocument2 document = this.site.Document; // First save the document to a stream NativeMethods.IPersistStreamInit psi = (NativeMethods.IPersistStreamInit)document; Debug.Assert(psi != null, "Expected IPersistStreamInit"); NativeMethods.IStream stream = null; NativeMethods.CreateStreamOnHGlobal(NativeMethods.NullIntPtr, true, out stream); psi.Save(stream, 1); // Now copy the stream to the string NativeMethods.STATSTG stat = new NativeMethods.STATSTG(); stream.Stat(stat, 1); int length = (int)stat.cbSize; byte[] bytes = new byte[length]; IntPtr hglobal; NativeMethods.GetHGlobalFromStream(stream, out hglobal); Debug.Assert(hglobal != NativeMethods.NullIntPtr, "Failed in GetHGlobalFromStream"); // First copy the stream to a byte array IntPtr pointer = NativeMethods.GlobalLock(hglobal); if (pointer != NativeMethods.NullIntPtr) { Marshal.Copy(pointer, bytes, 0, length); NativeMethods.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(); } } } } catch (Exception exception) { Debug.Fail("HtmlControl.SaveHtml" + exception.ToString()); content = String.Empty; } finally { } if (content == null) { content = String.Empty; } return(content); /* * HtmlFormatter formatter = new HtmlFormatter(); * StringWriter writer = new StringWriter(); * formatter.Format(content, writer); * return writer.ToString(); */ }
/* * public void LoadHtml(string content, string url) * { * this.LoadHtml(content, url, null); * } */ // 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) { if (content == null) { content = ""; } if (!this.isCreated) { this.desiredContent = content; this.desiredUrl = url; this.desiredLoad = true; return; } NativeMethods.IStream stream = null; //First we create a COM stream IntPtr hglobal = Marshal.StringToHGlobalUni(content); NativeMethods.CreateStreamOnHGlobal(hglobal, true, out stream); // Initialize a new document if there is nothing to load if (stream == null) { NativeMethods.IPersistStreamInit psi = (NativeMethods.IPersistStreamInit) this.site.Document; Debug.Assert(psi != null, "Expected IPersistStreamInit"); psi.InitNew(); psi = null; } else { NativeMethods.IHTMLDocument2 document = this.site.Document; if (url == null) { // If there is no specified URL load the document from the stream. NativeMethods.IPersistStreamInit psi = (NativeMethods.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. NativeMethods.IPersistMoniker persistMoniker = (NativeMethods.IPersistMoniker)document; NativeMethods.IMoniker moniker = null; NativeMethods.CreateURLMoniker(null, url, out moniker); NativeMethods.IBindCtx bindContext = null; NativeMethods.CreateBindCtx(0, out bindContext); persistMoniker.Load(1, moniker, bindContext, 0); persistMoniker = null; moniker = null; bindContext = null; } } this.url = url; }
/* * public void LoadHtml(string content, string url) * { * this.LoadHtml(content, url, null); * } */ // 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) { if (content == null) { content = ""; } if (!this.isCreated) { this.desiredContent = content; this.desiredUrl = url; this.desiredLoad = true; return; } NativeMethods.IStream stream = null; // added by Erik Frey - UTF preamble byte[] preamble = UnicodeEncoding.Unicode.GetPreamble(); string byteOrderMark = UnicodeEncoding.Unicode.GetString(preamble, 0, preamble.Length); // i guess .NET 2.0 fixed up unicode string handling if (System.Environment.Version.Major > 1 || !content.StartsWith(byteOrderMark)) { content = byteOrderMark + content; } //First we create a COM stream IntPtr hglobal = Marshal.StringToHGlobalUni(content); NativeMethods.CreateStreamOnHGlobal(hglobal, true, out stream); // Initialize a new document if there is nothing to load if (stream == null) { NativeMethods.IPersistStreamInit psi = (NativeMethods.IPersistStreamInit) this.site.Document; Debug.Assert(psi != null, "Expected IPersistStreamInit"); psi.InitNew(); psi = null; } else { NativeMethods.IHTMLDocument2 document = this.site.Document; if (url == null) { // If there is no specified URL load the document from the stream. NativeMethods.IPersistStreamInit psi = (NativeMethods.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. NativeMethods.IPersistMoniker persistMoniker = (NativeMethods.IPersistMoniker)document; NativeMethods.IMoniker moniker = null; NativeMethods.CreateURLMoniker(null, url, out moniker); NativeMethods.IBindCtx bindContext = null; NativeMethods.CreateBindCtx(0, out bindContext); persistMoniker.Load(1, moniker, bindContext, 0); persistMoniker = null; moniker = null; bindContext = null; } } this.url = url; }