Gradient color caption bar

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:

 

  1. Add the PaintCap.c , PaintCap.h , SubClass.cpp, and SubClass.h files to your project.
  2. Add the following lines to the protected section of the CMainFrame class definition (MainFrm.h)
  3. 	CCaptionPainter m_capp;
    	virtual void OnUpdateFrameTitle(BOOL bAddToTitle);
    	LRESULT OnPaintMyCaption(WPARAM wp,LPARAM lp);
  4. Add the following include to the top of the MainFrm.h file
  5. #include "PaintCap.h"
  6. Add the following to the CMainFrame::OnCreate member function just after the toolbar stuff (MainFrm.cpp):
  7. m_capp.Install(this,WM_PAINTMYCAPTION);
  8. Add the following to the top of the MainFrm.cpp file:
  9. const UINT WM_PAINTMYCAPTION = WM_USER+5;
  10. Place the following message map definition to the CMainFrame class (MainFrm.cpp). Make sure it's after the grayed out AFX_MSG_MAP block;
  11. ON_MESSAGE(WM_PAINTMYCAPTION,OnPaintMyCaption)
  12. Add the following two member functions to the CMainFrame class:
  13. 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:

     

  14. In the CMainFrame::OnCreate member function change the m_capp.Install call added in step 2 above to:
  15. m_capp.Install(this,WM_PAINTMYCAPTION,TRUE);
  16. Add a public virtual function override for the SetModifiedFlag do your CDocument derived class:
  17. virtual void SetModifiedFlag(BOOL);

  18. Add the member function definition to your CDocument derived cpp file:
  19. 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.

  20. Add the MainFrm.h include to the top of your CDocument derived cpp file:
  21. #include "MainFrm.h"

  22. Add a public member function called RedrawCaption to your CMainFrame class in MainFrm.h:
  23. void RedrawCaption(void);
  24. Add the member function definition to the MainFrm.cpp file:
  25. 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:

     

  26. Let's first add an update handler for the caption text so that we can modify it and save it with the ChildFrame. Add the following public virtual function definition to the CChildFrame class in the CChildFrm.h file:
  27. virtual void OnUpdateFrameTitle(BOOL bAddToTitle);

  28. Add the following OnUpdateFrameTitle virtual member function to your CChildFrm.cpp file:
  29. 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.

  30. Modify the SetModifiedFlag function you added in step 10 above to:
  31. 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);
        }
      }
    }
  32. Add the include file ChildFrm.h to the top of the file containing the SetModifiedFlag code in step 16:
#include "ChildFrm.h"

 

 



Comments

  • Asterix Implementation

    Posted by chilly32 on 05/23/2005 05:18am

    Another way to implements asterix: http://www.neatcpp.com/NCAsterix.html

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • You probably have several goals for your patient portal of choice. Is "community" one of them? With a bevy of vendors offering portal solutions, it can be challenging for a hospital to know where to start. Fortunately, YourCareCommunity helps ease the decision-making process. Read this white paper to learn more. "3 Ways Clinicians can Leverage a Patient Portal to Craft a Healthcare Community" is a published document owned by www.medhost.com

  • Ever-increasing workloads and the challenge of containing costs leave companies conflicted by the need for increased processing capacity while limiting physical expansion. Migration to HP's new generation of increased-density rack-and-blade servers can address growing demands for compute capacity while reducing costly sprawl. Sponsored by: HP and Intel® Xeon® processors Intel, the Intel logo, and Xeon Inside are trademarks of Intel Corporation in the U.S. and/or other countries. HP is the sponsor …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds