QA: How do I add a title to a view?

Joao Paulo Figueira (joao.fig@mail.telepac.pt), September 29, 2003.

Question

I want to add a title to a CView. How do I do this without changing my view code?

Answer

Before I answer this question, let me make a brief comment on why this is a valid question. The Designed for Windows for Pocket PC Handbook for Software Applications document clearly states that the application title must be displayed on the NavBar (topmost bar on the Pocket PC screen) and that the NavBar should not be used for anything application specific. So there is a real interest in devising a customized solution like this. Now, on to the answer.

The class is derived from CControlBar and implements three virtual members that allow it to behave like an MFC toolbar: OnUpdateCmdUI, CalcFixedLayout and CalcDynamicLayout. To set the title, you use the SetTitle method. By default, this method redraws the title bar, but you can override this functionality setting the bRepaint parameter to FALSE.

Use in a Document / View Application

To use this title bar class in a document view application, use the sample application as a guide. This is a revised version of the PicView application I published on the CodeProject a few months ago. It implements the document / view model in a simplified way (does not allow you to create new documents), but shows the basic principles.

The title bar object is encapsulated in the view object and is created when the view is created, when the OnCreate event is processed:

int CPicViewView::OnCreate(LPCREATESTRUCT lpCreateStruct ) { int iResult = CView::OnCreate(lpCreateStruct); if(iResult == 0) { ShowDoneButton(TRUE); m_wndTitleBar.Create(GetParent()); } return iResult; }

Notice that the title bar is a child of the frame window (a sibling to the view). Destruction of the title is done when the view's OnDestroy event is called:

void CPicViewView::OnDestroy() { if(IsWindow(m_wndTitleBar)) m_wndTitleBar.DestroyWindow(); CView::OnDestroy(); }

The title's text is set during the view's OnUpdate processing:

void CPicViewView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) { CSize sizeTotal; CPicViewDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if(pDoc->GetBitmap()) { sizeTotal.cx = pDoc->GetZoomWidth(); sizeTotal.cy = pDoc->GetZoomHeight(); SetScrollSizes(MM_TEXT, sizeTotal); InvalidateRect(NULL, FALSE); m_wndTitleBar.SetTitle(pDoc->GetTitle()); } }

Finally, the sample application supports a very simple mechanism for showing and hiding the title. This is achieved through a main menu option that toggles the title appearance.

void CPicViewView::OnUpdateShowTitle(CCmdUI* pCmdUI) { pCmdUI->SetCheck(IsWindow(m_wndTitleBar)); } void CPicViewView::OnShowTitle() { CFrameWnd* pFrame = (CFrameWnd*)GetParent(); if(IsWindow(m_wndTitleBar)) m_wndTitleBar.DestroyWindow(); else m_wndTitleBar.Create(pFrame); pFrame->RecalcLayout(); }

The layout is recalculated by forcing a call to RecalcLayout, that will resize the view so that the title will be shown or hidden.

And that's it.

Sample

You can download a sample here - PicView.zip (170K).

Related resources: