VC修改IE浏览器中,输入框的文本

某些时候我们又需要修改或者获取浏览器中的文本。。。

//显示IE浏览器中表单中对象的名称

BOOL ShowIEFormItemName(LPCWSTR sWinTitle)  {

   _variant_t   v;

  HRESULT   hr;

  VARIANT   id,   index;

  SHDocVw::IShellWindowsPtr spSHWinds(NULL);

  CoInitialize(NULL);

  if(spSHWinds == NULL)

  {

   hr = spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows));

   if(FAILED(hr))

   {

    //MessageBox( "Failed ");

    CoUninitialize();

    return FALSE;

   }

  }

  if(spSHWinds)

  {

   long   n = 0;

   spSHWinds->get_Count(&n);   //得到浏览器的个数

   //遍历每一个浏览器

   for   (long   i   =   0;   i   <   n;   i++)

   {

    //CComPtr <IDispatch>   spDispatch;

    //CComPtr <IDispatch>   spDisp;

    IDispatchPtr spDispatch;

    IDispatchPtr spDisp;

    CComQIPtr <IHTMLElementCollection,&IID_IHTMLElementCollection>   pElementCol;

    CComQIPtr <IHTMLDocument2,   &IID_IHTMLDocument2>   pDoc2;

    v   =   (long)i;

    spDisp   = spSHWinds->Item(&v);

    SHDocVw::IWebBrowser2Ptr   spBrowser(spDisp);       //生成一个IE窗口的智能指针

    if(spBrowser)

    {

     CComBSTR sTitle;

     CComBSTR sURL;

     spBrowser->get_LocationName(&sTitle);

     //spBrowser->get_LocationURL(&sURL);

     if(NULL == _tcsstr(sTitle,sWinTitle))//如果标题中有 sWinTitle 字串,就视为找到_tcsstr

     {

      continue;

     }

     //获取IHTMLDocument2接口

     if(SUCCEEDED(spBrowser->get_Document(&spDispatch)))

      pDoc2 = spDispatch;

     if(pDoc2 != NULL)

     {

      //获取所有元素的集合

      if(SUCCEEDED(pDoc2->get_all(&pElementCol)))

      {

       long   p=0;

       if(SUCCEEDED(pElementCol->get_length(&p)))

       {

        if(p!=0)

        {

         //遍历所有元素

         for(long   j=0;j <p;j++)

         {

          CComPtr<IDispatch> spDispatch2;

          CComQIPtr <IHTMLElement,   &IID_IHTMLElement>   pElement;

          CComPtr <IHTMLInputTextElement>   pInputTextElement;

          V_VT(&id)   =   VT_I4;

          V_I4(&id)   =   j;

          V_VT(&index)   =   VT_I4;

          V_I4(&index)   =   0;

          if(SUCCEEDED(pElementCol-> item(id,index,   &spDispatch2)))             {

           if(SUCCEEDED(spDispatch2-> QueryInterface(IID_IHTMLElement,(void**)&pElement)))

           {

            if(SUCCEEDED(pElement->QueryInterface(IID_IHTMLInputTextElement,(void**)&pInputTextElement)))

            {

             CComBSTR   type;                           //输入框类型

             CComBSTR sValue;

             CComBSTR sName;

             pInputTextElement->get_type(&type);//获取输入框类型

             pInputTextElement->get_value(&sValue);

             pInputTextElement->get_name(&sName);

             //CString   strtype(type);

             //strtype.MakeUpper();

             if(lstrcmpi(type, TEXT( "TEXT"))   ==   0)

             {

              //pInputTextElement-> put_value(strName);//设置文本框的值(用户名)

              pInputTextElement-> put_value(sName);

             }

            }

           }

          }

         }

        }

       }

      }

     }

     break;

    }

   }

  }

  if (spSHWinds)

  {

   spSHWinds.Release();

   spSHWinds = 0;

  }

  CoUninitialize();

  return TRUE;

 }

BOOL SetIEFormItemText(LPCWSTR sWinTitle,LPCWSTR sName,LPCWSTR sText)

 {

  _variant_t   v;

  HRESULT   hr;

  VARIANT   id,   index;

  SHDocVw::IShellWindowsPtr spSHWinds(NULL);

  CoInitialize(NULL);

  if(spSHWinds == NULL)

  {

   hr = spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows));

   if(FAILED(hr))

   {

    //MessageBox( "Failed ");

    CoUninitialize();

    return FALSE;

   }

  }

  if(spSHWinds)

  {

   long   n = 0;

   spSHWinds->get_Count(&n);   //得到浏览器的个数

   //遍历每一个浏览器

   for   (long   i   =   0;   i   <   n;   i++)  

   {

    //CComPtr <IDispatch>   spDispatch;

    //CComPtr <IDispatch>   spDisp;

    IDispatchPtr spDispatch;

    IDispatchPtr spDisp;

    CComQIPtr <IHTMLElementCollection,&IID_IHTMLElementCollection>   pElementCol;

    CComQIPtr <IHTMLDocument2,   &IID_IHTMLDocument2>   pDoc2;

    v   =   (long)i;

    spDisp   = spSHWinds->Item(&v);

    SHDocVw::IWebBrowser2Ptr   spBrowser(spDisp);       //生成一个IE窗口的智能指针

    if(spBrowser)

    {

     CComBSTR sTitle;

     CComBSTR sURL;

     spBrowser->get_LocationName(&sTitle);

     //spBrowser->get_LocationURL(&sURL);

     if(NULL == _tcsstr(sTitle,sWinTitle))//如果标题中有 sWinTitle 字串,就视为找到_tcsstr

     {

      continue;

     }

     //获取IHTMLDocument2接口

     if(SUCCEEDED(spBrowser->get_Document(&spDispatch)))

      pDoc2 = spDispatch;

     if(pDoc2 != NULL)

     {

      //获取所有元素的集合

      if(SUCCEEDED(pDoc2->get_all(&pElementCol)))

      {

       long   p=0;

       if(SUCCEEDED(pElementCol->get_length(&p)))

       {

        if(p!=0)

        {

         //遍历所有元素    

         for(long   j=0;j <p;j++)

         {

          CComPtr<IDispatch> spDispatch2;

          CComQIPtr <IHTMLElement,   &IID_IHTMLElement>   pElement;

          CComPtr <IHTMLInputTextElement>   pInputTextElement;

          V_VT(&id)   =   VT_I4;

          V_I4(&id)   =   j;

          V_VT(&index)   =   VT_I4;

          V_I4(&index)   =   0;

          if(SUCCEEDED(pElementCol-> item(id,index,   &spDispatch2)))             {

           if(SUCCEEDED(spDispatch2-> QueryInterface(IID_IHTMLElement,(void**)&pElement)))  

           {

            if(SUCCEEDED(pElement->QueryInterface(IID_IHTMLInputTextElement,(void**)&pInputTextElement)))  

            {

             CComBSTR sTheName;

             pInputTextElement->get_name(&sTheName);

             if(lstrcmp(sTheName, sName) == 0)  

             {

              CComBSTR sTheText(sText);

              //pInputTextElement-> put_value(strName);//设置文本框的值(用户名)

              pInputTextElement->put_value(sTheText);

              break;

             }

            }

           }

          }

         }

        }

       }

      }

     }

     break;

    }

   } 

  }

  if (spSHWinds)

  {

   spSHWinds.Release();

   spSHWinds = 0;

  }

  CoUninitialize();

  return TRUE;

 }

你可能感兴趣的:(VC修改IE浏览器中,输入框的文本)