16,472
社区成员
发帖
与我相关
我的任务
分享
想实现的功能:打开页面,然后输用户名和密码提交表单。
用下面的代码可以把指定的网站加载到控件中,
但是查找表单个数时,返回0,
//nFormCount值为0。。。。。
hr = spElementCollection->get_length( &nFormCount );
弄不清楚什么原因。
// WebBrowseDlg.h : header file
#include "Atlbase.h " //查找Html中控件
#include <mshtml.h> //查找Html中控件
CWebBrowser2 m_ctrlWeb;
// WebBrowseDlg.cpp : implementation file
BOOL CWebBrowseDlg::OnInitDialog()
{
//移动网站
m_ctrlWeb.Navigate("http://221.178.143.198:7001/browser_index.html",NULL,NULL,NULL,NULL);
}
//自动提交表单
void CWebBrowseDlg::PutFormValue(IHTMLDocument2 * pIHTMLDocument2)
{
if(!pIHTMLDocument2)
return;
HRESULT hr;
CComBSTR bstrTitle;
//获取加载页面的标题
pIHTMLDocument2->get_title( &bstrTitle );
USES_CONVERSION;
CComQIPtr<IHTMLElementCollection>spElementCollection;
hr = pIHTMLDocument2->get_forms( &spElementCollection );
if (FAILED(hr))
{
AfxTrace(_T("获取表单的集合 IHTMLElementCollection 错误"));
return;
}
long nFormCount=0;
//nFormCount返回值为 0 ??????
//获取表单数目
hr = spElementCollection->get_length( &nFormCount );
if ( FAILED( hr ) )
{
AfxTrace( _T("获取表单数目错误"));
return;
}
}
//调用
void CWebBrowseDlg::OnOK()
{
//创建IHTMLDocument2类型的对象
CComPtr <IDispatch> spDispDoc;
spDispDoc = m_ctrlWeb.GetDocument();
CComQIPtr<IHTMLDocument2> spDocument2 = spDispDoc;
if (!spDocument2)
return;
PutFormValue(spDocument2);
CDialog::OnOK();
}