Chinaunix首页 | 论坛 | 博客
  • 博客访问: 562226
  • 博文数量: 97
  • 博客积分: 5090
  • 博客等级: 大校
  • 技术积分: 969
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-01 14:56
文章分类

全部博文(97)

文章存档

2011年(1)

2009年(1)

2008年(14)

2007年(37)

2006年(44)

我的朋友

分类: WINDOWS

2009-08-31 16:40:32

一、摘要

由于本人在做自动注册时遇到的问题 经过实践今得到了解决。这种方式只针对某个论坛而已(其他的论坛,
我想也可以作为参考).现将过程描述如下.

二、网页文档对象说明

IHTMLDocument2          表示网页文档
IHTMLElementCollection  表示表单集合
IHTMLElement            表示单个的页面元素

IHTMLAnchorElement 表示页面链接元素
IHTMLImgElement    表示页面图像元素
IHTMLElement 表示页面脚本元素
IHTMLFormElement   表示页面表单元素


三、程序实现

<1>在工程中添加webbrowser控件,并给该控件加上OnDocumentCompleteExplorer1这个事件(它表示当页面完全加载完毕,会执行).

   主要的工作都是在这个页面里完成的.

<2>得到IHTMLDocument2 这个接口指针

   CComQIPtr   pHTMLDocument2(m_ie.GetDocument());

<3>得到IHTMLElementCollection的接口

   HRESULT hr;
   IHTMLElementCollection* pColl = NULL;
   hr = pHTMLDocument2->get_all( &pColl );

<4>得到表单集合IHTMLElementCollection的总数目
   
   LONG celem;
   pColl->get_length(&celem);

<5>枚举元素.IHTMLElementCollection::item方法为你提供一个你能调用 QueryInterface ,请求IID_IHTMLElement的IDispatch指针。
   这将会给你一个你能用来为个别的元素获得或设置信息的IHTMLElement接口指针.
   
    VARIANT varIndex;
    varIndex.vt = VT_UINT;
    varIndex.lVal = i;
    VARIANT var2;
    VariantInit( &var2 );
    IDispatch* pDisp; 

    hr = pColl->item( varIndex, var2, &pDisp );//Get an element

<6>得到 IHTMLElement接口

   IHTMLElement* pElem;
   hr = pDisp->QueryInterface(  IID_IHTMLElement,(void **)&pElem );

<7>得到标记.如中的img

   BSTR bstr;
   hr = pElem->get_tagName(&bstr);
   CString strTag; 
   strTag = bstr;
  
<8>得到 IHTMLImgElement接口
   
   if(strTag=="IMG")
   {
 IHTMLImgElement*   pImage; 
        hr = pDisp->QueryInterface( IID_IHTMLImgElement,(void **)&pImage);

        if ( hr == S_OK)
 {
            CString strTag1;
            pImage->get_src (&bstr);//得到中src的值
     strTag1 = bstr;
       }
  } 

<9>得到 IHTMLElement2接口

    IHTMLElement*    pelmBody      =   NULL; 
    IHTMLElement2*   pelmBodyTwo   =   NULL; 
    pHTMLDocument2->get_body(&pelmBody);
    pelmBody->QueryInterface(IID_IHTMLElement2,   (void**)&pelmBodyTwo);

<10> 为非文本对象建立控制排版集合
    IDispatch*   pdispCtrlRange   =   NULL;   
    pelmBodyTwo->createControlRange(&pdispCtrlRange);  

<11>得到IHTMLControlRange接口
    IHTMLControlRange* pCtrlRange = NULL;
    pdispCtrlRange->QueryInterface(IID_IHTMLControlRange, (void**) &pCtrlRange) ;
    
   
<12>得到 IHTMLControlElement接口
    IHTMLControlElement* pCtrlElement = NULL;    
    pDisp->QueryInterface(IID_IHTMLControlElement, (void**) &pCtrlElement) ;   

<13> 将验证码图片copy到剪帖板
      BSTR bstrCommand = SysAllocString(L"Copy");
      VARIANT_BOOL vbReturn;
      VARIANT vEmpty;
      VariantInit(&vEmpty);

      hr = pCtrlRange->add(pCtrlElement);
      hr = pCtrlRange->execCommand(bstrCommand, VARIANT_FALSE, vEmpty, &vbReturn);
                                           
<14>最后对剪帖板操作 就可以了 
阅读(3289) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~