VC自动提交表单(提交按纽不是submit控件)

#include <windows.h>
//引用HTML相关头文件
#include <atlbase.h>
CComModule _Module;    
#include <mshtml.h>  
#include <atlcom.h>
#include <oleacc.h>
#include <comdef.h>

#include <string>
using   namespace   std;

#import <shdocvw.dll>

void PutFormValue(IHTMLDocument2 * pIHTMLDocument2);

BOOL CWebTestDlg::OnInitDialog()
{
    
//加载网站
    m_ctrlWeb.Navigate("http://",NULL,NULL,NULL,NULL);    
    return TRUE;  
}

//向Edit文本框中填充数据
void PutFormValue(IHTMLDocument2 * pIHTMLDocument2)
{   
    if(!pIHTMLDocument2)    
        return;
    
    HRESULT hr;
    CComBSTR bstrTitle;
    
    
//获取页面标题
    pIHTMLDocument2->get_title( &bstrTitle );  
    USES_CONVERSION;
    
    CComQIPtr<IHTMLElementCollection>spElementCollection; 
    hr = pIHTMLDocument2->get_forms( &spElementCollection );
    
     if (FAILED(hr))
    {
        AfxTrace(_T("获取表单的集合 IHTMLElementCollection 错误"));
        return;
    }
    
    long nFormCount=0;
    
    
//获取表单数目
    hr = spElementCollection->get_length( &nFormCount );
    if ( FAILED( hr ) )
    {
        AfxTrace( _T("获取表单数目错误"));
        return;
    }
    
    for(long i=0; i<nFormCount; i++)
    {
        IDispatch *pDisp = NULL; 
//取得第 i 项表单
        hr = spElementCollection->item( CComVariant( i ), CComVariant(), &pDisp );
        if ( FAILED( hr ) )        continue;
        
        CComQIPtr <IHTMLFormElement> spFormElement = pDisp;
        pDisp->Release();
        
        long nElemCount=0;     
//取得表单中 域的数目
        hr = spFormElement->get_length( &nElemCount );
        if ( FAILED( hr ) )        continue;   
        
        CString strName;
        CString strVal;
        for(long j=0; j<nElemCount; j++)
        {
            CComDispatchDriver spInputElement; 
//取得第 j 项表单域     
            hr = spFormElement->item( CComVariant( j ), CComVariant(), &spInputElement );
            if ( FAILED( hr ) )    continue;
            
            CComVariant vName,vVal,vType;    
//取得表单域的 名,值,类型
            hr = spInputElement.GetPropertyByName( L"name", &vName );
            if( FAILED( hr ) )  continue;
            hr = spInputElement.GetPropertyByName( L"value", &vVal );
            if( FAILED( hr ) )  continue;
            hr = spInputElement.GetPropertyByName( L"type", &vType );
            if( FAILED( hr ) )  continue;
            
            LPCTSTR lpName = vName.bstrVal?
                OLE2CT( vName.bstrVal ) : _T("NULL"); 
//未知域名
            LPCTSTR lpVal  = vVal.bstrVal?
                OLE2CT( vVal.bstrVal  ) : _T("NULL");    
//空值,未输入
            LPCTSTR lpType = vType.bstrVal?
                OLE2CT( vType.bstrVal ) : _T("NULL"); 
//未知类型
            
            strName =  lpName;
            strName.TrimLeft();
            strName.TrimRight();
            AfxTrace(_T("%s/r/n"),strName);
            
                        
//向用户名文本框填值
            if (strName == "UserName")
            {        
                TCHAR szText[32= "123456";
                CComVariant vMyVal = (LPCTSTR)(szText);
                spInputElement.PutPropertyByName( L"value",&vMyVal);
            }
            
            
//向密码文本框填值
            if (strName == "PassWord")
            {
                TCHAR szText[32= "789";
                CComVariant vMyVal = (LPCTSTR)(szText);
                spInputElement.PutPropertyByName( L"value",&vMyVal);
            }
        }  
//for(long j=0; j<nElemCount; j++)
    }  
//for(long i=0; i<nFormCount; i++)
}

//(1)测试向文本框中填充数据
void CWebTestDlg::OnOK() 
{
    CComPtr <IDispatch> spDispDoc;
    spDispDoc = m_ctrlWeb.GetDocument();
    
    CComQIPtr<IHTMLDocument2> spDocument2 = spDispDoc;
    if (!spDocument2)    
        return;    
    
    PutFormValue(spDocument2);
}

//测试触发按纽的Click事件
//该按纽为超级链接,不是submit控件
void CWebTestDlg::OnButton1() 
{
    IHTMLElementCollection *objAllElement=NULL;
    IHTMLDocument2 *objDocument=NULL;
    CString strUrl,strTemp;
    strUrl=m_ctrlWeb.GetLocationURL();
//得到当前网页的URL
    if(strUrl.IsEmpty())
        return;
    objDocument=(IHTMLDocument2 *)m_ctrlWeb.GetDocument(); 
//由控件得到IHTMLDocument2接口指针
    objDocument->get_all(&objAllElement); 
//得到网页所有元素的集合
    
//由于所有页面下载完后都会执行这个函数,所以必须根据URL判断消息来源网页
    
//if(strUrl=="判断网址" ) 
    {
        CComPtr<IDispatch>pDisp;
        long   lcount   =   0;   
        objAllElement->get_length(&lcount);   
        for(int   i=0;i<lcount;i++)   
        {   
            _variant_t   index;   
            index.vt=VT_I4;   
            index.intVal=i;   
            IDispatchPtr   disp;   
            objAllElement->item(index,index,&disp);   
            if(disp==NULL)   
                continue;  
            else   
            {   
                CComQIPtr<IHTMLElement>   pInput(disp);   
                if(pInput)   
                {   
                    BSTR   bstrtype; 
                    pInput->get_tagName(&bstrtype);
                    
//pInput->toString(&bstrtype);
                    
//pInput->get_type(&bstrtype);   
                    
//printf(_bstr_t(bstrtype));
                    _bstr_t bInner = bstrtype;
                    TCHAR *szIner =  bInner;
                    
                    if(_tcscmp(szIner, _T("A"))==0)   
                    {
                        CComVariant Attribute;
                        pInput->getAttribute(_bstr_t(_T("href")),2,&Attribute);
                        CString  strAttrib = (LPCTSTR)Attribute.bstrVal;
                        if (strAttrib == _T("#"))
                        {
                            AfxTrace(_T("%s/r/n"),(LPCTSTR)Attribute.bstrVal);
                            pInput->get_innerHTML(&bstrtype);
                            CString strLable = (LPCTSTR)bstrtype;
                            bInner = bstrtype;
                            TCHAR *szIner = bInner; 

                                                        
//找到"登陆"这个超级链接
                                                        
//触发执行Click事件   
                            if (_tcscmp(szIner, _T("登 录")) == 0)
                            {
                                
//AfxTrace(_T("%s/r/n"),szIner);
                                pInput->click();                
                            }
                        }
                    }
                    SysFreeString(bstrtype);       
                }          
            }      
        }   
        objAllElement->Release();   
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值