在学习WebBrowser控件时遇到的类型转换问题?
在万一的Dephi博客看到一篇介绍TWebBrowser控件使用的例子,用CB改写下,遇到了类型转换错误,不知道如何解决?
IHTMLWindow2* window; window = ((IHTMLDocument2*)WebBrowser1->Document)->parentWindow; window->document->body->innerHTML = "<hr>万一的 Delphi 博客<hr>"; window->alert("JavaScript 的提示: 修改成功!");
while (CppWebBrowser1->Busy) Application->ProcessMessages();IHTMLDocument2 *spDoc = NULL;HRESULT hr = CppWebBrowser1->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();}
[解决办法]
WebBrowser也是一样的,以下代码在XE2中测试通过。
__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(); }}