Article ID: 196835
Article Last Modified on 6/1/2005
CMyOleControlContainer::CMyOleControlContainer(CWnd* pWnd) :
COleControlContainer(pWnd)
{
}
virtual COleControlContainer* CreateContainer(CWnd* pWnd)
{
// Advanced control container applications may want to override.
return new CMyOleControlContainer(pWnd);
}
CMyOccManager theManager;
// ...
BOOL CMfcaxscrvbApp::InitInstance()
{
// Initialize OLE libraries
if (!AfxOleInit())
// ...
AfxEnableControlContainer(&theManager);
NOTE: For a CHtmlView derived class, its Create function calls
AfxEnableControlContainer() causing it to override our initial custom
COccManager class. To avoid this problem, you need to override
CHtmlView::Create, copy all the code into your overridden Create function minus
the call to AfxEnableC ntrolContainer(). Remember NOT to call the base class
Create method since the original implementation did not.
#undef AFX_DATA
#define AFX_DATA AFX_DATA_IMPORT
#include "c:\program files\devstudio\vc\mfc\src\occimpl.h"
#undef AFX_DATA
#define AFX_DATA AFX_DATA_EXPORT
141277 How to override an interface in an MFC application
// Inside header definition in .h.
class CMyOleControlContainer : public COleControlContainer
{
// ...
// Interface maps.
BEGIN_INTERFACE_PART(MyOleIPFrame, IOleInPlaceFrame)
INIT_INTERFACE_PART(CMyOleControlContainer, MyOleIPFrame)
STDMETHOD(GetWindow)(HWND*);
STDMETHOD(ContextSensitiveHelp)(BOOL);
STDMETHOD(GetBorder)(LPRECT);
STDMETHOD(RequestBorderSpace)(LPCBORDERWIDTHS);
STDMETHOD(SetBorderSpace)(LPCBORDERWIDTHS);
STDMETHOD(SetActiveObject)(LPOLEINPLACEACTIVEOBJECT,
LPCOLESTR);
STDMETHOD(InsertMenus)(HMENU, LPOLEMENUGROUPWIDTHS);
STDMETHOD(SetMenu)(HMENU, HOLEMENU, HWND);
STDMETHOD(RemoveMenus)(HMENU);
STDMETHOD(SetStatusText)(LPCOLESTR);
STDMETHOD(EnableModeless)(BOOL);
STDMETHOD(TranslateAccelerator)(LPMSG, WORD);
END_INTERFACE_PART(MyOleIPFrame)
DECLARE_INTERFACE_MAP()
};
// Inside implementation file .cpp:
BEGIN_INTERFACE_MAP(CMyOleControlContainer, COleControlContainer)
INTERFACE_PART(CMyOleControlContainer, IID_IOleInPlaceFrame,
MyOleIPFrame)
END_INTERFACE_MAP()
STDMETHODIMP CMyOleControlContainer::XMyOleIPFrame::QueryInterface(
REFIID iid, LPVOID* ppvObj)
{
METHOD_PROLOGUE_EX_(CMyOleControlContainer, MyOleIPFrame)
return (HRESULT)pThis->InternalQueryInterface(&iid, ppvObj);
}
STDMETHODIMP_(ULONG) CMyOleControlContainer::XMyOleIPFrame::AddRef()
{
METHOD_PROLOGUE_EX_(CMyOleControlContainer, MyOleIPFrame)
return (ULONG)pThis->InternalAddRef();
}
// ...
// Cut and paste all of this code from the MFC source in Occimpl.h
// and change all references to COleControlContainer to reference
// your class. Then, modify methods as desired. For example, here is
// a customized enablemodeless call:
STDMETHODIMP
CMyOleControlContainer::XMyOleIPFrame::EnableModeless(BOOL f)
{
METHOD_PROLOGUE_EX(CMyOleControlContainer, MyOleIPFrame)
CWinApp* pApp = AfxGetApp();
if (!pApp)
return E_FAIL;
pApp->EnableModeless(f);
return S_OK;
}
CMyOleControlSite::CMyOleControlSite(COleControlContainer* pCtrlCont) :
COleControlSite(pCtrlCont)
{
}
The COccManager::CreateSite is used to create COleControlSite objects.
To use your ControlSite class, add the following method to your COccManager
class definition:
virtual COleControlSite* CreateSite(COleControlContainer* pCtrlCont)
{
// advanced control container override
return new CMyOleControlSite(pCtrlCont);
}
A typical example that overrides the IOleControlSite interface follows:
// Inside header definition in .h:
class CMyOleControlSite : public COleControlSite
{
// ...
// Interface maps.
BEGIN_INTERFACE_PART(MyOleControlSite, IOleControlSite)
INIT_INTERFACE_PART(CMyOleControlSite, MyOleControlSite)
STDMETHOD(OnControlInfoChanged)();
STDMETHOD(LockInPlaceActive)(BOOL fLock);
STDMETHOD(GetExtendedControl)(LPDISPATCH* ppDisp);
STDMETHOD(TransformCoords)(POINTL* lpptlHimetric,
POINTF* lpptfContainer, DWORD flags);
STDMETHOD(TranslateAccelerator)(LPMSG lpMsg, DWORD grfModifiers);
STDMETHOD(OnFocus)(BOOL fGotFocus);
STDMETHOD(ShowPropertyFrame)();
END_INTERFACE_PART(MyOleControlSite)
DECLARE_INTERFACE_MAP()
};
// Inside implementation file .cpp:
BEGIN_INTERFACE_MAP(CMyOleControlSite, CMyOleControlSite)
INTERFACE_PART(CMyOleControlSite, IID_IOleControlSite,
MyOleControlSite)
END_INTERFACE_MAP()
STDMETHODIMP_(ULONG) CMyOleControlSite::XMyOleControlSite::AddRef()
{
METHOD_PROLOGUE_EX_(CMyOleControlSite, OleControlSite)
return (ULONG)pThis->InternalAddRef();
}
STDMETHODIMP_(ULONG) CMyOleControlSite::XMyOleControlSite::Release()
{
METHOD_PROLOGUE_EX_(CMyOleControlSite, OleControlSite)
return (ULONG)pThis->InternalRelease();
}
// ...
// Cut and paste all of this code from the MFC source in Occimpl.h
// and change all references to COleControlSite to reference your
// class. Then, modify methods as desired.
141277 How to override an interface in an MFC application
Keywords: kbhowto kbcontainer kbcode KB196835