1.关于嵌入网页URL;
Navigate(url);
2. 拦截网页中URL地址
ON_EVENT(CHtmlDL, AFX_IDC_BROWSER, 250, _OnBeforeNavigate2, VTS_DISPATCH VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PBOOL)
方法:
:_OnBeforeNavigate2(LPDISPATCH pDisp, VARIANT FAR* URL, VARIANT FAR* Flags, VARIANT FAR* TargetFrameName, VARIANT FAR* PostData, VARIANT FAR* Headers, BOOL FAR* Cancel)
{
}
3. MFC调用网页中的 JavaScript
IHTMLDocument2* pDocument;
HRESULT hr1 =m_Html.GetDHtmlDocument(&pDocument);
if(FAILED(hr1))
return;
IHTMLWindow2* pWindow;
HRESULT hr2 =pDocument->get_parentWindow(&pWindow);
if(FAILED(hr2))
return;
VARIANT ret;
ret.vt = VT_EMPTY;
CString strfunc1("refreshFlash()");
HRESULT hr3 = pWindow->execScript(strfunc1.AllocSysString(),L"JavaScript",&ret);
if(FAILED(hr3))
return;
//清理工作,释放 Document 和 window节点
pDocument->Release();
pWindow->Release();
一开始
GetDHtmlDocument方法总是获得为空对象;后来查看了了 关于 HtmlDiaglog的源代码中; 需要首先调用 _OnNavigateComplete2 这个方法
GetDHtmlDocument方法总是获得为空对象;后来查看了了 关于 HtmlDiaglog的源代码中; 需要首先调用 _OnNavigateComplete2 这个方法
void CHtmlDL::_OnNavigateComplete2(LPDISPATCH pDisp, VARIANT FAR* URL)
{
CDHtmlDialog::_OnNavigateComplete2(pDisp,URL);
}
和
void CHtmlDL::_OnDocumentComplete(LPDISPATCH pDisp, VARIANT FAR* URL)
{
}
请仔细查看 : dlgdhtml.cpp 源文件!!
GetDoucuemt 中需要 把 m_spHtmlDoc 变量赋值; 这个赋值过程在
void CDHtmlDialog::OnNavigateComplete(LPDISPATCH pDisp, LPCTSTR szUrl) //框架中的源文件 !!!!!!
{
if (pDisp != m_pBrowserApp)
return;
IDispatch *pdispDoc = NULL;
m_pBrowserApp->get_Document(&pdispDoc);
if (!pdispDoc)
return;
ASSERT(m_spHtmlDoc==NULL);
pdispDoc->QueryInterface(IID_IHTMLDocument2, (void **) &m_spHtmlDoc); // 注意此处 !!!你会明白什么!
if (m_bUseHtmlTitle)
{
CComBSTR bstrTitle;
m_spHtmlDoc->get_title(&bstrTitle);
CString str = CString(bstrTitle);
SetWindowText(str);
}
m_strCurrentUrl = szUrl;
ConnectDHtmlEvents(pdispDoc);
pdispDoc->Release();
}
所以我们 还要实现 _OnNavigateComplete2 和 _OnDocumentComplete 事件
ON_EVENT(CHtmlDL, AFX_IDC_BROWSER, 259 /* DocumentComplete */, _OnDocumentComplete, VTS_DISPATCH VTS_PVARIANT)
ON_EVENT(CHtmlDL, AFX_IDC_BROWSER, 252 /* NavigateComplete2 */, _OnNavigateComplete2, VTS_DISPATCH VTS_PVARIANT)
void CHtmlDL::_OnNavigateComplete2(LPDISPATCH pDisp, VARIANT FAR* URL)
{
CDHtmlDialog::_OnNavigateComplete2(pDisp,URL);
}
void CDHtmlDialog::_OnDocumentComplete(LPDISPATCH pDisp, VARIANT* URL)
{
ASSERT(V_VT(URL) == VT_BSTR);
CString str(V_BSTR(URL));
OnDocumentComplete(pDisp, str);
}