views:

1259

answers:

5

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;
}
A: 

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"&gt;
        <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>
tyndall
It doesn't have to be a separate process, but it does have to open up inside Internet Explorer.
Ian Boyd
+1  A: 

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.

Charlie Salts
Yeah, i'm trying to find a .NET equivalent to my existing functional solution that doesn't require a temporary file.
Ian Boyd
I'm not sure your what your larger goal is, but there's always the possibility that ie.Navigate2( ) could navigate to a file on a webserver. So you're still accessing a file, but it's somewhere else. Again, I am not sure if this would help. Your browser MUST get its html from somewhere!
Charlie Salts
A: 

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.

Leo Moore
Where do call this code from?
Ian Boyd
Behind a button. i.e. click the button and up the new page appears keeping your original page open.
Leo Moore
i can't find any Page class in....ohhh...you think i want to do it from a browser. i want to open a browser from managed code (.NET)
Ian Boyd
A: 

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!

A: 

i need to make registration page like the registration page of Yahoo. When i make new mail by html and java script and i don't know how to do that so i need help???

soso
i tried to fix the grammer on this - but i have no idea what he's asking. Either way, this needs to be it's own question, not an answer on this one.
Ian Boyd