在Delphi中可以从TWebBrowser对象中直接获取document对象,方法如下:
IHTMLDocument2 htmlDoc := WebBrowser1.Document as IHTMLDocument2;
但是在c++builder中却不可以,即TWebBrowser没有Document成员。但c++builder中获取document对象可以使用如下方法:
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
WebBrowser1->Navigate(L"www.ccrun.com");
}
//---------------------------------------------------------------------------
#include <mshtml.h>
void __fastcall TForm1::Button1Click(TObject *Sender)
{
while (WebBrowser1->Busy)
Application->ProcessMessages();
IHTMLDocument2 *spDoc = NULL;
HRESULT hr = WebBrowser1->Document->QueryInterface(
::IID_IHTMLDocument2, (void **)&spDoc);
if (SUCCEEDED(hr))
{
IHTMLElement *pElement;
hr = spDoc->get_body(&pElement);
if (SUCCEEDED(hr))
{
pElement->put_innerHTML(L"<a href=\"www.ccrun.com\">C++Builder研究</a>");
pElement->Release();
}
spDoc->Release();
}
}