如何在托管WebBrowser控件时获取文档界面?

时间:2013-07-22 10:25:38

标签: c# .net internet-explorer webbrowser-control mshtml

请查看MSDN

上提供的以下优秀文章

我正在BandObjects

的帮助下创建IE工具栏

我可以访问WebBrowser Control但无法实例化修改DOM所需的HTMLDocument。

以下是我的代码的摘录:

// Registering for DocumentComplete envent 
Explorer.DocumentComplete += new SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(Explorer_DocumentComplete);

// I am not sure about the following function. 
// I am trying to do something as suggested in this MSDN article - 
// http://msdn.microsoft.com/en-us/library/aa752047%28v=vs.85%29.aspx#Document

void Explorer_DocumentComplete(object pDisp, ref object URL)
{  
        IPersistStream pStream;
        string htmlText = "<html><h1>Stream Test</h1><p>This HTML content is being loaded from a stream.</html>";

        HTMLDocumentClass document = (HTMLDocumentClass)this.Explorer.IWebBrowser_Document;
        document.clear();
        document.write(htmlText);
        IHTMLDocument2 document2 = (IHTMLDocument2)pDisp;

        pStream = (IPersistStream)document2.queryCommandValue("IID_IHTMLDocument2");
        HtmlDocument objdec = webBroswer.Document;
        objdec.Write(htmlText);
}

1 个答案:

答案 0 :(得分:3)

你在本机IE对象模型中有点迷失。这确实使您将自己的HTML加载到浏览器中有点痛苦。

如果您实际使用WebBrowser控件,那不是问题。胶水隐藏在.NET包装器中。这非常简单,只需指定DocumentText property即可。酒店负责人照顾喧闹。您不需要DocumentCompleted事件处理程序,它可以折叠到您可以放在任何位置的单行代码:

   webBrowser1.DocumentText = "<h1>Hello world</h1>";