Search code examples
c++visual-c++mfcdockingdocument-view

MFC CView into CDockablePane


I need to place a CView derived class into a CDockablePane. Is there any code example somewhere, or can someone provide such code?

What I tried:

Apparently should be simple, online I found advice like "just create the view and set its parent to be the dialog or the dockable pane or what kind of window you want". But for some reason it doesn't work, maybe is because it needs a CFrameWnd, I don't know.

Anyway, I need to be able to do this without creating another document template class. Just to work with preexisting document and view classes.


Solution

  • Here's an example:

    a class derived from CDockablePane:

    //CRichEditPane .h

    class CRichEditPane : public CDockablePane
    {
        DECLARE_DYNAMIC(CRichEditPane)
    
    public:
        CRichEditPane();
        virtual ~CRichEditPane();
    
    protected:
        void AdjustLayout();
    protected:
        DECLARE_MESSAGE_MAP()
    public:
        afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
        afx_msg void OnSize(UINT nType, int cx, int cy);
    };
    

    //CRichEditPane .cpp

    IMPLEMENT_DYNAMIC(CRichEditPane, CDockablePane)
    
    CRichEditPane::CRichEditPane()
    {
    
    }
    
    CRichEditPane::~CRichEditPane()
    {
    }
    
    
    BEGIN_MESSAGE_MAP(CRichEditPane, CDockablePane)
        ON_WM_CREATE()
        ON_WM_SIZE()
    END_MESSAGE_MAP()
    
    
    // CRichEditPane message handlers
    
    
    int CRichEditPane::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
        if (CDockablePane::OnCreate(lpCreateStruct) == -1)
            return -1;
    
        CRuntimeClass *pClass = RUNTIME_CLASS(CRichEditViewInPane);
    
        // calling constructor using IMPLEMENT_DYNCREATE macro
        CRichEditViewInPane *pView = (CRichEditViewInPane*)pClass->CreateObject();
    
    
        if (!pView->Create(NULL, NULL, AFX_WS_DEFAULT_VIEW, CRect(0,0,0,0), this, AFX_IDW_PANE_FIRST, NULL))
        {
            return -1;
        }
    
        CRichEditCtrl ctrl;
        ctrl.Create(WS_CHILD, CRect(0, 0, 0, 0), this, 10991);
    
        return 0;
    }
    
    
    void CRichEditPane::OnSize(UINT nType, int cx, int cy)
    {
        CDockablePane::OnSize(nType, cx, cy);
    
        AdjustLayout();
    }
    

    a view class derived from CView:

    //CRichEditViewInPane .h

    class CRichEditViewInPane : public CRichEditView
    {
        DECLARE_DYNCREATE(CRichEditViewInPane)
    
    protected:
        CRichEditViewInPane();           // protected constructor used by dynamic creation
        virtual ~CRichEditViewInPane();
    
    public:
    #ifdef _DEBUG
        virtual void AssertValid() const;
    #ifndef _WIN32_WCE
        virtual void Dump(CDumpContext& dc) const;
    #endif
    #endif
    
    protected:
        DECLARE_MESSAGE_MAP()
    };
    

    //CRichEditViewInPane. cpp

    IMPLEMENT_DYNCREATE(CRichEditViewInPane, CRichEditView)
    
    CRichEditViewInPane::CRichEditViewInPane()
    {
    
    }
    
    CRichEditViewInPane::~CRichEditViewInPane()
    {
    }
    
    BEGIN_MESSAGE_MAP(CRichEditViewInPane, CRichEditView)
    END_MESSAGE_MAP()