简体   繁体   中英

.NET: How to make WebBrowser control launch in IE, display HTML, out of process?

i want to create a WebBrowser control, give it the HTML to display, then make it appear out of process in its own Internet Explorer window.

Can it be done?


  • yes it has to be out of process
  • i already have a technique that involves writing a temporary file. i want to remove this hack solution

i have three other questions on stackoverflow running, all working on a tiny segment of making the following code work:

public static void SpawnIEWithSource(String szHtml)
{
   IWebBrowser2 ie = (IWebBrowser2)new InternetExplorer();

   object mv = System.Reflection.Missing.Value; //special "nothing" value
   object url = (String)@"about:blank";
   ie.Navigate2(ref url, ref mv, ref mv, ref mv, ref mv);

   mshtml.HTMLDocumentClass doc = (mshtml.HTMLDocumentClass)ie.Document;
   doc.Write(szHtml);
   doc.Close();

   ie.Visible = true;
}

Note: The above code works fine in native apps.

i thought i would cut to the chase, and see if anyone has any other ideas, that don't involve the only way i've been able to figure it out.


The hack solution, that uses a temporary file, is:

public static void SpawnIEWithSource(String szHtml)
{
   IWebBrowser2 ie = (IWebBrowser2)new InternetExplorer();

   object mv = System.Reflection.Missing.Value; //special "nothing" value
   object url = (String)@"about:blank";
   ie.Navigate2(ref url, ref mv, ref mv, ref mv, ref mv);

   //Todo: Figure out the .NET equivalent of the following
   //so that it doesn't have to write a temporary file
   //IDispatch webDocument = ie.Document;
   //webDocument.Write(szHtml);
   //webDocument.Close();

   String tempFilename = Path.GetTempFileName();
   try
   {
      //Rename to .htm, or else ie won't show it as actual HTML
      String htmlFile = Path.ChangeExtension(tempFilename, "htm");
      File.Move(tempFilename, htmlFile); //.NET's version of File.Rename
      tempFilename = htmlFile;

      //Write string to file
      StreamWriter writer = new StreamWriter(tempFilename);
      writer.Write(szHtml);
      writer.Close();

      url = (String)tempFilename;
      ie.Navigate2(ref url, ref mv, ref mv, ref mv, ref mv);

      //If we're going to delete the file, then we have to wait for IE to use it
      //else we delete it before it uses it
      while (ie.ReadyState != tagREADYSTATE.READYSTATE_COMPLETE)
      {
         System.Threading.Thread.Sleep(10);
      }
   }
   finally
   {
      File.Delete(tempFilename);
   }

   //Make IE Visible
   ie.Visible = true;
}

The way I have done this before, is to save the HTML file to disk, and pass the filename to the shell, like this:

 System.Diagnostics.Process p = new System.Diagnostics.Process();
 p.StartInfo.FileName = "C:\Path\To\myHTMLFile.html";
 p.StartInfo.UseShellExecute = true;
 p.StartInfo.RedirectStandardOutput = false;
 p.StartInfo.Arguments = "";
 p.Start();

The neat thing about this is that it will open with the user's default web browser. If you needed IE, then you'd likely have to dig into the registry and get its path, and execute it specifically.

You'll also probably want to dispose of the process when it's done. There's an event you can subscribe to, called Exited, that will inform you when the process can be disposed.

Load the WebBrowser control with HTML like the example below.
Maybe not a separate process per se, but gets the job done. Let me know if it truly needs to be in a separate process and I may be able to work something up like that. Which involve maybe the System.Diagnostics.Process.start() method and some JavaScript.

        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title></title>
        </head>
        <body>
        <div id="holdingArea" style="display:none">
            <div>
            <strong>In a new page</strong>
            </div>
        </div>
        <script type="text/javascript">
            var h = document.getElementById('holdingArea').innerHTML;
            var w = window.open('', '_blank');
            w.document.write(h);
        </script>
        </body>
        </html>

I use this to create a new IE window with a unique random handle to enable multiple windows to be open at the same time.

 Dim WindowName As String = CStr(Rnd())
 WindowName = "W" & Replace(WindowName, ".", "0")
 Page.ClientScript.RegisterStartupScript(Me.GetType(), WindowName, WindowName & "=window.open('NominateList.aspx?Inviters=" & InviterList & "','" & WindowName & "','menubar=1,resizable=1,scrollbars=1,status=1,width=900,height=900');" & WindowName & ".moveTo(0,0);", True)

I can use this to spin up as many pages as needed. Hope this helps.

WebBrowser control has the DocumentText property. If you set it to an HTML string, the control will render proper HTML on the screen. Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM