求教取得基于frame结构的网页中控件的IHTMLDocument2接口并对控件进行某些操作(比如取文本域中的文本值)
信阳毛尖 2010-08-26 11:14:37 如题
对于form结构的,大致代码如下:
void CAboutIHTMLDocument2Dlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
HWND HWin = NULL;
USES_CONVERSION;
HWin=::FindWindowEx( NULL, NULL, "IEFrame", "2010年河南省高校门户网站评选 - Microsoft Internet Explorer"); //
if(HWin==NULL)
{
DWORD err=::GetLastError();
::AfxMessageBox("找不到指定窗口");
}
CoInitialize( NULL );
HINSTANCE hInst = ::LoadLibrary(_T("OLEACC.DLL"));
if ( hInst != NULL )
{
if( qqMailWin != NULL )
{
HWND hWndChild = NULL;
::EnumChildWindows(qqMailWin,EnumChildProc, (LPARAM)&hWndChild );
if ( hWndChild )
{
CComPtr<IHTMLDocument2> spDoc;
LRESULT lRes;
UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );
::SendMessageTimeout( hWndChild, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );
LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, (LPCSTR)"ObjectFromLresult" );
if( pfObjectFromLresult !=NULL )
{
HRESULT hr;
hr = pfObjectFromLresult( lRes,IID_IHTMLDocument2,0,(void**)&spDoc );
if ( SUCCEEDED(hr) )
{
// 获取各个控件
CComQIPtr < IHTMLElementCollection > spElementCollection;
hr = spDoc->get_forms(&spElementCollection);
long nFormCount=0; //取得表单数目
hr = spElementCollection->get_length( &nFormCount );
if (nFormCount==0)
::AfxMessageBox("表单数目为0");
for( long i=0;i<nFormCount;i++ )
{
IDispatch *pDisp = NULL; //取得第 i 项表单
hr = spElementCollection->item( CComVariant( i ), CComVariant(), &pDisp );
if ( FAILED( hr ) ) continue;
CComQIPtr < IHTMLFormElement > spFormElement = pDisp;
pDisp->Release();
long nElemCount=0; //取得表单中 域 的数目
hr = spFormElement->get_length( &nElemCount );
if ( FAILED( hr ) ) continue;
for(long j=0; j <nElemCount; j++)
{
CComDispatchDriver spInputElement; //取得第 j 项表单域
hr = spFormElement->item( CComVariant( j ), CComVariant(), &spInputElement );
if ( FAILED( hr ) ) continue;
CComVariant vName,vVal,vType; //取得表单域的 名,值,类型
hr = spInputElement.GetPropertyByName( L"name", &vName );
if( FAILED( hr ) ) continue;
hr = spInputElement.GetPropertyByName( L"value", &vVal );
if( FAILED( hr ) ) continue;
hr = spInputElement.GetPropertyByName( L"type", &vType );
if( FAILED( hr ) ) continue;
LPCTSTR strTextType= _T("text");
LPCTSTR strElementType = vType.bstrVal ? OLE2CT( vType.bstrVal ) : _T("NULL");
LPCTSTR strElementName = vName.bstrVal ? OLE2CT( vName.bstrVal ) : _T("NULL");
LPCTSTR strValue = vVal.bstrVal ? OLE2CT(vVal.bstrVal) : _T("NULL");
if( StrCmp( strTextType,strElementType) == 0)
{
if( StrCmp( strElementName, _T("UserName")))
{
::AfxMessageBox("找到指定输入框!!");
CComBSTR str = NULL;
CComQIPtr < IHTMLInputTextElement > spGetText(spInputElement); hr = spGetText->get_value(&str);
if( SUCCEEDED(hr) )
{
if( str.Length() != 0 )
{
CComBSTR msg=_T("输入框类容是:");
msg += str;
CString s( str );
::AfxMessageBox( s );
}
}
}
else
::AfxMessageBox("没有找到指定输入框!!");
}
}//for j
}//for i
}
}
}
}
::BringWindowToTop(qqMailWin);
::FreeLibrary( hInst );
}
CoUninitialize();
OnOK();
}
但是基于frame结构的我探索了很久,没有实现功能,
大致需要把上文红色字体的两句改为:
//CComQIPtr < IHTMLElementCollection > spElementCollection; 把原来的这一句换成下一句
CComQIPtr < IHTMLFramesCollection2 > spElementCollection;
//hr = spDoc->get_forms(&spElementCollection); 把原来的这一句换成下一句
hr = spDoc->get_frames(&spElementCollection);
接下来的..........
望高手们教我,谢谢