我现在能够得到IHTMLElementCollection,查找EDIT,button都能够找到何操作,
但是现在我想访问页面中的图片,也就是采用IHTMLImgElement,但是我怎么能够得到
一个有效的IHTMLImgElement呢?
CComPtr<IDispatch>pDisp;
pobjAllElement->item(COleVariant(strName),COleVariant((long)0),&pDisp);
如上面所示,通过控件的名称,我能够得到有效的控件的指针,但是img里面没有name这个属性
我应该怎么办呢?

解决方案 »

  1.   

    length 和 item
    item的name可以上数字VT_I4,进行枚举
    HRESULT item(
        VARIANT name,
        VARIANT index,
        IDispatch **pdisp
    );
    Parametersname
    [in] VARIANT of type VT_I4 or VT_BSTR that specifies the object or collection to retrieve. If this parameter is an integer, it is the zero-based index of the object. If this parameter is a string, all objects with matching name or id properties are retrieved, and a collection is returned if more than one match is made.
    index
    [in] VARIANT of type VT_I4 that specifies the zero-based index of the object to retrieve when a collection is returned.
    pdisp
    [out, retval] Address of a pointer that receives an IDispatch interface for the object or collection if successful, or NULL otherwise.
      

  2.   

    能够说详细一点吗?我知道能够枚举数字,可是我怎么知道找到的就是我需要的那个img呢?
    或者说,我找到那个img后,如何得到他的url呢?
      

  3.   

    请看,我用这个方法,可是要报非法操作。能够指点一下吗?
    CComPtr<IDispatch> pDisp; 
    long l ;
    CComBSTR bStr;
    VARIANT id;
    pobjAllElement->get_length(&l);
    for (long i =0;i<l;i++)
    {
    V_VT(&id) = VT_I4;
    V_I4(&id) = i;
    pobjAllElement->item(id,COleVariant((long)0),&pDisp); 
    CComPtr<IHTMLImgElement> pElement;
    if(SUCCEEDED(pDisp->QueryInterface(IID_IHTMLImgElement,(void**)&pElement)))
    {
    pElement->get_src(&bStr);
    AfxMessageBox((CString)bStr);
    }
    else
    AfxMessageBox("Img get error!");
    }
      

  4.   

    如果用SUCCEEDED,就会报告Img get error的错误。也就是说,这个函数执行不成功阿。
      

  5.   

    不是每个元素都是<IMG>,当然在所有元素集合里面有时候会查询失败了
      

  6.   

    else
    AfxMessageBox("Img get error!");
    这里去掉就可以
      

  7.   

    还是有问题,在循环中,执行到第二次的时候,这个语句就报告非法操作了.
    pobjAllElement->item(id,COleVariant((long)0),&pDisp);
      

  8.   

    哦,如果是release版本就不会报告非法操作了,谢谢!