一、首先新建一个MFC对话框程序,添加webbrowser控件,其过程是在工具箱中右键,点击选择项按钮
然后在COM组件中选择Microsoft Web browser控件即可
这样工具箱中就有了webbrowser 控件,将其拖放在对话框程序中即可,并生成了explorer1.h和explorer1.cpp文件
二、现在开始对webbrowser控件进行操作,添加一个按钮进行事件触发
void CVS_ONLINE_PAGEDlg::OnBnClickedTest()
{
//测试使用函数
m_ie.Navigate2(COleVariant(L"http://www.oschina.net"),NULL,NULL,NULL,NULL);
//get_Document函数必须在网页加载完成之后使用,也就是说不能直接在Navigate2之后使用!!!!
}
这样我们就可以浏览一个网页了,但是对于网页中内容进行操作,则必须在网页加载完成之后进行,不能调用Navigate2之后,直接进行,这样的操作是无效的!
三、为webbrowser控件添加事件触发处理程序,比如文档加载完成之后,进行某些操作
有两种方式,一是利用向导来进行的
void CExplorer1::DocumentCompleteExplorer1(LPDISPATCH pIDisp, VARIANT* URL)
{
IWebBrowser2* pWb;
IHTMLDocument2* pDoc=NULL;
IDispatch* pDis;
HRESULT hr=pIDisp->QueryInterface(IID_IWebBrowser2,(void**)&pWb);
hr=pWb->get_Document(&pDis);
hr=pDis->QueryInterface(IID_IHTMLDocument2,(void**)&pDoc);
if(SUCCEEDED(hr)){
long lCount=0;
IHTMLElementCollection*pCollection=NULL;
pDoc->get_all(&pCollection);
pCollection->get_length(&lCount);
for(long i=0;i<lCount;i++){
IHTMLElement*pElement=NULL;
VARIANT vItem;
vItem.vt=VT_I4;
vItem.intVal=i;
pCollection->item(vItem,CComVariant(),&pIDisp);
hr=pIDisp->QueryInterface(IID_IHTMLElement,(void**)&pElement);
if(SUCCEEDED(hr)){
//设置编辑框内容
BSTR bstrId;
pElement->get_id(&bstrId);
if(bstrId!=NULL){
if(wcsstr(bstrId,L"q_top")){
pElement->put_innerText(L"DSADA");
MessageBox(L"找到编辑框了",L"提示",0);
}
}
//模拟点击按钮
pElement->get_id(&bstrId);
if(bstrId!=NULL){
if(wcscmp(bstrId,L"button")==0){
MessageBox(L"找到按钮了",L"提示",0);
pElement->click();
}
}
pElement->Release();
}
pIDisp->Release();
}
pIDisp->Release();
}
}
或者也可以进行手动操作,只是该方法是在Dlg中进行的
3.1 首先在Dlg.h文件中添加声明
DECLARE_EVENTSINK_MAP()
3.2 在Dlg.cpp文件中添加EVENT宏
BEGIN_EVENTSINK_MAP(CVS_ONLINE_PAGEDlg, CDialog)
{{
AFX_EVENTSINK_MAP(CMFCHtmlDlg)
ON_EVENT(CVS_ONLINE_PAGEDlg,
IDC_EXPLORER1, 259 /* DocumentComplete */,
OnDocumentCompleteExplorerMain, VTS_DISPATCH VTS_PVARIANT)
}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
3.3 添加事件处理函数
void CVS_ONLINE_PAGEDlg::OnDocumentCompleteExplorerMain(LPDISPATCH pDisp, VARIANT FAR* URL) {
// TODO: Add your control notification handler code here
HRESULT hr=0;
IDispatch* pIDisp = NULL;
pIDisp = m_ie.get_Document();
IHTMLDocument2* pDoc=NULL;
hr=pIDisp->QueryInterface(IID_IHTMLDocument2,(void**)&pDoc);
if(SUCCEEDED(hr))
{
long lCount=0;
IHTMLElementCollection*pCollection=NULL;
pDoc->get_all(&pCollection);
pCollection->get_length(&lCount);
for(long i=0;i<lCount;i++)
{
IHTMLElement*pElement=NULL;
VARIANT vItem;
vItem.vt=VT_I4;
vItem.intVal=i;
pCollection->item(vItem,CComVariant(),&pIDisp);
hr=pIDisp->QueryInterface(IID_IHTMLElement,(void**)&pElement);
if(SUCCEEDED(hr))
{
//设置编辑框内容
BSTR bstrId;
pElement->get_id(&bstrId);
if(bstrId!=NULL)
{
if(wcsstr(bstrId,L"q_top"))
{
pElement->put_innerText(L"DSADA");
MessageBox(L"找到编辑框了",L"提示",0);
}
}
//模拟点击按钮
pElement->get_id(&bstrId);
if(bstrId!=NULL)
{
if(wcscmp(bstrId,L"button")==0)
{
MessageBox(L"找到按钮了",L"提示",0);
pElement->click();
}
}
pElement->Release();
}
pIDisp->Release();
}
pIDisp->Release();
}
}