Beispiel #1
0
        /*
         * 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;
        }
Beispiel #2
0
        /*
         * 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;
        }