703
社区成员
发帖
与我相关
我的任务
分享
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
CppWebBrowser1->Navigate(L"about:blank");
}
#include <mshtml.h>
// ---------------------------------------------------------------------------
//
bool __fastcall ExecuteScript(IHTMLDocument2 *spDoc, WideString strScript, WideString strLang)
{
bool bRet = false;
if (spDoc)
{
IHTMLWindow2 *pWin;
HRESULT hr = spDoc->get_parentWindow(&pWin);
if (SUCCEEDED(hr))
{
VARIANT vRet;
hr = pWin->execScript(strScript, strLang, &vRet);
pWin->Release();
bRet = SUCCEEDED(hr);
}
}
return bRet;
}
// ---------------------------------------------------------------------------
//
void __fastcall TForm1::Button1Click(TObject *Sender)
{
while (CppWebBrowser1->Busy)
Application->ProcessMessages();
IHTMLDocument2 *spDoc = NULL;
HRESULT hr = CppWebBrowser1->Document->QueryInterface(
::IID_IHTMLDocument2, (void **)&spDoc);
if (SUCCEEDED(hr))
{
WideString strScript = L"alert(3);";
ExecuteScript(spDoc, strScript, L"javascript");
}
spDoc->Release();
}