Click here to Skip to main content
15,867,453 members
Articles / Desktop Programming / MFC
Article

A Gradient Title Bar for modal and modeless dialog

Rate me:
Please Sign up or sign in to vote.
4.29/5 (4 votes)
22 Jan 2001 248.1K   4.5K   50   39
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

    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


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

     
    GeneralRe: Min/Max/Close Repaint Pin
    14-Oct-01 22:03
    suss14-Oct-01 22:03 
    QuestionOther colors? Pin
    Matt Philmon13-Nov-00 9:32
    Matt Philmon13-Nov-00 9:32 
    AnswerRe: Other colors? Pin
    Matt Philmon13-Nov-00 10:07
    Matt Philmon13-Nov-00 10:07 
    Generalno tile bar message after min. Pin
    7-Nov-00 23:37
    suss7-Nov-00 23:37 
    GeneralNo Icon and Close button Pin
    Rohit5-Oct-00 23:33
    Rohit5-Oct-00 23:33 
    GeneralRe: No Icon and Close button Pin
    Matt Philmon13-Nov-00 10:14
    Matt Philmon13-Nov-00 10:14 
    GeneralRe: No Icon and Close button Pin
    Matt Philmon13-Nov-00 10:35
    Matt Philmon13-Nov-00 10:35 
    GeneralRe: No Icon and Close button Pin
    Amit Dey23-Nov-00 12:49
    Amit Dey23-Nov-00 12:49 
    Hey Matt, Thanks again. This is a good fix.
    Amit.
    GeneralIcon and close button Pin
    Rohit4-Oct-00 3:35
    Rohit4-Oct-00 3:35 
    GeneralDialog Bar... Pin
    Manikandan8-Sep-00 7:46
    Manikandan8-Sep-00 7:46 
    GeneralRe: Dialog Bar... Pin
    dimps14-Sep-00 10:47
    dimps14-Sep-00 10:47 
    GeneralRe: Dialog Bar... Pin
    Rainer Koschnick19-Sep-00 9:26
    Rainer Koschnick19-Sep-00 9:26 

    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.