枚举所有iframe的IWebBrowser2
// Get the IDispatch of the document. // LPDISPATCH lpDisp = NULL; lpDisp = m_webBrowser.GetDocument(); if (lpDisp) { IOleContainer* pContainer; // Get the container. // HRESULT hr = lpDisp->QueryInterface(IID_IOleContainer, (void**)&pContainer); lpDisp->Release(); if (FAILED(hr)) return hr; // Get an enumerator for the frames. // IEnumUnknown* pEnumerator; hr = pContainer->EnumObjects(OLECONTF_EMBEDDINGS, &pEnumerator); pContainer->Release(); if (FAILED(hr)) return hr; IUnknown* pUnk; ULONG uFetched; // Enumerate and refresh all the frames. // for (UINT i = 0; S_OK == pEnumerator->Next(1, &pUnk, &uFetched); i++) { // QI for IWebBrowser here to see whether we have // an embedded browser. IWebBrowser2* pWebBrowser; hr = pUnk->QueryInterface(IID_IWebBrowser2, (void**)&pWebBrowser); pUnk->Release(); if (SUCCEEDED(hr)) { // Refresh the frame. pWebBrowser->Refresh(); pWebBrowser->Release(); } } pEnumerator->Release(); }
枚举所有iframe的element
HRESULT hr = S_OK; try { CComPtr<IDispatch> spDispDoc; hr = m_spWebBrowser->get_Document(&spDispDoc); // find all iframe elements and filter by id, then insert ad js CComQIPtr<IHTMLDocument2> spDoc2 = spDispDoc; if(spDoc2){ CComQIPtr<IHTMLElementCollection> spAll; hr = spDoc2->get_all(&spAll); if (SUCCEEDED(hr)) { CComPtr<IDispatch> spDispObjects; // just handle iframes as example CComVariant varTag = _T("IFRAME"); varTag.vt = VT_BSTR; hr = spAll->tags(varTag, &spDispObjects); if(SUCCEEDED(hr)) { CComQIPtr<IHTMLElementCollection> spElementCollection = spDispObjects; long lObjectCount = 0; if( spElementCollection ) hr = spElementCollection->get_length(&lObjectCount);if (SUCCEEDED(hr) && lObjectCount > 0) { for (int i = 0; i < lObjectCount; i++) { CComVariant vIndex = i; CComVariant vEmpty; CComPtr<IDispatch> spdispObject; hr = spElementCollection->item(vIndex, vEmpty, &spdispObject); if (SUCCEEDED(hr)) { CComQIPtr<IHTMLElement> spElement = spdispObject; if (spElement) { CComBSTR bstrId = _T("");; long width = 0, height = 0; spElement->get_id(&bstrId); spElement->get_offsetHeight(&height); spElement->get_offsetWidth(&width); if(!IsAcceptableSize(width, height)) continue;
CComVariant vSrcUrl = _T(""); hr = spElement->getAttribute(_T("src"), 0, &vSrcUrl);
} } } } } } } } catch(_com_error &ex) { LPCTSTR errMsg = ex.ErrorMessage(); WriteLog(_T("_com_error: ") + CString(errMsg)); } return hr;