標籤:cWeb style http io ar os 使用 sp for
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:得到當前網頁的URLCString 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;}}}}}
WebBrowser控制項提示