DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  4737 随笔 :: 2 文章 :: 542 评论 :: 1615万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

1.独立代码

//-----------开始---------------------//
#include <atlbase.h>
#include <mshtml.h>
#include <winuser.h>
#include <comdef.h>
#include <string.h>
void EnumIE(void);//处理网页
CComModule _Module;  //使用CComDispatchDriver ATL的智能指针,此处必须声明
#include <atlcom.h>
void EnumAllElement(IHTMLDocument2 * pIHTMLDocument2);

void EnumIE(void)  
{  
  CComPtr<IShellWindows> spShellWin;  
  HRESULT hr=spShellWin.CoCreateInstance(CLSID_ShellWindows);  
  if (FAILED(hr))  
  {  
  return;  
  }      

  long nCount=0;    //取得浏览器实例个数(Explorer和IExplorer)  
  spShellWin->get_Count(&nCount);  
  if (0==nCount)  
  {  
    return;  
  }

  for(int i=0; i<nCount; i++)  
  {  
    CComPtr<IDispatch> spDispIE;  
    hr=spShellWin->Item(CComVariant((long)i), &spDispIE);  
    if (FAILED(hr)) continue;
 
    CComQIPtr<IWebBrowser2>spBrowser=spDispIE;  
    if (!spBrowser) continue;
 
    CComPtr<IDispatch> spDispDoc;  
    hr=spBrowser->get_Document(&spDispDoc);  
    if (FAILED(hr)) continue;
 
    CComQIPtr<IHTMLDocument2>spDocument2 =spDispDoc;  
    if (!spDocument2) continue;      

 //Modify by jncao 2007-09-17
 //*******************************************************************************
 CString cIEUrl_Filter;  //设置的URL(必须是此URL的网站才有效);
    cIEUrl_Filter="http://127.0.0.1:8082/csp/"; //设置过滤的网址
    //*******************************************************************************

    CComBSTR IEUrl;
 spBrowser->get_LocationURL(&IEUrl);
 CString cIEUrl_Get;     //从机器上取得的HTTP的完整的URL;
 cIEUrl_Get=IEUrl;
 cIEUrl_Get=cIEUrl_Get.Left(cIEUrl_Filter.GetLength()); //截取前面N位

 if (strcmp(cIEUrl_Get,cIEUrl_Filter)==0)
 {
     // 程序运行到此,已经找到了IHTMLDocument2的接口指针      
       EnumAllElement(spDocument2);//枚举所有元素

 }   
  }  
}

 

void EnumFrame(IHTMLDocument2 * pIHTMLDocument2)
{  
 if (!pIHTMLDocument2) return;      
 HRESULT   hr;  
   
 CComPtr<IHTMLFramesCollection2> spFramesCollection2;  
 pIHTMLDocument2->get_frames(&spFramesCollection2); //取得框架frame的集合  
   
 long nFrameCount=0;        //取得子框架个数  
 hr=spFramesCollection2->get_length(&nFrameCount);  
 if (FAILED(hr)|| 0==nFrameCount) return;  
   
 for(long i=0; i<nFrameCount; i++)  
 {  
  CComVariant vDispWin2; //取得子框架的自动化接口  
  hr = spFramesCollection2->item(&CComVariant(i), &vDispWin2);  
  if (FAILED(hr)) continue;      
  CComQIPtr<IHTMLWindow2>spWin2 = vDispWin2.pdispVal;  
  if (!spWin2) continue; //取得子框架的   IHTMLWindow2   接口      
  CComPtr <IHTMLDocument2> spDoc2;  
  spWin2->get_document(&spDoc2); //取得子框架的   IHTMLDocument2   接口
  
  EnumAllElement(spDoc2);      //递归枚举当前子框架   IHTMLDocument2   上的所有控件
 }  
}

 

 

void EnumAllElement(IHTMLDocument2 * pIHTMLDocument2) //枚举所有字段
{
 if (!pIHTMLDocument2) return; 
 EnumFrame(pIHTMLDocument2);   //递归枚举当前IHTMLDocument2上的子框架frame  
 HRESULT   hr;  

 CComQIPtr<IHTMLElementCollection> spAllElement;
 hr=pIHTMLDocument2->get_all(&spAllElement);//获取所有网页内所有元素
 if (FAILED(hr))  return;  

 long nLength = 0;
 spAllElement->get_length (&nLength);
 for (int i = 0; i < nLength; i++)
 {
        CComPtr<IDispatch> pDisp;
  hr = spAllElement->item(COleVariant((long)i),COleVariant((long)0),&pDisp); //获取单个元素
  if(SUCCEEDED(hr))
  {
   //CComQIPtr <IHTMLElement, &IID_IHTMLElement> pElement(pDisp);
   CComQIPtr<IHTMLElement, &IID_IHTMLElement> pElement;
   pDisp->QueryInterface(&pElement);
   BSTR bTemp;
   pElement->get_id(&bTemp);//可以获取其他特征,根据具体元素而定
   CString strTemp=bTemp;
   if(!strTemp.IsEmpty() && strTemp=="callNo")//根据callNo(效能提升text控件id)是主叫号码获取值或作其他处理
   {
    IHTMLInputTextElement* input;
    pDisp->QueryInterface(IID_IHTMLInputTextElement,(void**)&input);
    input->get_value(&bTemp);
    if(bTemp==NULL) strTemp="null";
    else strTemp=bTemp;
    CStdioFile ioFile;
    ioFile.Open("callerno.txt",CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate);
    ioFile.SeekToEnd();//先定位到文件尾部
    CString strInsert=strTemp+"/n";
    ioFile.WriteString(strInsert);
    ioFile.Close();
   }
  }
 }
}

//-----------结束---------------------//

2.执行代码:

void CDemoDlg::OnOK()
{
 // TODO: Add extra validation here
 ::CoInitialize(NULL); //初始化COM
     EnumIE();             //枚举浏览器      
     ::CoUninitialize();   //释放COM
 //CDialog::OnOK();
}

 

from:http://blog.csdn.net/fjssharpsword/article/details/6193564

 

posted on   DoubleLi  阅读(1277)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示