VC/MFC 从WebBrower 中获取 HTML 和文本

来源:互联网 发布:李宗仁故居风水数据 编辑:程序博客网 时间:2024/06/07 17:19

本文部分转载于 http://blog.chinaunix.net/uid-2516614-id-2496197.html

用于参考


///////////////////////////////////////////////////////////////

外部窗口接口获取(非原博客,是自己添加的其他通过获取IE浏览器窗口的句柄,在由句柄转换成IE的接口):


void CHTMLContrlDlg::OnBnClickedButtonOpen(){// TODO: 在此添加控件通知处理程序代码HWND ExplorerWnd = ::FindWindow(NULL, _T("CrystalDiskMark 3.0 x64"));//根据IE主窗口获取浏览器窗口    if (!ExplorerWnd)::MessageBox(m_hWnd, TEXT("CrystalDiskMark - Internet Explorer"), NULL, MB_OK);else{if (m_WinThread){return;}m_WinThread  = AfxBeginThread(MyThreadRead, (void*)this);::SetForegroundWindow(ExplorerWnd);FindWindowsHwnd(ExplorerWnd);}}


void CHTMLContrlDlg::FindWindowsHwnd(HWND hWnd){HWND hWndChild = NULL;::EnumChildWindows(hWnd, EnumChildProc, (LPARAM)&hWndChild);if (NULL == hWndChild) return;UINT nMsg = ::RegisterWindowMessage(_T("WM_HTML_GETOBJECT"));LRESULT lRes;::SendMessageTimeout(hWndChild, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes);CComPtr < IHTMLDocument2 > spDoc;HRESULT hr = ::ObjectFromLresult(lRes, IID_IHTMLDocument2, 0, (LPVOID *)&spDoc);if (FAILED(hr)) return ;// 程序运行到此,已经找到了 IHTMLDocument2 的接口指针CComBSTR bstrTitle;spDoc->get_title(&bstrTitle);//取得文档标题  CString str(bstrTitle);SetWindowText(str);CComPtr < IHTMLElementCollection > spElementCollection;hr = spDoc->get_all(&spElementCollection);if (FAILED(hr)){return;}long elementLength;hr = spElementCollection->get_length(&elementLength);////获取HTML元素数量if (FAILED(hr)){return;}       //接下来就可以通过指针循环查找元素,根据自己的查找条件判别要找的元素VARIANT name;CComBSTR tag;name.vt = VT_I4;for (int i = 0; i < elementLength; i++){name.lVal = i;IDispatch * pDispatch = NULL;HRESULT res = spElementCollection->item(name, name, &pDispatch);if (FAILED(res)){continue;}CComPtr<IHTMLElement> pHtmlElement;////IHTMLSelectElement是你想找的元素类型,
还有IHTMLSpanElement、IHTMLElement、IHTMLSpanElement等hr = pDispatch->QueryInterface(IID_IHTMLElement, (void**)&pHtmlElement);        
if (FAILED(hr)){continue;}BSTR id;BSTR _innerText;pHtmlElement->get_id(&id);pHtmlElement->get_innerText(&_innerText);
           }
}

///////////////////////////////////////////////////////////////

 本代码是关于 从 ACTIVEX控件 WebBrower 中获取 HTML 和文本。本代码来自网络,从哪里来不记得了。  原来代码中有获得HTML,但是我项目中的目的是得到HTML中的文本。料想 自己还要写一个HTML-->TXT的代码来解决最后的问题。写着写着看到 hr=pElement->get_outerHTML();这个代码 和javascript中的inner***很类似。如是利用自动不起功能居然发现了pElement->get_outerText(&pContent);于是直接省略了HTML-->TXT的代码。。噢 吔!!!pBrowse = (CWebBrowser2*)this->GetDlgItem(IDC_EXPLORER3);    IHTMLDocument2 *pHTMLDocument=NULL;    if (!(pHTMLDocument = (IHTMLDocument2*)pBrowse->GetDocument()))        return;    CComPtr<IHTMLElementCollection> pAllColl;    HRESULT hr;    hr=pHTMLDocument->get_all(&pAllColl);    if(hr==S_OK){        LONG length=0;        hr=pAllColl->get_length(&length);        if(hr==S_OK){            for(int i=0;i<length;i++){                VARIANT vIndex,vName;                vName.vt=vIndex.vt=VT_I4;                vName.lVal=vIndex.lVal=i;                CComPtr<IDispatch> pDisp;                hr=pAllColl->item(vName,vIndex,&pDisp);                if( hr==S_OK ){                    CComPtr<IHTMLElement> pElement;                    hr=pDisp->QueryInterface(IID_IHTMLElement,(void**)&pElement);                    if( hr==S_OK ){                        CComBSTR tagName;                        hr=pElement->get_tagName(&tagName);                        if(hr==S_OK){                            CString str(tagName);                            if(str=="HTML"){                                CComBSTR pContent;                                hr=pElement->get_outerText(&pContent);                                //hr=pElement->get_outerHTML();                                if(hr==S_OK){                                    UpdateData(true);                                    m_text = CString(pContent);                                    UpdateData(false);                                    i=length;//以便退出循环                                }                                else{//if get_outerHTML failed                                 MessageBox("can't get html code");                                }                            }//else if tagName isnot 'HTML'                        }//else if get_tagName failed                    }//else if don't get IHMTLElement interface                }//if no items            }        }//if get_length failed    }//if get_all failed    pHTMLDocument->Release();}