Click here to Skip to main content
Click here to Skip to main content

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 -->

    Introduction

    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.

    #define WM_MYPAINTMESSAGE WM_USER+5

    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)
    {
    	m_cap.PaintCaption(wp,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 )

    ON_MESSAGE(WM_MYPAINTMESSAGE,OnPaintMyMessage)

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

    CString str="My caption";
    m_cap.SetCaption(str);

    Then install the message hook as follows

    m_cap.Install(this,WM_MYPAINTMESSAGE)

    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:

    case WM_NCLBUTTONDOWN:
    case WM_NCRBUTTONDOWN:
    return 0; 

    That's it. Now everything should work properly.

    License

    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

    Share

    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 visualcdev@hotmail.com
     

    Comments and Discussions

     
    Questionbug CCaptionPainter::CreateFonts() under XP VS2008, and i done Pinmemberjacksp30-Nov-11 21:32 
    Questioncan i avoid drawing cutom buttons? Pinmemberingoogle16-Apr-08 21:46 
    GeneralDoes anybody recognize this... Pinmemberwaldermort23-Aug-06 10:34 
    QuestionHow to remove the custom Caption? Pinmemberpubududilena21-Mar-05 21:19 
    GeneralBUG! Pinmemberihoapm13-Dec-04 20:20 
    QuestionButton on dialog title??? Pinmemberxxhimanshu21-Aug-03 21:53 
    GeneralOnNcActivate not called in PaintCap.cpp in modeless children PinsussJoeDude20024-Oct-02 13:09 
    GeneralChild Dlg: No App Icon or Restore buttons when minimized PinsussJoeDude20023-Oct-02 14:11 
    GeneralRe: Child Dlg: No App Icon or Restore buttons when minimized PinmemberAmit Dey3-Oct-02 22:25 
    GeneralRe: Child Dlg: No App Icon or Restore buttons when minimized PinsussJoeDude20024-Oct-02 7:03 
    GeneralFit a dialog into a View of a Window PinmemberAnonymous17-Apr-02 8:28 
    GeneralRe: Fit a dialog into a View of a Window PinmemberAnonymous25-Apr-02 11:24 
    GeneralCustom Caption for SubClassing Dialogs !! Pinmembernguyencindy130-Oct-01 11:52 
    GeneralProblem with MDI PinmemberParas20-Aug-01 22:28 
    GeneralRe: Problem with MDI Pinmemberingoogle16-Apr-08 21:00 
    GeneralIt would be cool if... PinmemberMatt Philmon4-Jan-01 6:45 
    GeneralRe: It would be cool if... PinmemberAlexMarbus23-Jan-01 12:11 
    GeneralRe: It would be cool if... PinmemberAnonymous16-Feb-01 8:42 
    GeneralCustom Captions may be of interest Pinmemberdlorde18-Dec-00 5:01 
    GeneralRe: Custom Captions may be of interest PinmemberAmit Dey19-Dec-00 23:13 
    GeneralRe: Custom Captions may be of interest PinmemberEugene Polonsky29-Apr-02 6:32 
    GeneralRe: Custom Captions may be of interest PinmemberAmit Dey7-Jun-02 12:04 
    GeneralRe: Custom Captions may be of interest Pinmemberdlorde9-Jun-02 23:21 
    GeneralRe: Custom Captions may be of interest PinmemberAmit Dey10-Jun-02 18:31 
    GeneralRe: Custom Captions may be of interest PinmemberAmit Dey10-Jun-02 18:31 

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

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

    | Advertise | Privacy | Mobile
    Web03 | 2.8.140827.1 | Last Updated 23 Jan 2001
    Article Copyright 2000 by Amit Dey
    Everything else Copyright © CodeProject, 1999-2014
    Terms of Service
    Layout: fixed | fluid