添加webbrowser Controls组件
Insert OK Close。
添加导航代码
void CExtractTextDlg::OnButtonExplorer()
{
UpdateData(TRUE);
if (!m_strUrl.IsEmpty())
{
COleVariant vtEmpty;
// m_WebBrowser1.Navigate(_T("F:\\test.html"), &vtEmpty, &vtEmpty,&vtEmpty, &vtEmpty);
m_WebBrowser1.Navigate(m_strUrl, &vtEmpty, &vtEmpty,&vtEmpty, &vtEmpty);
}
}
为使用IHTMLElement2,dlg头文件加入
#include <mshtml.h>
#include "webbrowser2.h"
void CExtractTextDlg::OnButtonExtract()
{
IHTMLDocument2*pDoc=(IHTMLDocument2*)m_WebBrowser1.GetDocument();
IHTMLElement*pHTMLBodyElement=NULL;
if(pDoc!=NULL)
{
HRESULT hr=pDoc->get_body(&pHTMLBodyElement);
if(SUCCEEDED(hr)&&pHTMLBodyElement!=NULL)
{
BSTR b;
pHTMLBodyElement->get_innerText(&b);
CString strHtml(b);
//自己添加
m_strInnerText=strHtml;
AfxMessageBox(m_strInnerText);
UpdateData(FALSE);
}
}
}
另外一种方法
void CDlgebDlg::getcode()
{
/**
* @定义变量
*/
long BufSize = 100000L;
long Size;
FILE *fp;
IStream *Stream;
HGLOBAL hHTMLText;
ULARGE_INTEGER size;
IPersistStreamInit *psi;
IHTMLDocument2 *htm;
/**
* @获取指定网页的源代码
*/
htm = (IHTMLDocument2*)m_WebBrowser1.GetDocument();
htm->QueryInterface((REFIID)IID_IPersistStreamInit, (void**) &psi);
psi->GetSizeMax(&size);
hHTMLText = GlobalAlloc(GMEM_FIXED, BufSize);
if ((void*)NULL == hHTMLText )
{
return;
}
memset((char*)hHTMLText,0,BufSize);
CreateStreamOnHGlobal(hHTMLText, true, &Stream);
psi->Save(Stream, false);
Size = strlen((char*)hHTMLText);
/**
* @相关信息保存到指定文件中
*/
fp = fopen("a.txt","wt");
fwrite((char*)hHTMLText,Size,1, fp);
fclose(fp);
// AfxMessageBox((char*)hHTMLText);
GlobalFree(hHTMLText);
psi->Release ();
}
//其中GIVENFILE是文件名称,如a.txt.

void CDlgebDlg::OnButton5()
{
// TODO: Add your control notification handler code here
m_WebBrowser1.GoHome();
}
void CDlgebDlg::OnButton2()
{
// TODO: Add your control notification handler code here
m_WebBrowser1.GoBack();
}
void CDlgebDlg::OnButton3()
{
// TODO: Add your control notification handler code here
m_WebBrowser1.GoForward();
}
void CDlgebDlg::OnButton4()
{
// TODO: Add your control notification handler code here
m_WebBrowser1.Stop();
}
void CDlgebDlg::OnButton7()
{
// TODO: Add your control notification handler code here
m_WebBrowser1.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_PROMPTUSER,
NULL, NULL);
}
void CDlgebDlg::OnButton8()
{
// TODO: Add your control notification handler code here
m_WebBrowser1.ExecWB(OLECMDID_PAGESETUP, OLECMDEXECOPT_PROMPTUSER,
NULL, NULL);
}