Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A Gradient Title Bar for modal and modeless dialog

0.00/5 (No votes)
22 Jan 2001 1  
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