如何在c ++ / MFC中使用IHTMLDocument2获得重点IE选项卡

时间:2013-12-23 06:05:06

标签: c++ internet-explorer mfc

我正在开展一个项目,我想在IE中最近使用的标签的文本框中粘贴用户名和密码。我正在使用以下代码,但它正在向我提供IE中当前序列的文档。 `

IShellWindowsPtr m_spSHWinds;
m_spSHWinds.CreateInstance(__uuidof(ShellWindows));
IDispatch *spDisp;
for (long i = 0; i < nCount; i++)
{
   _variant_t va(i, VT_I4);
   hR = m_spSHWinds->Item(va, &spDisp);
   IHTMLWindow2 *pWnd= NULL;
   IWebBrowser2 *spBrowser = NULL;
   hR =spDisp->QueryInterface(IID_IWebBrowser2, (void**)&spBrowser);
   if (spBrowser != NULL)
   {
      CString csString;
      //// 
      BSTR *str = (BSTR*)malloc(_MAX_PATH);
      hR = spBrowser->get_LocationName(str);
      IDispatch *sDis;
      IHTMLDocument2 *spDoc = NULL;
      hR = spBrowser->get_Document((IDispatch**)&sDis);
      hR = sDis->QueryInterface(IID_IHTMLDocument2, (void**)&spDoc);
      if(hR == S_OK)
      {
         BSTR p;
         hR = spDoc->get_URL(&p);
         TRACE("\n%s", p);
         hR = spDoc->get_parentWindow(&pWnd);
         if(hR == S_OK)
         {
            CString code,lang;
            code.Format("var editcount = document.getElementsByTagName('input');"
                        "var count = 0;"
                        "for(var cpt = 0; cpt < editcount.length; cpt++){"
                        "if (editcount[cpt].type == 'text')"
                        "   editcount[cpt].value = \"xxxx\";"
                        "else if(editcount[cpt].type == \"password\")"
                        "   editcount[cpt].value = \"xxx\";"
                        "else if(editcount[cpt].type == \"hidden\")"
                        "   editcount[cpt].value = \"xxx\";"
                        "else if(editcount[cpt].type == \"email\")"
                        "   editcount[cpt].value = \"XXX@gmail.com\";"
                        "}");                               
            lang.Format("javascript");
            VARIANT v;
            hR = pWnd->execScript(code.AllocSysString(),
                                  lang.AllocSysString(), &v);
            if(hR == S_OK)
            {
               TRACE("%s", str);
            }
         }
      }
   }
}

这里我得到序列中所有选项卡的句柄,但我需要最近使用的选项卡。 如果任何人有一些代码或建议,请帮助。 先感谢您。 抱歉英语不好。

0 个答案:

没有答案