VC++,COM操纵IE网页

本文介绍了一个用于解析HTML文档的类CBrowser,该类能够读取HTML表格、枚举表单及编辑表单元素。通过使用COM接口,它能遍历文档中的各种元素并进行操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#ifndef _BROWSER_H
#define _BROWSER_H

class CBrowser
{

public:
                            CBrowser( void );
                            ~CBrowser( void );
public:
            void            ReadHTMLTable( void );
            void            InitInstance( void );
            void            EditForm( void );

protected:
            void            EnumFrame( IHTMLDocument2 *spIHTMLDocument2 );

private:   
            CComQIPtr        < IHTMLDocument2 > m_spDocument2;

            IHTMLFormElement *SelectedForm( IHTMLElementCollection *spElementCollectionForm, long lFormIndex );
            void            EnumForm( IHTMLDocument2 *spIHTMLDocument2 );
            void            PutElement( IHTMLFormElement *spFormElement, long lIndex, const char* pszBuffer );

            void            EnumTable( IHTMLDocument2 *spIHTMLDocument2 );
            void            SelectedRow(IHTMLElementCollection *spElementCollectionTable, long lRowIndex);
            void            SelectedTable( IHTMLElementCollection *spTableRow, long lTableIndex );
};

void CBrowser::ReadHTMLTable( void )
{
    EnumTable( m_spDocument2 );
}

void CBrowser::EnumTable( IHTMLDocument2 *spIHTMLDocument2 )
{
    if( !spIHTMLDocument2 ){ return; }
    EnumFrame( spIHTMLDocument2 );

    HRESULT hr;
    long lRow = 0;
    long lElementCursor = 0;
    long lTableRow = 0;

    CComBSTR bstrTitle;
    spIHTMLDocument2->get_title( &bstrTitle );

    USES_CONVERSION;
    cout << OLE2CT( bstrTitle ) << endl;

    CComQIPtr< IHTMLElementCollection > spElementCollectionTable;
    hr = spIHTMLDocument2->get_all( &spElementCollectionTable );
    if( FAILED(hr) ){ return; }

    long lAllElementCount = 0;
    hr = spElementCollectionTable->get_length( &lAllElementCount );
    for( int lIndex=0; lIndex<lAllElementCount; lIndex++ ) {
        SelectedTable( spElementCollectionTable, lIndex);
    }
}


void CBrowser::SelectedTable( IHTMLElementCollection *spElementCollectionTable, long lTableIndex )
{
    HRESULT hr;

    CComPtr< IDispatch > spDisp;
    hr = spElementCollectionTable->item( CComVariant(lTableIndex),CComVariant(), &spDisp ); 
    if( FAILED(hr) ){ return; }

    CComQIPtr< IHTMLTable > spTable = spDisp;
    if( !spTable ){ return; }

    CComPtr< IHTMLElementCollection > spTableRow;
    hr = spTable->get_rows( &spTableRow );
    if( !spTableRow ){ return; }

    long lRowCount = 0;
    spTableRow->get_length( &lRowCount );

    for( long lIndex=0; lIndex<lRowCount; lIndex++)
    {
        SelectedRow( spTableRow, lIndex );   
    }
       
}

void CBrowser::SelectedRow( IHTMLElementCollection *spTableRow, long lRowIndex )
{

    long lRowCount = 0;
    spTableRow->get_length( &lRowCount );
//    cout << "Row Count: " << lRowCount << endl;
    if( lRowIndex < 0 || lRowIndex > lRowCount - 1) { return; }

    HRESULT hr;
    VARIANT vIndexRow;
    vIndexRow.vt = VT_UINT;
    vIndexRow.lVal = lRowIndex;
    VARIANT var0 = { 0 };
    CComPtr< IDispatch > spDisp;
   
    if( SUCCEEDED( hr = spTableRow->item( vIndexRow, var0, &spDisp ) ))
    {
        CComPtr<IHTMLElement> spVerboseElement;
        if( SUCCEEDED( hr = spDisp->QueryInterface( IID_IHTMLElement, (LPVOID*)&spVerboseElement) ))
        {
            BSTR bstrTag;
            long lIndex;
            CComPtr< IHTMLElement > spRow = spVerboseElement;
            spVerboseElement->get_innerText( &bstrTag );
            spVerboseElement->get_sourceIndex( &lIndex );
           
            CString csTemp(bstrTag);
            cout << (LPSTR)(LPCTSTR)csTemp << endl;
        }
    }
}


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

    long lCount = 0;
    spShellWin->get_Count( &lCount );
    cout << lCount << endl;
    if( 0 == lCount ){ return; }

    for( long lIndex=0; lIndex<lCount; lIndex++ )
    {
        CComPtr< IDispatch > spDispIE;
        hr = spShellWin->Item( CComVariant(lIndex), &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; }
        m_spDocument2 = spDocument2;
    }
}

