您的位置:首页 > 其它

有关于mfc webbrowser插件的使用

2016-05-25 09:35 615 查看
最近写的东西中常常需要嵌入一些浏览器,微软提供了一个比较好的接口,可以在MFC写的程序中嵌入一个简易的浏览器,是以ActiveX插件的形式提供的接口,使用起来也比较的方便,这里我就简单记录下这个插件的使用

这里我用vc6为例吧,我的机器太烂,跑不动vs这种巨人级别的软件。

1.首先创建一个对话框,在对话框上右击插入ActiveX的插件



2.可以看到微软提供了很多ActiveX的插件供我们选择。



3.选中浏览器插件确定后就能看到一个浏览器的ActiveX的插件了,下面我们为他关联一个变量m_test如下图



4.关联好后在确定按钮处填写一个消息响应。

m_test.Navigate("www.baidu.com",NULL,NULL,NULL,NULL);

用这个变量的一个成员函数打开这个网址



关于WebBrowser几个问题

1.关于如何取得这个网页的内容

HRESULT hr;
IDispatch* lpDispatch;
lpDispatch = m_WebBrower.GetDocument();
IHTMLDocument2* lpDocument2;
hr = lpDispatch->QueryInterface(IID_IHTMLDocument2, (PVOID*)&lpDocument2);
if ( hr == S_OK )
{

IHTMLElement * pBody;
lpDocument2->get_body(&pBody);
BSTR html;//存放html源代码
CComBSTR html_t;//用于将BSTR转换为cout可以处理的字符串
pBody->get_innerHTML(&html);
CString strCookie(html);
CFile myfile("1.html",CFile::modeWrite|CFile::modeCreate);
myfile.Write(strCookie,strCookie.GetLength());
myfile.Close();
pBody->Release();
lpDocument2->Release();
}
lpDispatch->Release();


  

2.有关于如何取得这个网页的cookie

HRESULT hr;
IDispatch* lpDispatch;
lpDispatch = m_WebBrower.GetDocument();
IHTMLDocument2* lpDocument2;
hr = lpDispatch->QueryInterface(IID_IHTMLDocument2, (PVOID*)&lpDocument2);
if ( hr == S_OK )
{

hr = lpDocument2->get_cookie(&bstrCookie);
if ( hr == S_OK )
{
CString strCookie(bstrCookie);
CFile myfile("1.txt",CFile::modeWrite|CFile::modeCreate);
myfile.Write(strCookie,strCookie.GetLength());
myfile.Close();
//::MessageBox(NULL, strCookie,"当前Cookie", MB_ICONINFORMATION);

}
lpDocument2->put_cookie(NULL);
pBody->Release();
lpDocument2->Release();
}
lpDispatch->Release();


 3.关于一些消息映射

往往我们要等待网页加载完成才能进行一些操作,微软为我们提供了丰富的消息映射,在ClassWizard中可以看到



程序员的基础教程:菜鸟程序员
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: