[获取IE内容]获取IE浏览器的网页内容

  代码在C++ builder测试过。

  1. 获取IE软件的句柄

          如果想获取其他软件的webbrowser,可以在这部分先获取其他软件的句柄。

HWND hWnd= FindWindow("IEFrame", NULL);

  2. 获取IE软件浏览页的句柄

         注意,这个可以用到获取其他软件内嵌的webbrowser句柄 。

HWND BrowserWnd=FindWithClassName( hWnd , _T("Internet Explorer_Server"));

   

HWND FindWithClassName(HWND ParentWnd,TCHAR* FindClassName)
{
	HWND hChild = ::GetWindow(ParentWnd, GW_CHILD);

	for(; hChild!=NULL ; hChild=::GetWindow(hChild,GW_HWNDNEXT))
	{
		TCHAR ClassName[100]={0};
		::GetClassName(hChild,ClassName,sizeof(ClassName)/sizeof(TCHAR));

		if (_tcscmp(ClassName,FindClassName)==0)
			return hChild;
  
		HWND FindWnd=FindWithClassName(hChild,FindClassName);
		if (FindWnd)
		return FindWnd;
	}
 	return NULL;
}

 

     3. 向IE发送WM_HTML_GETOBJECT消息获得对象

   可能需要头文件:

#include <oleacc.h>
#pragma link "oleacc.lib"

 

   代码:

  LRESULT lRes;
  UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );
  ::SendMessageTimeout( hWnd, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );
  HRESULT hr;
  CComPtr<IHTMLDocument2>spDoc;
  HRESULT iResult=ObjectFromLresult(lRes,IID_IHTMLDocument2,0,(void**)&spDoc);

  4. 获得IHTMLDocument2对象,对其进行操作。

        //修改页面外观
      spDoc->put_bgColor( CComVariant("red") );

  

        //运行jscipt
        IHTMLWindow2* sParentWindow = NULL;
        spDoc->get_parentWindow(&sParentWindow);
        if(sParentWindow!=NULL)
        {
            try
            {
                VARIANT vRet;
                WideString str = "alert('Test');";
                sParentWindow->execScript(str.c_bstr(), L"JScript", &vRet);
            }catch(...)
            {
                ShowMessage("error sParentWindow");
            }
        }

  

        //获得页面URL
        BSTR bstrV = ::SysAllocString(L"");
        spDoc->get_URL(&bstrV);
        ShowMessage(bstrV);
        SysFreeString(bstrV);

  

        //获得页面内容
        /* 获得type为“text” "password"的文本内容
           获得name为“BTEST”的文本内容
            <html>
                <head><title>TEST</title></head>
                <body>
                    <form id="form1" name="form1" method="post" action="">
                        <input type="text" id="username">
                        <p/>
                        <input type="password" id="psw">
                        <input name="BTEST" type="text" id="dot">
                        <input type="submit" id="ok">
                    </form>
                </body>
            </html>
        */
        bool    bFinded = false;
        WCHAR   wzEmailAddr[100] = {0};
        WCHAR   wzEmailPsw[100] = {0};
        WCHAR   wzTest[100] = {0};
        CComPtr<IHTMLElementCollection> pAllColl;
        HRESULT ihr_getAll = spDoc->get_all(&pAllColl);
        if( SUCCEEDED(ihr_getAll) && pAllColl != NULL )
        {
            LONG    length = 0;

            ihr_getAll = pAllColl->get_length(&length);
            ShowMessage(pAllColl->toString(""));
            if (S_OK == ihr_getAll)
            {
                for(int i = 0; i < length; i++)
                {
                    VARIANT vIndex,vName;
                    vName.vt = vIndex.vt = VT_I4;
                    vName.lVal = vIndex.lVal = i;
  
                    CComPtr<IDispatch> pDisp;
                    ihr_getAll = pAllColl->item(vName,vIndex,&pDisp);
                    if (S_OK != ihr_getAll || pDisp == NULL)
                    {
                        continue;
                    }
                    CComPtr<IHTMLInputTextElement> pElement;
                    ihr_getAll = pDisp->QueryInterface(IID_IHTMLInputTextElement,(void**)&pElement);

                    if (S_OK != ihr_getAll || pElement == NULL)
                    {
                        continue;
                    }
                    CComBSTR type;
                    hr = pElement->get_type(&type);
                    if(SUCCEEDED(hr))
                    {
                        if(type == "password")
                        {
                            CComBSTR pwd;
                            pElement->get_value(&pwd);
                            lstrcpyW(wzEmailPsw, (PWCHAR)pwd);
                            bFinded = true;
                        }
                        else if (type == "text")
                        {
                            CComBSTR eml;
                            pElement->get_value(&eml);

                            VARIANT AttrIndex;
                            AttrIndex.vt=VT_I4;
                            AttrIndex.lVal=4;


                            lstrcpyW(wzEmailAddr, (PWCHAR)eml);
                            bFinded = true;
                        }
                    }
                    CComBSTR name;
                    hr = pElement->get_name(&name);
                    if(SUCCEEDED(hr))
                    {
                        if(name == "BTEST")
                        {
                            CComBSTR value;
                            pElement->get_value(&value);
                            lstrcpyW(wzTest, (PWCHAR)value);
                            bFinded = true;
                        }
                    }

                }
            }
        }
        else ShowMessage(" get_all error ");

        ShowMessage(wzEmailAddr);
        ShowMessage(wzEmailPsw);
        ShowMessage(wzTest);

  

 

posted @ 2015-05-11 15:20  SKeyC27  阅读(634)  评论(0编辑  收藏  举报