http://blog.csdn.net/tangyin025/article/details/8675513
添加CWebBrowser2類
右鍵項目-〉Add-〉Class...-〉MFC-〉MFC Class From ActiveX Controlapp
在Available ActiveX controls中選擇Microsoft Web Browser<1.0>,而後在左側Interfaces中選擇IWebBrowser2,在點擊「〉」,就會在右側出現CWebBrowser2,按Finish就會生成對應的頭文件和cpp文件less
添加CWebBrowser2控件
在對話框資源中,右鍵Insert ActiveX Control...,而後在彈出的對話框中選擇Microsoft Web Browser,就會添加對應的控件了,對其使用Add Variable...就會自動添加CWebBrowser2成員變量並綁定控件了ide
添加必要的com事件處理,並實現DocHostUIHandler::GetHostInfo
我這邊直接把4個代碼文件貼上來:idispimp.h、idispimp.cpp、custsite.h、custsite.cpp函數
-
-
-
-
-
-
-
-
- #ifndef _IDISPIMP_H_
- #define _IDISPIMP_H_
-
- class CImpIDispatch : public IDispatch
- {
- protected:
- ULONG m_cRef;
-
- public:
- CImpIDispatch(void);
- ~CImpIDispatch(void);
-
- STDMETHODIMP QueryInterface(REFIID, void **);
- STDMETHODIMP_(ULONG) AddRef(void);
- STDMETHODIMP_(ULONG) Release(void);
-
-
- STDMETHODIMP GetTypeInfoCount(UINT* pctinfo);
- STDMETHODIMP GetTypeInfo( UINT iTInfo,
- LCID lcid,
- ITypeInfo** ppTInfo);
- STDMETHODIMP GetIDsOfNames(
- REFIID riid,
- LPOLESTR *rgszNames,
- UINT cNames,
- LCID lcid,
- DISPID *rgDispId);
- STDMETHODIMP Invoke(
- DISPID dispIdMember,
- REFIID riid,
- LCID lcid,
- WORD wFlags,
- DISPPARAMS *pDispParams,
- VARIANT *pVarResult,
- EXCEPINFO *pExcepInfo,
- UINT *puArgErr);
-
- };
- #endif //_IDISPIMP_H_
-
-
-
-
-
-
-
- #include "stdafx.h"
- #include "idispimp.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
-
-
-
-
- const WCHAR pszExtend[10]=L"xxyyzz";
- #define DISPID_Extend 12345
-
-
-
-
-
-
-
-
-
-
-
-
- CImpIDispatch::CImpIDispatch( void )
- {
- m_cRef = 0;
- }
-
- CImpIDispatch::~CImpIDispatch( void )
- {
- ASSERT( m_cRef == 0 );
- }
-
-
-
-
-
-
-
-
-
-
-
- STDMETHODIMP CImpIDispatch::QueryInterface( REFIID riid, void **ppv )
- {
- *ppv = NULL;
-
-
- if ( IID_IDispatch == riid )
- {
- *ppv = this;
- }
-
- if ( NULL != *ppv )
- {
- ((LPUNKNOWN)*ppv)->AddRef();
- return NOERROR;
- }
-
- return E_NOINTERFACE;
- }
-
-
- STDMETHODIMP_(ULONG) CImpIDispatch::AddRef(void)
- {
- return ++m_cRef;
- }
-
- STDMETHODIMP_(ULONG) CImpIDispatch::Release(void)
- {
- return --m_cRef;
- }
-
-
-
- STDMETHODIMP CImpIDispatch::GetTypeInfoCount(UINT* )
- {
- return E_NOTIMPL;
- }
-
- STDMETHODIMP CImpIDispatch::GetTypeInfo( UINT ,
- LCID ,
- ITypeInfo** )
- {
- return E_NOTIMPL;
- }
-
- STDMETHODIMP CImpIDispatch::GetIDsOfNames(
- REFIID riid,
- OLECHAR** rgszNames,
- UINT cNames,
- LCID lcid,
- DISPID* rgDispId)
- {
- HRESULT hr;
- UINT i;
-
-
- hr = NOERROR;
-
-
-
- for ( i=0; i < cNames; i++)
- {
- if ( 2 == CompareString( lcid, NORM_IGNOREWIDTH, (TCHAR*)pszExtend, 3, (TCHAR*)rgszNames[i], 3 ) )
- {
- rgDispId[i] = DISPID_Extend;
- }
- else
- {
-
- hr = ResultFromScode(DISP_E_UNKNOWNNAME);
- rgDispId[i] = DISPID_UNKNOWN;
- }
- }
- return hr;
- }
-
- STDMETHODIMP CImpIDispatch::Invoke(
- DISPID dispIdMember,
- REFIID ,
- LCID ,
- WORD wFlags,
- DISPPARAMS* pDispParams,
- VARIANT* pVarResult,
- EXCEPINFO* ,
- UINT* puArgErr)
- {
-
-
-
- if ( dispIdMember == DISPID_Extend )
- {
- if ( wFlags & DISPATCH_PROPERTYGET )
- {
- if ( pVarResult != NULL )
- {
- WCHAR buff[10]=L"Wibble";
- BSTR bstrRet = SysAllocString( buff );
- VariantInit(pVarResult);
- V_VT(pVarResult)=VT_BSTR;
- V_BSTR(pVarResult) = bstrRet;
- }
- }
- }
-
- return S_OK;
- }
-
-
-
- #ifndef __CUSTOMSITEH__
- #define __CUSTOMSITEH__
-
- #include "idispimp.h"
- #include <mshtmhst.h>
-
-
-
-
-
-
-
-
-
- class CCustomControlSite:public COleControlSite
- {
- public:
- CCustomControlSite(COleControlContainer *pCnt):COleControlSite(pCnt){}
-
- protected:
-
- DECLARE_INTERFACE_MAP();
- BEGIN_INTERFACE_PART(DocHostUIHandler, IDocHostUIHandler)
- STDMETHOD(ShowContextMenu)( DWORD dwID,
- POINT __RPC_FAR *ppt,
- IUnknown __RPC_FAR *pcmdtReserved,
- IDispatch __RPC_FAR *pdispReserved);
- STDMETHOD(GetHostInfo)(
- DOCHOSTUIINFO __RPC_FAR *pInfo);
- STDMETHOD(ShowUI)(
- DWORD dwID,
- IOleInPlaceActiveObject __RPC_FAR *pActiveObject,
- IOleCommandTarget __RPC_FAR *pCommandTarget,
- IOleInPlaceFrame __RPC_FAR *pFrame,
- IOleInPlaceUIWindow __RPC_FAR *pDoc);
- STDMETHOD(HideUI)(void);
- STDMETHOD(UpdateUI)(void);
- STDMETHOD(EnableModeless)( BOOL fEnable);
- STDMETHOD(OnDocWindowActivate)( BOOL fEnable);
- STDMETHOD(OnFrameWindowActivate)( BOOL fEnable);
- STDMETHOD(ResizeBorder)(
- LPCRECT prcBorder,
- IOleInPlaceUIWindow __RPC_FAR *pUIWindow,
- BOOL fRameWindow);
- STDMETHOD(TranslateAccelerator)(
- LPMSG lpMsg,
- const GUID __RPC_FAR *pguidCmdGroup,
- DWORD nCmdID);
- STDMETHOD(GetOptionKeyPath)(
- LPOLESTR __RPC_FAR *pchKey,
- DWORD dw);
- STDMETHOD(GetDropTarget)(
- IDropTarget __RPC_FAR *pDropTarget,
- IDropTarget __RPC_FAR *__RPC_FAR *ppDropTarget);
- STDMETHOD(GetExternal)(
- IDispatch __RPC_FAR *__RPC_FAR *ppDispatch);
- STDMETHOD(TranslateUrl)(
- DWORD dwTranslate,
- OLECHAR __RPC_FAR *pchURLIn,
- OLECHAR __RPC_FAR *__RPC_FAR *ppchURLOut);
- STDMETHOD(FilterDataObject)(
- IDataObject __RPC_FAR *pDO,
- IDataObject __RPC_FAR *__RPC_FAR *ppDORet);
- END_INTERFACE_PART(DocHostUIHandler)
- };
-
-
- class CCustomOccManager :public COccManager
- {
- public:
- CCustomOccManager(){}
- COleControlSite* CreateSite(COleControlContainer* pCtrlCont)
- {
- CCustomControlSite *pSite = new CCustomControlSite(pCtrlCont);
- return pSite;
- }
- };
-
- #endif
-
-
-
-
-
-
-
-
-
-
-
-
-
- #include "stdafx.h"
- #undef AFX_DATA
- #define AFX_DATA AFX_DATA_IMPORT
-
-
-
-
-
-
-
-
- #undef AFX_DATA
- #define AFX_DATA AFX_DATA_EXPORT
- #include "custsite.h"
- #include "App類的頭文件"
-
-
- BEGIN_INTERFACE_MAP(CCustomControlSite, COleControlSite)
- INTERFACE_PART(CCustomControlSite, IID_IDocHostUIHandler, DocHostUIHandler)
- END_INTERFACE_MAP()
-
-
-
- ULONG FAR EXPORT CCustomControlSite::XDocHostUIHandler::AddRef()
- {
- METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
- return pThis->ExternalAddRef();
- }
-
-
- ULONG FAR EXPORT CCustomControlSite::XDocHostUIHandler::Release()
- {
- METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
- return pThis->ExternalRelease();
- }
-
- HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::QueryInterface(REFIID riid, void **ppvObj)
- {
- METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
- HRESULT hr = (HRESULT)pThis->ExternalQueryInterface(&riid, ppvObj);
- return hr;
- }
-
-
-
-
-
- HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::GetHostInfo( DOCHOSTUIINFO* pInfo )
- {
-
- METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
- pInfo->dwFlags = DOCHOSTUIFLAG_NO3DBORDER | DOCHOSTUIFLAG_SCROLL_NO;
- pInfo->dwDoubleClick = DOCHOSTUIDBLCLK_DEFAULT;
-
- return S_OK;
- }
-
-
-
-
-
- HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::ShowUI(
- DWORD dwID,
- IOleInPlaceActiveObject * ,
- IOleCommandTarget * pCommandTarget,
- IOleInPlaceFrame * ,
- IOleInPlaceUIWindow * )
- {
-
- METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
-
- return S_OK;
- }
-
-
-
-
-
- HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::HideUI(void)
- {
- METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
- return S_OK;
- }
-
-
-
-
-
- HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::UpdateUI(void)
- {
- METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
-
- return S_OK;
- }
-
-
-
-
-
- HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::EnableModeless(BOOL )
- {
- METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
- return E_NOTIMPL;
- }
-
-
-
-
-
- HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::OnDocWindowActivate(BOOL )
- {
- METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
- return E_NOTIMPL;
- }
-
-
-
-
-
- HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::OnFrameWindowActivate(BOOL )
- {
- METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
- return E_NOTIMPL;
- }
-
-
-
-
-
- HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::ResizeBorder(
- LPCRECT ,
- IOleInPlaceUIWindow* ,
- BOOL )
- {
- METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
- return E_NOTIMPL;
- }
-
-
-
-
-
- HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::ShowContextMenu(
- DWORD ,
- POINT* ,
- IUnknown* ,
- IDispatch* )
- {
- METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
- return S_OK;
- }
-
-
-
-
-
- HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::TranslateAccelerator(LPMSG lpMsg,
- const GUID __RPC_FAR *pguidCmdGroup,
- DWORD nCmdID)
- {
- METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
- return S_FALSE;
- }
-
-
-
-
-
-
- HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::GetOptionKeyPath(BSTR* pbstrKey, DWORD)
- {
-
- METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
- return E_NOTIMPL;
- }
-
- STDMETHODIMP CCustomControlSite::XDocHostUIHandler::GetDropTarget(
- IDropTarget __RPC_FAR *pDropTarget,
- IDropTarget __RPC_FAR *__RPC_FAR *ppDropTarget)
- {
- METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
- return E_NOTIMPL;
- }
-
- STDMETHODIMP CCustomControlSite::XDocHostUIHandler::GetExternal(
- IDispatch __RPC_FAR *__RPC_FAR *ppDispatch)
- {
-
- IDispatch* pDisp = (IDispatch*)theApp.m_pDispOM;
- pDisp->AddRef();
- *ppDispatch = pDisp;
- return S_OK;
- }
-
- STDMETHODIMP CCustomControlSite::XDocHostUIHandler::TranslateUrl(
- DWORD dwTranslate,
- OLECHAR __RPC_FAR *pchURLIn,
- OLECHAR __RPC_FAR *__RPC_FAR *ppchURLOut)
- {
- METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
- return E_NOTIMPL;
- }
-
- STDMETHODIMP CCustomControlSite::XDocHostUIHandler::FilterDataObject(
- IDataObject __RPC_FAR *pDO,
- IDataObject __RPC_FAR *__RPC_FAR *ppDORet)
- {
- METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
- return E_NOTIMPL;
- }
修改App類,截獲COM容器事件
在本身的App類中添加一個成員,注意正確的includeoop
- class CMyApp : public CWinApp
- {
- public:
- ...
- class CImpIDispatch* m_pDispOM;
- };
在CMyApp::InitInstance中替換原來的AfxEnableControlContainer(在生成項目時選中支持ActiveX容器,就會有這一項)ui
- BOOL CMyApp::InitInstance()
- {
- ...
- CWinAppEx::InitInstance();
-
-
- CCustomOccManager *pMgr = new CCustomOccManager;
-
-
- m_pDispOM = new CImpIDispatch;
-
-
-
- AfxEnableControlContainer(pMgr);
- ...
- }
最後不要忘記在析構函數中清理m_pDispOMthis