适用版本 VS2005.
#ifndef HtmlCtrl_H
#define HtmlCtrl_H
//
#include <AfxOle.h>
#include <AfxHtml.h>
#include <AtlBase.h>
//
#include "OwnerPolicy.h"
//
template <typename OwnerT>
class HtmlCtrlT : public CHtmlView,
public OwnerPolicy<OwnerT>
{
public:
virtual HRESULT OnShowContextMenu(DWORD dwID, LPPOINT ppt, LPUNKNOWN pcmdtReserved, LPDISPATCH pdispReserved)
{
return S_OK;
}
virtual HRESULT OnGetExternal(LPDISPATCH *lppDispatch)
{
OwnerT *pOwner = owner();
*lppDispatch = (pOwner == NULL) ? NULL : (owner()->GetIDispatch(TRUE));
return S_OK;
}
HRESULT SetHTMLText(LPCTSTR lpszHTML)
{
HRESULT hr = E_FAIL;
COleStreamFile fileOleStream;
fileOleStream.CreateMemoryStream();
CString strHTML(lpszHTML);
fileOleStream.Write(strHTML.GetBuffer(0), strHTML.GetLength() * sizeof(TCHAR));
strHTML.ReleaseBuffer();
fileOleStream.SeekToBegin();
CComQIPtr<IPersistStreamInit> pPersistStreamInit(GetHtmlDocument());
if (pPersistStreamInit == NULL) {
Navigate(TEXT("about:blank"));
}
pPersistStreamInit = GetHtmlDocument();
if (pPersistStreamInit) {
hr = pPersistStreamInit->Load(static_cast<IStream *>(fileOleStream.GetStream()));
}
return hr;
}
virtual void PostNcDestroy()
{
// disable CView::PostNcDestroy(), he will call (delete this);
}
};
//
#endif