CWebBrowser2(MSIE控件)如何获取已经显示在其内的网页的源代码?

qiek 2004-12-30 02:51:51
100分只给一个人!
...全文
260 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
qiek 2005-01-02
  • 打赏
  • 举报
回复
现在用不着了,所以没机会试,就看着给分吧
qrlvls 2004-12-30
  • 打赏
  • 举报
回复
see see 而已
zhaolaoxin 2004-12-30
  • 打赏
  • 举报
回复
xx
kingzai 2004-12-30
  • 打赏
  • 举报
回复
1.USE MSHTML
void CMyHtmlView::OnSource()
{
IHTMLDocument2 doc;
HGLOBAL hMem = NULL;
LPSTREAM pStream = NULL;
IPersistStreamInit *pPersistStream = NULL;
ULARGE_INTEGER uli;
HRESULT hr;
doc=GetHtmlDocument();
hr = ptrDoc.QueryInterface(IID_IPersistStreamInit, &pPersistStream);
if(pPersistStream == NULL)
return;
if(FAILED(pPersistStream->GetSizeMax(&uli)))
{
pPersistStream->Release();
return;
}
uli.LowPart = 65536;
hMem = ::GlobalAlloc(GPTR, uli.LowPart);
if(hMem == NULL)
{
pPersistStream->Release();
return;
}

hr = ::CreateStreamOnHGlobal(hMem, TRUE, &pStream);
if(FAILED(hr))
{
pPersistStream->Release();
::GlobalFree(hMem);
return;
}
hr = pPersistStream->Save(pStream, TRUE);

AfxMessageBox((LPCTSTR)hMem);
::GlobalUnlock(hMem);
::GlobalFree(hMem);
hr = pPersistStream->Release();
pStream->Release();
}
2.implementing the "View Source" option in Internet Explorer


void CMyBrowser::OnViewSource()
{
CWnd* pWnd = NULL;

CWnd* pwndShell = m_WebBrowser2.GetWindow(GW_CHILD); // get the webbrowser window pointer

if (pwndShell)
{
pWnd = pwndShell->GetWindow(GW_CHILD); //get the child window pointer
}

if(pWnd != NULL)
{
WPARAM wParam = MAKELONG(IDM_VIEWSOURCE, 1); //convert to unsigned 32 bit value and pass it to WPARAM
pWnd->SendMessage(WM_COMMAND, wParam, (LPARAM)pWndHTML->m_hWnd); //cool send a message to retreive the source.
}
}
http://www.vckbase.com/document/上面的一篇文章
stavck 2004-12-30
  • 打赏
  • 举报
回复
路过,
CoInitialize(NULL);//每个线程必须单独使用

SHDocVw::IShellWindowsPtr spSHWinds;

if(spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows)) != S_OK)
{
CString str = _T("创建浏览器对象失败,无法从取得浏览器信息");
SetErrorMessage(str);
return FALSE;
}

CString strData;
IDispatchPtr spDisp;

BOOL bRet = FALSE;

long nCount = spSHWinds->GetCount();
char * pBuf = NULL;

for (long i = 0; i < nCount; i++)
{
_variant_t va(i, VT_I4);
spDisp = spSHWinds->Item(va);
if(spDisp != NULL)
{
SHDocVw::IWebBrowser2Ptr spBrowser(spDisp);
if (spBrowser != NULL)
{
MSHTML::IHTMLDocument2Ptr spDoc(spBrowser->GetDocument());
if (spDoc != NULL)
{
//字符串转换
pBuf = _com_util::ConvertBSTRToString(spDoc->Gettitle());
CString strTitle1 = pBuf;
delete [] pBuf;
pBuf = NULL;

//判断网页标题,
if(strTitle1 == strTitle)
{
MSHTML::IHTMLElement* pElement;
spDoc->get_body(&pElement);

BSTR str;
pElement->get_innerHTML(&str);
pBuf = _com_util::ConvertBSTRToString(str);
strData = pBuf;//strData就是网页内容
delete [] pBuf;
pBuf = NULL;


pElement->Release();
}
spDoc.Release();
}
spBrowser.Release();
}
spDisp.Release();
}
}

spSHWinds.Release();
CoUninitialize();
蒋晟 2004-12-30
  • 打赏
  • 举报
回复
可以查询浏览器控件中的HTMLDocument2对象的IPersistStream接口,之后调用其Save方法,传递CreateStreamOnHGlobal创建的内存流对象的IStream接口指针作为参数。之后可以用GetHGlobalFromStream函数获得内存流对象的内存句柄,使用GlobalLock和GlobalUnlock来访问其中的数据。

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