.NET: How to launch WebBrowser control in IE, display HTML from a process?

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; //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 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; //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 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;
}
+3
4

, HTML , :

 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();

, -. IE, , , .

, , , . "", , .

+1

WebBrowser HTML, . , , . , , . , , System.Diagnostics.Process.start() 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>
0

, IE , .

 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)

, , . , .

0

WebBrowser DocumentText. HTML, HTML. , !

0

Source: https://habr.com/ru/post/1702330/


All Articles