Click here to Skip to main content
13,248,087 members (37,117 online)
Click here to Skip to main content
Add your own
alternative version


49 bookmarked
Posted 6 Sep 2000

A Gradient Title Bar for modal and modeless dialog

, 22 Jan 2001
Rate this:
Please Sign up or sign in to vote.
This article shows you how to give your Win95/NT4 modeless dialogs a Win98/W2K like gradient title bar.
  • Download source files - 11 Kb
  • Download demo project - 31 Kb
  • Sample Image - GradientTitleBar.gif

    <!-- Main HTML starts here -->


    This article shows you how to add a Win98 like gradient title bar to a modal or a modeless dialog and is a modification of Philip Petrescu's article found at codeguru. I also use Paul DiLascia's famous CSubclassWnd from 1997 Microsoft Sytems Journal.

    I needed a gradient title bar in a modal /modeless dialog and had to modify the code for it to work properly as I needed. It works in modal/modeless dialogs with support for changing the caption text. I acknowledge the contribution of all the people whose names appear in various parts of the source. They are the gurus, I'm just a kiddo!

    How to Use

    Include the files PaintCap.cpp, PaintCap.h, Subclass.cpp, Subclass.h in you project, and declare a member variable of type CCaptionPainter:

    CCaptionPainter m_cap

    You can use the ClassView to add this member variable, but if you add it manually then ensure you add the

    #include "PaintCap.h"

    to your the dialog's header file.

    Add a user defined message WM_MYPAINTMESSAGE to your dialog's header.


    Write a handler for this message in the dialog's .cpp file and call CCaptionPainter's PaintCaption member function with the required parameters as shown below.

    LRESULT <Your Dialog>::OnMyPaintMessage(WPARAM wp,LPARAM lp)
    	return 0;

    Next map the user defined message to the handler by adding a message map to the dialog class's .cpp file (after the AFX_MESSAGE_MAP and before END_MESSAGE_MAP )


    In dialog's InitInstance, first set the caption text by calling SetCaption function like

    CString str="My caption";

    Then install the message hook as follows


    That's it.

    If you want to change the caption text at any time, use the following:

    CString newstr="New Text"; // new string
    m_cap.SetCaption(newstr); //set the caption
    m_cap.UpdateFrameTitle(this->m_hWnd); //paint the new caption 

    You can use the gradient caption bar similarly in modal dialogs.

    I have tested this code under Win95 with VC++ 6.0 but I think it should work just fine with other versions on other platforms like Win98/NT.

    Removing Icon and Close button display

    Normally, the dialog bar displays the close button and the icon in a dialog. But there are workarounds these. If you do not want to display the icon in the dialog bar or do not want the close button to appear, you have to edit some of the code yourself. Look in the paintcap.cpp file and do the following:

    Locate the PaintCaption(WPARAM bActive, LPARAM lParam) member function of the CCaptionPainter class.

    To remove icon support

    Move to the line:

    int cxIcon=DrawIcon(pc);

    Replace this line with the following code,

    int cxIcon=0;

    To remove the close button

    Move to the line

    int cxButns= DrawButtons(pc);

    Replace this line with the following code,

    int cxButns=0;

    You are almost done. If your dialog is not system menu enabled, verything should work out fine. If the system menu option is enabled in the dialog properties, you need to handle the WM_NCLBUTTONDOWN and the WM_NCRBUTTONDOWN messages.

    If you do not do this, then as soon as you click on the dialog bar, the close button pops up and the effect is rather undesirable.

    To handle these messages, look at CCaptionPainter's WindowProc() . Add the following code after ypu handle the WM_SETTINGCHANGE and WM_SYSCOLORCHANGE messages:

    return 0; 

    That's it. Now everything should work properly.


    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    About the Author

    Amit Dey
    Web Developer
    India India
    Amit Dey is a freelance programmer from Bangalore,India. Chiefly programming VC++/MFC, ATL/COM and PocketPC and Palm platforms. Apart from programming and CP, he is a self-taught guitar and keyboard player.

    He can be contacted at

    You may also be interested in...

    Comments and Discussions

    Questionbug CCaptionPainter::CreateFonts() under XP VS2008, and i done Pin
    jacksp30-Nov-11 22:32
    memberjacksp30-Nov-11 22:32 
    Questioncan i avoid drawing cutom buttons? Pin
    ingoogle16-Apr-08 22:46
    memberingoogle16-Apr-08 22:46 
    GeneralDoes anybody recognize this... Pin
    waldermort23-Aug-06 11:34
    memberwaldermort23-Aug-06 11:34 
    QuestionHow to remove the custom Caption? Pin
    pubududilena21-Mar-05 22:19
    memberpubududilena21-Mar-05 22:19 
    GeneralBUG! Pin
    ihoapm13-Dec-04 21:20
    memberihoapm13-Dec-04 21:20 
    QuestionButton on dialog title??? Pin
    xxhimanshu21-Aug-03 22:53
    memberxxhimanshu21-Aug-03 22:53 
    GeneralOnNcActivate not called in PaintCap.cpp in modeless children Pin
    JoeDude20024-Oct-02 14:09
    sussJoeDude20024-Oct-02 14:09 
    GeneralChild Dlg: No App Icon or Restore buttons when minimized Pin
    JoeDude20023-Oct-02 15:11
    sussJoeDude20023-Oct-02 15:11 
    GeneralRe: Child Dlg: No App Icon or Restore buttons when minimized Pin
    Amit Dey3-Oct-02 23:25
    memberAmit Dey3-Oct-02 23:25 
    GeneralRe: Child Dlg: No App Icon or Restore buttons when minimized Pin
    JoeDude20024-Oct-02 8:03
    sussJoeDude20024-Oct-02 8:03 
    GeneralFit a dialog into a View of a Window Pin
    Anonymous17-Apr-02 9:28
    memberAnonymous17-Apr-02 9:28 
    GeneralRe: Fit a dialog into a View of a Window Pin
    Anonymous25-Apr-02 12:24
    memberAnonymous25-Apr-02 12:24 
    GeneralCustom Caption for SubClassing Dialogs !! Pin
    nguyencindy130-Oct-01 12:52
    membernguyencindy130-Oct-01 12:52 
    GeneralProblem with MDI Pin
    Paras20-Aug-01 23:28
    memberParas20-Aug-01 23:28 
    GeneralRe: Problem with MDI Pin
    ingoogle16-Apr-08 22:00
    memberingoogle16-Apr-08 22:00 
    GeneralIt would be cool if... Pin
    Matt Philmon4-Jan-01 7:45
    memberMatt Philmon4-Jan-01 7:45 
    GeneralRe: It would be cool if... Pin
    AlexMarbus23-Jan-01 13:11
    memberAlexMarbus23-Jan-01 13:11 
    GeneralRe: It would be cool if... Pin
    Anonymous16-Feb-01 9:42
    memberAnonymous16-Feb-01 9:42 
    GeneralCustom Captions may be of interest Pin
    dlorde18-Dec-00 6:01
    memberdlorde18-Dec-00 6:01 
    GeneralRe: Custom Captions may be of interest Pin
    Amit Dey20-Dec-00 0:13
    memberAmit Dey20-Dec-00 0:13 
    GeneralRe: Custom Captions may be of interest Pin
    Eugene Polonsky29-Apr-02 7:32
    memberEugene Polonsky29-Apr-02 7:32 
    GeneralRe: Custom Captions may be of interest Pin
    Amit Dey7-Jun-02 13:04
    memberAmit Dey7-Jun-02 13:04 
    GeneralRe: Custom Captions may be of interest Pin
    dlorde10-Jun-02 0:21
    memberdlorde10-Jun-02 0:21 
    GeneralRe: Custom Captions may be of interest Pin
    Amit Dey10-Jun-02 19:31
    memberAmit Dey10-Jun-02 19:31 
    GeneralRe: Custom Captions may be of interest Pin
    Amit Dey10-Jun-02 19:31
    memberAmit Dey10-Jun-02 19:31 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    Permalink | Advertise | Privacy | Terms of Use | Mobile
    Web04 | 2.8.171114.1 | Last Updated 23 Jan 2001
    Article Copyright 2000 by Amit Dey
    Everything else Copyright © CodeProject, 1999-2017
    Layout: fixed | fluid