CppWebBrowser执行js脚本问题
IHTMLDocument2 *pDoc2;
IDispatch *WebDocument = static_cast<IDispatch*>(CppWebBrowser1->Document);
pDoc2 = static_cast<IHTMLDocument2*>(WebDocument);
IHTMLWindow2 *PWin2 = NULL;
pDoc2->get_parentWindow(&PWin2);
VARIANT vtEmpty;
vtEmpty.vt = VT_EMPTY;
jsName = "alert(3);" ;
BSTR bstrCode = WideString(jsName).c_bstr();
BSTR bstrLang = WideString("JavaScript").c_bstr();
if (pDoc2) {
MessageBox(0, jsName.c_str(), "提示", MB_OK + MB_ICONINFORMATION);
}
try
{
PWin2->execScript(bstrCode, bstrLang, &vtEmpty);
}
__finally
{
SysFreeString(bstrCode);
SysFreeString(bstrLang);
VariantClear(&vtEmpty);
}
我在页面没有看到对话框,为什么?
[解决办法]
来生意了,挖卡卡.
- C/C++ code
__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();}
[解决办法]
抢个板凳,坐下看看
[解决办法]
逛逛 CSDN ,无意又从妖哥学了一手,嘿嘿