I want to create a WebBrowser control, give it HTML to display, and then render it inoperative in my Internet Explorer window.
Can this be done?
- yes, it must be outside the process
- I already have a method that involves writing a temporary file. I want to remove this hacking solution
I have three more questions about starting stackoverflow, all working on a tiny segment of the work of the following code:
public static void SpawnIEWithSource(String szHtml)
{
IWebBrowser2 ie = (IWebBrowser2)new InternetExplorer();
object mv = System.Reflection.Missing.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 code above works great in native applications.
I thought I would move on to the persecution and see if anyone had any other ideas that were not related to the only way I could understand this.
A hack solution using a temporary file is as follows:
public static void SpawnIEWithSource(String szHtml)
{
IWebBrowser2 ie = (IWebBrowser2)new InternetExplorer();
object mv = System.Reflection.Missing.Value;
object url = (String)@"about:blank";
ie.Navigate2(ref url, ref mv, ref mv, ref mv, ref mv);
String tempFilename = Path.GetTempFileName();
try
{
String htmlFile = Path.ChangeExtension(tempFilename, "htm");
File.Move(tempFilename, htmlFile);
tempFilename = htmlFile;
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);
while (ie.ReadyState != tagREADYSTATE.READYSTATE_COMPLETE)
{
System.Threading.Thread.Sleep(10);
}
}
finally
{
File.Delete(tempFilename);
}
ie.Visible = true;
}