![]()
Cool Owner Drawn Caption
Version 3.1 May 10, 1998
By
Brent Corkum
Steps for adding color graded caption bars to your MFC MDI application:
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"