This class overides the default caption painting in MFC and replaces it with a graded caption bar. The source code for the class along with a sample application can be found at ftp://ftp.rocscience.com/caption3.zip . The code originates from the MSJ article in January 1997 and then again in June 1997. I’ve made a few modifications, cleaned it up and have simplified the installation process. Some of the new features include support for the modified state and the synchronization of MainFrame and ChildFrame caption text. I also explain how to easily update ChildFrame caption text through the view class and add modified state support to the ChildFrame captions.
Steps for adding color graded caption bars to your MFC MDI application:
- Add the PaintCap.c , PaintCap.h , SubClass.cpp, and SubClass.h files to your project.
- Add the following lines to the protected section of the CMainFrame class definition (MainFrm.h)
CCaptionPainter m_capp;
virtual void OnUpdateFrameTitle(BOOL bAddToTitle);
LRESULT OnPaintMyCaption(WPARAM wp,LPARAM lp);
#include “PaintCap.h”
m_capp.Install(this,WM_PAINTMYCAPTION);
const UINT WM_PAINTMYCAPTION = WM_USER+5;
ON_MESSAGE(WM_PAINTMYCAPTION,OnPaintMyCaption)
void CMainFrame::OnUpdateFrameTitle(BOOL bAddToTitle)
{
m_capp.UpdateFrameTitle(m_hWnd,m_strTitle);
}
LRESULT CMainFrame::OnPaintMyCaption(WPARAM bActive, LPARAM lParam)
{
m_capp.PaintMyCaption(bActive,lParam,m_strTitle);
return 0;
}
Programs like VC++ modify the document name in the caption bar if the document has been modified (using SetModifiedFlag()) by appending an asterix (*) to the document name. To add this functionality requires a few modifications:
m_capp.Install(this,WM_PAINTMYCAPTION,TRUE);
virtual void SetModifiedFlag(BOOL);
void CYourDoc::SetModifiedFlag(BOOL flag)
{
CDocument::SetModifiedFlag(flag);
((CMainFrame *)AfxGetMainWnd())->RedrawCaption();
}
IMPORTANT: Make sure you change the above CYourDoc class name to the name of your CDocument derived class name.
#include “MainFrm.h”
void RedrawCaption(void);
void CMainFrame::RedrawCaption(void)
{
m_capp.Invalidate();
m_capp.PaintCaption();
}
Programs like VC++ also modify the view name in the caption bar of all the views associated with the modified document by appending an asterix (*) to the view name. To add this functionality requires a few modifications:
virtual void OnUpdateFrameTitle(BOOL bAddToTitle);
void CChildFrame::OnUpdateFrameTitle(BOOL bAddToTitle)
{
CDocument* pDoc = GetActiveDocument();
if (bAddToTitle && pDoc != NULL)
{
CString strCurCaption, strWindowText, strNewCaption;
// Get the current child window caption text
GetWindowText(strCurCaption);
// Get the special view name through the view’s window text
GetActiveView()->GetWindowText(strWindowText);
// Get the doc name attached to this window
const CString& strDocTitle = pDoc->GetTitle();
// generate our new window caption
strNewCaption = strDocTitle;
if(m_nWindow > 0){
strNewCaption += “:”;
CString temp;
temp.Format(“%d”,m_nWindow);
strNewCaption += temp;
}
if(!strWindowText.IsEmpty()){
strNewCaption += ” – “;
strNewCaption += strWindowText;
}
if(pDoc->IsModified())strNewCaption += “*”;
// Only switch to the new caption if it differs from the old
// (this reduces flicker–MFC uses AfxSetCaption)
if (strNewCaption != strCurCaption)
SetWindowText(strNewCaption);
}
// give the MDI frame window a chance to update its title
GetMDIFrame()->OnUpdateFrameTitle(bAddToTitle);
}
*Note that a call to SetWindowText from within the view class will now update the caption on the views frame. Refer to the OnSettext() View member function in the sample program for more info. If you wish to change how the caption text is displayed in the child frames edit the above function.
void CCaptionDoc::SetModifiedFlag(BOOL flag)
{
CDocument::SetModifiedFlag(flag);
((CMainFrame *)AfxGetMainWnd())->RedrawCaption();
POSITION pos = GetFirstViewPosition();
CView *pView;
CChildFrame *pChild;
while(pos!=NULL){
pView = GetNextView(pos);
if(pView){
pChild = (CChildFrame *)pView->GetParent();
if(pChild)pChild->OnUpdateFrameTitle(TRUE);
}
}
}
#include “ChildFrm.h”