VC访问IE浏览器中的某个控件

  HINSTANCE h= ShellExecute(NULL,NULL,"iexplore","http://reg.sxzhibo.com",NULL,SW_SHOWNORMAL);  
  Sleep(1000);      
  if (m_spSHWinds == NULL){
   if (m_spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows)) != S_OK){
    MessageBox("Failed");
    CoUninitialize();  
   }
  }
  VARIANT id, index;
  CComPtr<IDispatch> spDispatch; 
  CComQIPtr<IHTMLDocument2, &IID_IHTMLDocument2> pDoc2;
  CComPtr<IHTMLElement> pElement;
  CComPtr<IHTMLElementCollection> pElementCol;
  CComPtr<IHTMLFormElement> pFormElement;
  CComPtr<IHTMLInputTextElement> pInputElement;
  TCHAR szStr[255];
  _bstr_t bStr;
  if (m_spSHWinds)
  {
   int n = m_spSHWinds->GetCount()-1;  
    _variant_t v = (long)n;
   IDispatchPtr spDisp = m_spSHWinds->Item(v);   
   SHDocVw::IWebBrowser2Ptr spBrowser(spDisp);   //生成一个IE窗口的智能指针
   spDisp.Release();

   bStr = spBrowser->GetLocationName();
   _stprintf(szStr, _T("%s"), (LPCTSTR)bStr);
   spBrowser->get_Document(&spDispatch);
   if (spDispatch!=NULL)
   {
    spDispatch->QueryInterface(IID_IHTMLDocument2, (void**)&pDoc2);
    if ( pDoc2 )
    {     
     pDoc2->get_forms(&pElementCol);  
     spDispatch.Release();
     long p=0;
     SUCCEEDED(pElementCol->get_length(&p));
     for(long i=0;i<=(p-1);i++)
     {
      V_VT(&id) = VT_I4;
      V_I4(&id) = i;
      V_VT(&index) = VT_I4;
      V_I4(&index) = 0;
      SUCCEEDED(pElementCol->item(id,index, &spDispatch));    
      SUCCEEDED(spDispatch->QueryInterface(IID_IHTMLFormElement,(void**)&pFormElement));
      spDispatch.Release();
      long q=0;
      SUCCEEDED(pFormElement->get_length(&q));
      for(long j=0;j<=(q-1);j++)
      {
       V_VT(&id) = VT_I4;
       V_I4(&id) = j;
       V_VT(&index) = VT_I4;
       V_I4(&index) = 0;
       SUCCEEDED(pFormElement->item(id,index, &spDispatch));
       if(SUCCEEDED(spDispatch->QueryInterface(IID_IHTMLInputTextElement,(void**)&pInputElement)))
       {
        spDispatch.Release();
        CComBSTR value;
        CComBSTR type;      
        pInputElement->get_type(&type);
        pInputElement->get_name(&type);
        CString strtype(type);
        strtype.MakeUpper();
        if(m_inputName.Compare(strtype)==0)
        {
         _bstr_t  str;
         CString ctemp;
         m_edtCharacterCode.GetWindowText(ctemp);
         str=ctemp;
         BSTR bstr= SysAllocString(str);         
         pInputElement->put_value(bstr);
         SysFreeString(bstr);
        }
       }
      }
     }
    }
  
   }

  }
  m_spSHWinds.Release();

你可能感兴趣的:(浏览器,IE,null,Forms)