Download source files - 11 Kb
Download demo project - 31 Kb
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";
m_cap.SetCaption(newstr);
m_cap.UpdateFrameTitle(this->m_hWnd);
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.