DataMekanix Home Docking Windows Made Easy


home / articles / docking views (2)


 

Docking views (2)

by M.Shams Mukhtar (XperSoft).

Step 1

Add a function like this to your FormView Class:

/** Note: pParent and pContext is a must in CreateView for doc/view ok */
BOOL CMyFormView::CreateView(CWnd* pParent, CCreateContext* pContext)
{
    DWORD dwStyle = AFX_WS_DEFAULT_VIEW;
    // dwStyle &= ~WS_BORDER;

    // Create with the right size (wrong position)
    CRect rect(0,0,300,300);

    /** Note:> Upon this Create(...) WM_CREATE message is sent
        so OnCreate(..) will be called
        pContext != NULL then this will be added or registered
        to the document i.e. pContext->m_pCurrentDoc->AddView(this);
        this attaches this view to the current document
        this function also sets the view's document pointer
        to this document.
    */

    if (!Create(NULL, NULL, dwStyle,
        rect, pParent, AFX_IDW_PANE_FIRST, pContext))
    {
        TRACE0("Warning: couldn't create treeview pane!. \n");
        return FALSE;
    }

    return TRUE;
}

Step 2

Add these two protected member pointer to your control bar class:

class CMyBar : public CSizingControlBar
{
protected:
      CMyFormView* m_pFormView;
      CCreateContext* m_pContext;
//...

public:
      CMyBar(CCreateContext* pContext =NULL);
//...
};

Step 3

Use this constructor for your control bar:

CMyBar::CMyBar(CCreateContext* pContext /* =NULL*/ )
{
    m_pContext = pContext;
    CRuntimeClass* pFactory = RUNTIME_CLASS(CMyFormView);
    m_pFormView = (CMyFormView *)(pFactory->CreateObject() );
}

Step 4

Add a ON_WM_CREATE handler into your control bar class:

BEGIN_MESSAGE_MAP(CMyBar, CControlBar)
    //{{AFX_MSG_MAP(CMyBar)
    ON_WM_CREATE()

    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

Step 5

Add OnCreate(...) to your control bar class

//////////////////////////////////////////////////////////////////////
// CMyBar message handlers

int CMyBar::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (baseCMyBar::OnCreate(lpCreateStruct) == -1)
        return -1;

   if (m_pFormView )
   {
        m_pFormView->CreateView(this, m_pContext);
   }
   return 0;
}

Step 6

Finally in your frame window or your child frame window (if MDI) override the OnCreateClient(...) like this:

BOOL CChildFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext*
pContext)
{
    // .....

    // Now create Control windows
    if(!CreateControlBars(pContext))
        return -1;      // fail to create

    return CMDIChildWnd::OnCreateClient(lpcs, pContext);
}

BOOL CChildFrame::CreateControlBars(CCreateContext* pContext)
{
    m_pwndDockBar = new CMyBar(pContext);

    if (!m_pwndDockBar->Create(_T("Resources"), this, CSize(80, 80),
        TRUE, 123))
    {
        TRACE0("Failed to create mybar\n");
        return FALSE;      // fail to create
    }

    return TRUE;
}

So that's it, it works and worked fine :)


Copyright © 1998-2019 DataMekanix. All rights reserved.