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();}
阅读全文
1 0
- VC/MFC 从WebBrower 中获取 HTML 和文本
- Delphi直接从流中读取HTML到WebBrower中
- VC MFC中修改STATIC的文本颜色和文本字体等
- VC MFC中修改STATIC的文本颜色和文本字体等
- html获取下拉列表中的值和文本值
- 直接将html字符串读入webbrower中
- jQuery获取表单中控件的值和文本
- MFC实现工具栏插入图片和文本
- VC/MFC中指针的获取
- 【vc】MFC应用程序中指针的获取
- C# html编码和文本的转换
- HTML容器标签和文本标签
- VC++------------MFC选择文件夹和文件对话框
- 【MFC】VC 删除目录和文件
- 获取select标签值和文本
- jquery获取select的中值和文本
- 获取select控件的值和文本
- 从一个视图获取另一个视图(vc mfc)
- C++ break 和 cintinue
- Amoeba实现Mysql主从复制读写分离
- Butterknife原理解析
- 关于Qt中LOGO与窗口图标显示问题的总结
- SSD固态硬盘优化方案,让新买的SSD速度不再慢
- VC/MFC 从WebBrower 中获取 HTML 和文本
- Dihedral Group
- CSDN-markdown编辑器
- [操作系统] I/O软件原理
- Java 字符串去中文(数字)
- Linux下去掉^M的方法
- Spring MVC学习
- 用结构体数组写的一个通讯录
- 关于引用 简单数组数组排序 和 最大值次最大值数组元素