转:CWebBrowser2去除边框、滚动条、右键菜单
http://blog.csdn.net/tangyin025/article/details/8675513
添加CWebBrowser2类
右键项目-〉Add-〉Class...-〉MFC-〉MFC Class From ActiveX Control
在Available ActiveX controls中选择Microsoft Web Browser<1.0>,然后在左侧Interfaces中选择IWebBrowser2,在点击“〉”,就会在右侧出现CWebBrowser2,按Finish就会生成对应的头文件和cpp文件
添加CWebBrowser2控件
在对话框资源中,右键Insert ActiveX Control...,然后在弹出的对话框中选择Microsoft Web Browser,就会添加对应的控件了,对其使用Add Variable...就会自动添加CWebBrowser2成员变量并绑定控件了
添加必要的com事件处理,并实现DocHostUIHandler::GetHostInfo
我这边直接把4个代码文件贴上来:idispimp.h、idispimp.cpp、custsite.h、custsite.cpp
- /*
- * IDispimp.H
- * IDispatch
- *
- * Copyright (c)1995-2000 Microsoft Corporation, All Rights Reserved
- */
- #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);
- //IDispatch
- STDMETHODIMP GetTypeInfoCount(UINT* pctinfo);
- STDMETHODIMP GetTypeInfo(/* [in] */ UINT iTInfo,
- /* [in] */ LCID lcid,
- /* [out] */ ITypeInfo** ppTInfo);
- STDMETHODIMP GetIDsOfNames(
- /* [in] */ REFIID riid,
- /* [size_is][in] */ LPOLESTR *rgszNames,
- /* [in] */ UINT cNames,
- /* [in] */ LCID lcid,
- /* [size_is][out] */ DISPID *rgDispId);
- STDMETHODIMP Invoke(
- /* [in] */ DISPID dispIdMember,
- /* [in] */ REFIID riid,
- /* [in] */ LCID lcid,
- /* [in] */ WORD wFlags,
- /* [out][in] */ DISPPARAMS *pDispParams,
- /* [out] */ VARIANT *pVarResult,
- /* [out] */ EXCEPINFO *pExcepInfo,
- /* [out] */ UINT *puArgErr);
- };
- #endif //_IDISPIMP_H_
- /*
- * idispimp.CPP
- * IDispatch for Extending Dynamic HTML Object Model
- *
- * Copyright (c)1995-2000 Microsoft Corporation, All Rights Reserved
- */
- #include "stdafx.h"
- #include "idispimp.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- // Hardcoded information for extending the Object Model
- // Typically this would be supplied through a TypeInfo
- // In this case the name "xxyyzz" maps to DISPID_Extend
- const WCHAR pszExtend[10]=L"xxyyzz";
- #define DISPID_Extend 12345
- /*
- * CImpIDispatch::CImpIDispatch
- * CImpIDispatch::~CImpIDispatch
- *
- * Parameters (Constructor):
- * pSite PCSite of the site we're in.
- * pUnkOuter LPUNKNOWN to which we delegate.
- */
- CImpIDispatch::CImpIDispatch( void )
- {
- m_cRef = 0;
- }
- CImpIDispatch::~CImpIDispatch( void )
- {
- ASSERT( m_cRef == 0 );
- }
- /*
- * CImpIDispatch::QueryInterface
- * CImpIDispatch::AddRef
- * CImpIDispatch::Release
- *
- * Purpose:
- * IUnknown members for CImpIDispatch object.
- */
- 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;
- }
- //IDispatch
- STDMETHODIMP CImpIDispatch::GetTypeInfoCount(UINT* /*pctinfo*/)
- {
- return E_NOTIMPL;
- }
- STDMETHODIMP CImpIDispatch::GetTypeInfo(/* [in] */ UINT /*iTInfo*/,
- /* [in] */ LCID /*lcid*/,
- /* [out] */ ITypeInfo** /*ppTInfo*/)
- {
- return E_NOTIMPL;
- }
- STDMETHODIMP CImpIDispatch::GetIDsOfNames(
- /* [in] */ REFIID riid,
- /* [size_is][in] */ OLECHAR** rgszNames,
- /* [in] */ UINT cNames,
- /* [in] */ LCID lcid,
- /* [size_is][out] */ DISPID* rgDispId)
- {
- HRESULT hr;
- UINT i;
- // Assume some degree of success
- hr = NOERROR;
- // Hardcoded mapping for this sample
- // A more usual procedure would be to use a TypeInfo
- for ( i=0; i < cNames; i++)
- {
- if ( 2 == CompareString( lcid, NORM_IGNOREWIDTH, (TCHAR*)pszExtend, 3, (TCHAR*)rgszNames[i], 3 ) )
- {
- rgDispId[i] = DISPID_Extend;
- }
- else
- {
- // One or more are unknown so set the return code accordingly
- hr = ResultFromScode(DISP_E_UNKNOWNNAME);
- rgDispId[i] = DISPID_UNKNOWN;
- }
- }
- return hr;
- }
- STDMETHODIMP CImpIDispatch::Invoke(
- /* [in] */ DISPID dispIdMember,
- /* [in] */ REFIID /*riid*/,
- /* [in] */ LCID /*lcid*/,
- /* [in] */ WORD wFlags,
- /* [out][in] */ DISPPARAMS* pDispParams,
- /* [out] */ VARIANT* pVarResult,
- /* [out] */ EXCEPINFO* /*pExcepInfo*/,
- /* [out] */ UINT* puArgErr)
- {
- // For this sample we only support a Property Get on DISPID_Extend
- // returning a BSTR with "Wibble" as the value
- 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;
- }
- //=--------------------------------------------------------------------------=
- // (C) Copyright 1996-2000 Microsoft Corporation. All Rights Reserved.
- //=--------------------------------------------------------------------------=
- #ifndef __CUSTOMSITEH__
- #define __CUSTOMSITEH__
- #include "idispimp.h"
- #include <mshtmhst.h>
- //
- // NOTE:
- // Some of the code in this file is MFC implementation specific.
- // Changes in future versions of MFC implementation may require
- // the code to be changed. Please check the readme of this
- // sample for more information
- //
- class CCustomControlSite:public COleControlSite
- {
- public:
- CCustomControlSite(COleControlContainer *pCnt):COleControlSite(pCnt){}
- protected:
- DECLARE_INTERFACE_MAP();
- BEGIN_INTERFACE_PART(DocHostUIHandler, IDocHostUIHandler)
- STDMETHOD(ShowContextMenu)(/* [in] */ DWORD dwID,
- /* [in] */ POINT __RPC_FAR *ppt,
- /* [in] */ IUnknown __RPC_FAR *pcmdtReserved,
- /* [in] */ IDispatch __RPC_FAR *pdispReserved);
- STDMETHOD(GetHostInfo)(
- /* [out][in] */ DOCHOSTUIINFO __RPC_FAR *pInfo);
- STDMETHOD(ShowUI)(
- /* [in] */ DWORD dwID,
- /* [in] */ IOleInPlaceActiveObject __RPC_FAR *pActiveObject,
- /* [in] */ IOleCommandTarget __RPC_FAR *pCommandTarget,
- /* [in] */ IOleInPlaceFrame __RPC_FAR *pFrame,
- /* [in] */ IOleInPlaceUIWindow __RPC_FAR *pDoc);
- STDMETHOD(HideUI)(void);
- STDMETHOD(UpdateUI)(void);
- STDMETHOD(EnableModeless)(/* [in] */ BOOL fEnable);
- STDMETHOD(OnDocWindowActivate)(/* [in] */ BOOL fEnable);
- STDMETHOD(OnFrameWindowActivate)(/* [in] */ BOOL fEnable);
- STDMETHOD(ResizeBorder)(
- /* [in] */ LPCRECT prcBorder,
- /* [in] */ IOleInPlaceUIWindow __RPC_FAR *pUIWindow,
- /* [in] */ BOOL fRameWindow);
- STDMETHOD(TranslateAccelerator)(
- /* [in] */ LPMSG lpMsg,
- /* [in] */ const GUID __RPC_FAR *pguidCmdGroup,
- /* [in] */ DWORD nCmdID);
- STDMETHOD(GetOptionKeyPath)(
- /* [out] */ LPOLESTR __RPC_FAR *pchKey,
- /* [in] */ DWORD dw);
- STDMETHOD(GetDropTarget)(
- /* [in] */ IDropTarget __RPC_FAR *pDropTarget,
- /* [out] */ IDropTarget __RPC_FAR *__RPC_FAR *ppDropTarget);
- STDMETHOD(GetExternal)(
- /* [out] */ IDispatch __RPC_FAR *__RPC_FAR *ppDispatch);
- STDMETHOD(TranslateUrl)(
- /* [in] */ DWORD dwTranslate,
- /* [in] */ OLECHAR __RPC_FAR *pchURLIn,
- /* [out] */ OLECHAR __RPC_FAR *__RPC_FAR *ppchURLOut);
- STDMETHOD(FilterDataObject)(
- /* [in] */ IDataObject __RPC_FAR *pDO,
- /* [out] */ 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
- //=--------------------------------------------------------------------------=
- // (C) Copyright 1996-2000 Microsoft Corporation. All Rights Reserved.
- //=--------------------------------------------------------------------------=
- //
- // NOTE:
- // Some of the code in this file is MFC implementation specific.
- // Changes in future versions of MFC implementation may require
- // the code to be changed. Please check the readme of this
- // sample for more information
- //
- #include "stdafx.h"
- #undef AFX_DATA
- #define AFX_DATA AFX_DATA_IMPORT
- // NOTE: THis line is a hardcoded reference to an MFC header file
- // this path may need to be changed to refer to the location of VC5 install
- // for successful compilation.
- //#include <..\src\occimpl.h>
- #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;
- }
- // * CImpIDocHostUIHandler::GetHostInfo
- // *
- // * Purpose: Called at initialization
- // *
- 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;
- }
- // * CImpIDocHostUIHandler::ShowUI
- // *
- // * Purpose: Called when MSHTML.DLL shows its UI
- // *
- HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::ShowUI(
- DWORD dwID,
- IOleInPlaceActiveObject * /*pActiveObject*/,
- IOleCommandTarget * pCommandTarget,
- IOleInPlaceFrame * /*pFrame*/,
- IOleInPlaceUIWindow * /*pDoc*/)
- {
- METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
- // We've already got our own UI in place so just return S_OK
- return S_OK;
- }
- // * CImpIDocHostUIHandler::HideUI
- // *
- // * Purpose: Called when MSHTML.DLL hides its UI
- // *
- HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::HideUI(void)
- {
- METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
- return S_OK;
- }
- // * CImpIDocHostUIHandler::UpdateUI
- // *
- // * Purpose: Called when MSHTML.DLL updates its UI
- // *
- HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::UpdateUI(void)
- {
- METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
- // MFC is pretty good about updating it's UI in it's Idle loop so I don't do anything here
- return S_OK;
- }
- // * CImpIDocHostUIHandler::EnableModeless
- // *
- // * Purpose: Called from MSHTML.DLL's IOleInPlaceActiveObject::EnableModeless
- // *
- HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::EnableModeless(BOOL /*fEnable*/)
- {
- METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
- return E_NOTIMPL;
- }
- // * CImpIDocHostUIHandler::OnDocWindowActivate
- // *
- // * Purpose: Called from MSHTML.DLL's IOleInPlaceActiveObject::OnDocWindowActivate
- // *
- HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::OnDocWindowActivate(BOOL /*fActivate*/)
- {
- METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
- return E_NOTIMPL;
- }
- // * CImpIDocHostUIHandler::OnFrameWindowActivate
- // *
- // * Purpose: Called from MSHTML.DLL's IOleInPlaceActiveObject::OnFrameWindowActivate
- // *
- HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::OnFrameWindowActivate(BOOL /*fActivate*/)
- {
- METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
- return E_NOTIMPL;
- }
- // * CImpIDocHostUIHandler::ResizeBorder
- // *
- // * Purpose: Called from MSHTML.DLL's IOleInPlaceActiveObject::ResizeBorder
- // *
- HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::ResizeBorder(
- LPCRECT /*prcBorder*/,
- IOleInPlaceUIWindow* /*pUIWindow*/,
- BOOL /*fRameWindow*/)
- {
- METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
- return E_NOTIMPL;
- }
- // * CImpIDocHostUIHandler::ShowContextMenu
- // *
- // * Purpose: Called when MSHTML.DLL would normally display its context menu
- // *
- HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::ShowContextMenu(
- DWORD /*dwID*/,
- POINT* /*pptPosition*/,
- IUnknown* /*pCommandTarget*/,
- IDispatch* /*pDispatchObjectHit*/)
- {
- METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
- return S_OK; // We've shown our own context menu. MSHTML.DLL will no longer try to show its own.
- }
- // * CImpIDocHostUIHandler::TranslateAccelerator
- // *
- // * Purpose: Called from MSHTML.DLL's TranslateAccelerator routines
- // *
- HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::TranslateAccelerator(LPMSG lpMsg,
- /* [in] */ const GUID __RPC_FAR *pguidCmdGroup,
- /* [in] */ DWORD nCmdID)
- {
- METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
- return S_FALSE;
- }
- // * CImpIDocHostUIHandler::GetOptionKeyPath
- // *
- // * Purpose: Called by MSHTML.DLL to find where the host wishes to store
- // * its options in the registry
- // *
- HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::GetOptionKeyPath(BSTR* pbstrKey, DWORD)
- {
- METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
- return E_NOTIMPL;
- }
- STDMETHODIMP CCustomControlSite::XDocHostUIHandler::GetDropTarget(
- /* [in] */ IDropTarget __RPC_FAR *pDropTarget,
- /* [out] */ IDropTarget __RPC_FAR *__RPC_FAR *ppDropTarget)
- {
- METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
- return E_NOTIMPL;
- }
- STDMETHODIMP CCustomControlSite::XDocHostUIHandler::GetExternal(
- /* [out] */ IDispatch __RPC_FAR *__RPC_FAR *ppDispatch)
- {
- // return the IDispatch we have for extending the object Model
- IDispatch* pDisp = (IDispatch*)theApp.m_pDispOM;
- pDisp->AddRef();
- *ppDispatch = pDisp;
- return S_OK;
- }
- STDMETHODIMP CCustomControlSite::XDocHostUIHandler::TranslateUrl(
- /* [in] */ DWORD dwTranslate,
- /* [in] */ OLECHAR __RPC_FAR *pchURLIn,
- /* [out] */ OLECHAR __RPC_FAR *__RPC_FAR *ppchURLOut)
- {
- METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
- return E_NOTIMPL;
- }
- STDMETHODIMP CCustomControlSite::XDocHostUIHandler::FilterDataObject(
- /* [in] */ IDataObject __RPC_FAR *pDO,
- /* [out] */ IDataObject __RPC_FAR *__RPC_FAR *ppDORet)
- {
- METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
- return E_NOTIMPL;
- }
修改App类,截获COM容器事件
在自己的App类中添加一个成员,注意正确的include
- class CMyApp : public CWinApp
- {
- public:
- ...
- class CImpIDispatch* m_pDispOM;
- };
在CMyApp::InitInstance中替换原来的AfxEnableControlContainer(在生成项目时选中支持ActiveX容器,就会有这一项)
- BOOL CMyApp::InitInstance()
- {
- ...
- CWinAppEx::InitInstance();
- // Create a custom control manager class so we can overide the site
- CCustomOccManager *pMgr = new CCustomOccManager;
- // Create an IDispatch class for extending the Dynamic HTML Object Model
- m_pDispOM = new CImpIDispatch;
- // Set our control containment up but using our control container
- // management class instead of MFC's default
- AfxEnableControlContainer(pMgr);
- ...
- }
最后不要忘记在析构函数中清理m_pDispOM