IE编程问题:程序在IE6中可以通过,但是在IE7中无法运行,是否与IE7采用的是选项卡式的子窗口有关?
/*函数名:GetDocInterface
  参数:hWnd,WebBrowser控件的窗口句柄
  功能:通过WM_HTML_GETOBJECT取得控件的IHTMLDocument2接口
*/
IHTMLDocument2* GetDocInterface(HWND hWnd) 
{
/*
CString hwnd;
hwnd.Format("%d",hWnd);
::AfxMessageBox(hwnd);
*/ // 我们需要显示地装载OLEACC.DLL,这样我们才知道有没有安装MSAA
HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL") );
IHTMLDocument2* pDoc2=NULL;
if ( hInst != NULL ){
if ( hWnd != NULL ){
CComPtr<IHTMLDocument> spDoc=NULL;
LRESULT lRes; UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );
::SendMessageTimeout( hWnd, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes ); LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, _T("ObjectFromLresult") );
if ( pfObjectFromLresult != NULL ){
HRESULT hr;
hr=pfObjectFromLresult(lRes,IID_IHTMLDocument,0,(void**)&spDoc);
if ( SUCCEEDED(hr) ){
CComPtr<IDispatch> spDisp;
CComQIPtr<IHTMLWindow2> spWin;
spDoc->get_Script( &spDisp );
spWin = spDisp;
spWin->get_document( &pDoc2 );
}
}
}
::FreeLibrary(hInst);

else{//如果没有安装MSAA
AfxMessageBox(_T("请您安装Microsoft Active Accessibility"));
}
return pDoc2;
} BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam)
{
TCHAR szClassName[100];

::GetClassName(hwnd,szClassName,sizeof(szClassName)); if (_tcscmp(szClassName,_T("Internet Explorer_Server"))==0 ) //IE 6.0
{
/*::AfxMessageBox(szClassName);
CString temp;
temp.Format("%d",hwnd);
::AfxMessageBox(temp);*/
*(HWND*)lParam=hwnd;
//return FALSE; // 找到第一个 IE 控件的子窗口就停止
}
//else
return TRUE; // 继续枚举子窗口
};/////////////////////////////////////////////////////////////////////////////-------下面是打开指定网页,并自动填写内容代码---------------// //打开IE,并进入网页
m_pWebBrowser2.CreateInstance(CLSID_InternetExplorer);
HRESULT hr;
hr = m_pWebBrowser2->put_StatusBar(VARIANT_TRUE);
hr = m_pWebBrowser2->put_ToolBar(VARIANT_TRUE);
hr = m_pWebBrowser2->put_MenuBar(VARIANT_TRUE);
hr = m_pWebBrowser2->put_Visible(VARIANT_TRUE);

CString m_strFileToFind;
m_strFileToFind=_T("http://mail.163.com");
COleVariant vaURL((LPCTSTR) m_strFileToFind);
m_pWebBrowser2->Navigate2(
&vaURL,COleVariant((long) 0,VT_I4),
COleVariant((LPCTSTR)NULL,VT_BSTR),
COleSafeArray(),
COleVariant((LPCTSTR)NULL,VT_BSTR)
);

//short i=m_pWebBrowser2->GetBusy();
//TRACE0( "Integer = %s\n, i "); HWND hwnd=(HWND)m_pWebBrowser2->GetHWND();
HWND hWndChild;
::EnumChildWindows(hwnd,EnumChildProc,(LPARAM)&hWndChild);
m_pHtmlDocument2=GetDocInterface(hWndChild); CComPtr<IHTMLElementCollection> pElementCollection;
HRESULT hr;
long lElemCount=0; //表单域的总数目
hr=m_pHtmlDocument2->get_all(&pElementCollection);
pElementCollection->get_length(&lElemCount);
CString strName;
for(long i=0;i<lElemCount;i++)
{
VARIANT vIndex,vName;
vName.vt=vIndex.vt=VT_I4;
vName.lVal=vIndex.lVal=i;
CComPtr<IDispatch> pDisp;
hr=pElementCollection->item(vIndex,vName,&pDisp);

CComPtr<IHTMLInputTextElement> pInputTextElement;
hr=pDisp->QueryInterface(IID_IHTMLInputTextElement,(void**)&pInputTextElement);
if(SUCCEEDED(hr))
{

BSTR bstrValue;
pInputTextElement->get_name(&bstrValue);
CString strValue=bstrValue;
if(strValue==_T("username"))
{
pInputTextElement->put_value(_bstr_t("AAAAAA"));
}
if(strValue==_T("password"))
{
pInputTextElement->put_value(_bstr_t("BBBBBB"));
}
SysFreeString(bstrValue);
} }
....../////////////////////////////////////////////////////////////////////上述代码在IE6中调试实现,在
IE7中无法通过,是否因为IE7采用的是选项卡式的子窗口,用上述的EnumChildProc()方法无法得到子窗口的句柄?
请问如何改正才能才能实现呢?

解决方案 »

  1.   

    你要用spy++看看子父窗口的关系
    HWND hwnd=(HWND)m_pWebBrowser2->GetHWND();//这里得到的是主进程窗口的句柄,而并不一定是m_pWebBrowser2的父窗口句柄
    HWND hWndChild;
    ::EnumChildWindows(hwnd,EnumChildProc,(LPARAM)&hWndChild);//然后你调用这个的时候,就只是遍历了主进程窗口的子窗口而已,并不一定是遍历跟网页窗口是同一级的如,打个比方:IE主进程窗口
    --主进程窗口的子窗口
    ----主进程窗口的子窗口的子窗口(网页窗口)而你遍历的只是主进程窗口的子窗口这一层的,所以就出错了当然,你也可以用如下方法得到IE窗口
    http://dev.21tx.com/2006/07/21/12836.html