void CBrowser::EnumFrame( IHTMLDocument2 *spIHTMLDocument2 )
{
    if( !spIHTMLDocument2 ){ return; }

    HRESULT hr;
    CComPtr< IHTMLFramesCollection2 > spFramesCollection2;
    spIHTMLDocument2->get_frames( &spFramesCollection2 );

    long lFrameCount = 0;
    hr = spFramesCollection2->get_length( &lFrameCount );
    if( FAILED( hr ) || 0 == lFrameCount ){ return; }

    for( long lIndex=0; lIndex<lFrameCount; lIndex++)
    {
        CComVariant vDispWin2;
        hr = spFramesCollection2->item( &CComVariant( lIndex ), &vDispWin2 );
        if( FAILED(hr) ){ continue; }

        CComQIPtr< IHTMLWindow2 > spWin2 = vDispWin2.pdispVal;
        if( !spWin2 ){ continue; }

        CComPtr< IHTMLDocument2 > spDoc2;
        spWin2->get_document( &spDoc2 );
        EnumForm( spDoc2 );
    }
}

void CBrowser::EnumForm( IHTMLDocument2    *spIHTMLDocument2 )
{
    if( !spIHTMLDocument2 ){ return; }

    EnumFrame( spIHTMLDocument2 );

    HRESULT hr;
    USES_CONVERSION;
    CComQIPtr< IHTMLElementCollection > spElementCollectionForm;
    hr = spIHTMLDocument2->get_forms( &spElementCollectionForm );
    if( FAILED(hr) ){ return; }

    long lFormCount = 0;
    hr = spElementCollectionForm->get_length( &lFormCount );
    if( FAILED(hr) ){ return; }

    SelectedForm( spElementCollectionForm, 0);
}

IHTMLFormElement* CBrowser::SelectedForm( IHTMLElementCollection *spElementCollectionForm, long lFormIndex )
{
        HRESULT hr;
        CComPtr< IDispatch > spDisp;

        hr = spElementCollectionForm->item(CComVariant(0), CComVariant(), &spDisp);
        if( FAILED(hr) ){ return NULL; }

        CComQIPtr< IHTMLFormElement > spFormElement = spDisp;
        long lElemCount = 0;
        hr = spFormElement->get_length( &lElemCount );
        if( FAILED(hr) ){ return NULL; }
       
        PutElement( spFormElement, 9, "miomio");
        return spFormElement;
}

void CBrowser::PutElement( IHTMLFormElement *spFormElement, long lIndex, const char* pszBuffer )
{
    HRESULT hr;
    CComDispatchDriver spInputElement;
    hr = spFormElement->item( CComVariant( lIndex ), CComVariant(), &spInputElement );
    if( FAILED(hr) ){ return; }

    CComVariant vName;
    CComVariant vVal;
    CComVariant vType;

    hr = spInputElement.GetPropertyByName(L"name", &vName);
    if( FAILED(hr) ){ return; }
    hr = spInputElement.GetPropertyByName(L"value", &vVal);
    if( FAILED(hr) ){ return; }
    hr = spInputElement.GetPropertyByName(L"type", &vType);
    if( FAILED(hr) ){ return; }

    const char* pszTempString = pszBuffer;
    cout<< pszTempString << endl;

    CComVariant vTestString( pszTempString );
    hr = spInputElement.PutPropertyByName(L"value", &vTestString);
    if( FAILED(hr) ){ return; }
}


void CBrowser::EditForm( void )
{
    EnumForm( m_spDocument2 );
}


CBrowser::~CBrowser( void )
{
}

CBrowser::CBrowser( void )
{
}

#endif //_BROWSER_H
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值