/////////////////////////////////////////////////////////////////////////// // 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; } }
/// <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; } }
/* * 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; }