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