4

MSDNで入手できる次の優れた記事をご覧ください。

BandObjectsを使用して IE ツールバーを作成中です。

WebBrowser コントロールにアクセスできますが、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);
}
4

1 に答える 1

3

ネイティブの IE オブジェクト モデルで少し迷っただけです。これにより、独自の HTML をブラウザーにロードするのが少し面倒になります。

実際に WebBrowser コントロールを使用する場合は問題ありません。接着剤は .NET ラッパーに隠されています。とてもシンプルで、DocumentText プロパティを割り当てるだけです。プロパティ セッターは騒ぎを処理します。DocumentCompleted イベント ハンドラーは必要ありません。すべて、どこにでも配置できる 1 行のコードに折りたたまれます。

   webBrowser1.DocumentText = "<h1>Hello world</h1>";
于 2013-07-29T19:40:37.447 に答える