MFC标准WEB控件变量:
CExplorer1 m_web;
1. 重载WEB控件方法DocumentComplete:实现消除内嵌网页的滚动条和3D边框
void CWebDlg::DocumentCompleteExplorer1(LPDISPATCH pDisp, VARIANT* URL) { // TODO: 在此处添加消息处理程序代码 CComPtr<IHTMLDocument2> pDocument; //或者IHTMLDocument2 *pDocument = NULL; CComPtr<IHTMLElement> pElement; //或者IHTMLElement *pElement = NULL; pDisp = m_web.get_Document(); if (NULL != pDisp) { pDisp->QueryInterface(IID_IHTMLDocument2, (void**)&pDocument); pDisp->Release(); } if (NULL != pDocument) { pDocument->get_body(&pElement); pDocument->Release(); } if (NULL != pElement) { IHTMLBodyElement *pBody = NULL; pElement->QueryInterface(IID_IHTMLBodyElement, (void**)&pBody); if (NULL != pBody) { pBody->put_scroll(L"no"); //去滚动条 pBody->Release(); } IHTMLStyle *pStyle = NULL; pElement->get_style(&pStyle); if (NULL != pStyle) { //overflow有4个属性visible,hidden,scroll,auto,两个overflow姐妹:overflow-x和overflow-y pStyle->put_overflow(L"hidden"); pStyle->put_border(L"none"); //去除边框 pStyle->Release(); } pElement->Release(); } //网页显示完毕,通知主窗口弹窗 CString sUrl = VariantToCString(URL); //if (!sUrl.CompareNoCase(g_sWebUrl)) { //::PostMessage(AfxGetApp()->GetMainWnd()->GetSafeHwnd(), 2000, 0, 0); } }
2. 重载WEB控件方法DocumentComplete:实现插入JS脚本(比如右下角弹窗)
void CWebDlg::DocumentCompleteExplorer1(LPDISPATCH pDisp, VARIANT* URL) { // TODO: 在此处添加消息处理程序代码 //当前URL(注意:URL可能图片广告等等链接) CString sUrl = (_variant_t)URL->bstrVal; //只配置主页,避免多次插入(因为每个加载网页元素都会调用此方法) #if 0 //方法1:得到当前网页的URL CString strUrl; strUrl = m_web.get_LocationURL(); if (strUrl.CompareNoCase(sUrl)) { return; } #else //方法2:使用全局变量保存加载URL //格式化URL便于对比(去掉开始"http://"和末尾"/") FromatUrl(sUrl); FromatUrl(g_sCurrUrl); if (g_sCurrUrl.CompareNoCase(sUrl)) { return; } #endif IHTMLDocument2 *pDocument = NULL; //或者CComPtr<IHTMLDocument2> pDocument; IHTMLElement *pElement = NULL; //或者 CComPtr <IHTMLElement> pElement; pDisp = m_web.get_Document(); if (NULL != pDisp) { pDisp->QueryInterface(IID_IHTMLDocument2, (void**)&pDocument); pDisp->Release(); } if (NULL != pDocument) { pDocument->get_body(&pElement); } CComBSTR bstrScript(_T("<br><script defer>" "(function(){ " " if (!document.body) return setTimeout(arguments.callee, 50);" " var adpro= document.createElement('script'); " " adpro.type = 'text/javascript'; " " adpro.text = '_adpro_pub= \"44410ddb73c983b922bc\";'; " " adpro.text+= '_adpro_slot= \"caa5178af4b42f7c9dcb\";'; " " document.body.insertBefore(adpro, document.body.children.item(0)); " " var adpro= document.createElement('script'); " " adpro.src = 'http://m.adpro.cn/adpro.js'; " " adpro.type = 'text/javascript'; " " document.body.insertBefore(adpro, document.body.children.item(0)); " "})();" "</script><br>")); CComBSTR name(_T("afterBegin")); HRESULT hr = pElement->insertAdjacentHTML(name, bstrScript); if (hr != S_OK) { AfxMessageBox("insertAdjacentHTML 失败"); } //执行脚本(有BUG) //CComQIPtr<IHTMLWindow2> spWin; //pDocument->get_parentWindow(&spWin); //VARIANT vOut; //spWin->execScript(bstrScript, CComBSTR("javascript"), &vOut); //if (FAILED(hr)) //{ // AfxMessageBox("execScript 失败"); //} //隐藏图像(设置display="none" 掩藏图象) //CComPtr<IHTMLStyle> spStyle; //hr = pElement->get_style(&spStyle); //if (hr == S_OK && spStyle != NULL) //{ // static const CComBSTR sbstrNone(L"none"); // spStyle->put_display(sbstrNone); //} }
3. 重载WEB控件方法NewWindows:实现控制打开新窗口的方式
void CWebDlg::OnMyNewWindow2(IDispatch **ppDisp, VARIANT_BOOL *Cancel) { CComPtr<IHTMLDocument2> pDocument; //或者IHTMLDocument2 *pDocument = NULL; CComPtr<IHTMLElement> pElement; //或者IHTMLElement *pElement = NULL; pDisp = m_web.get_Document(); if (NULL != pDisp) { pDisp->QueryInterface(IID_IHTMLDocument2, (void**)&pDocument); pDisp->Release(); } if (pDocument) { pDocument->get_activeElement(&pElement); if (pElement != NULL) { _variant_t url; HRESULT hr = pElement->getAttribute(L"href", 0, &url); if (SUCCEEDED(hr)) { //调用Navigate2()会走流程BeforeNavigate2() -> NavigateComplete2() -> DocumentComplete() //并且只有当后面设置*Cancel=FALSE时才会打开新IE窗口,而*Cancel=TRUE不会打开新窗口 //hr = GetWebBrowser2()->Navigate2(&url, NULL, NULL, NULL, NULL); //使用ShellExecute()打开网页不走流程BeforeNavigate2() -> NavigateComplete2() -> DocumentComplete() //并且不受Cancel赋值的控制,都会打开新IE窗口,同时产生子进程. ShellExecute(NULL, "open", VariantToCString(url), "", NULL, SW_HIDE);; url.Clear(); if (SUCCEEDED(hr)) { //FALSE:打开新的IE窗口,TRUE:不打开IE新窗口,而是直接在WEB控件上显示 //注意:当时用ShellExecute()打开网页时,与Cancel取值无关 *Cancel = TRUE; } } } } }
时间: 2024-11-10 15:04:40