Click here to Skip to main content
15,894,907 members
Articles / Multimedia / GDI

Custom Captions (Including Multi-line Captions)

Rate me:
Please Sign up or sign in to vote.
5.00/5 (9 votes)
15 Jul 2000CPOL 241.1K   6.8K   63  
Simple customised Window captions, including multi-line captions
#ifndef _CAPTION_H
#define _CAPTION_H

////////////////////////////////////////////////////////////////
//
// class CCaption
//
// Generic caption painter. Handles WM_NCPAINT, WM_NCACTIVATE, etc. to
// handle drawing custom captions. To use it:
//
// - call Install from your frame's OnCreate function. 
// - Set a custom CaptionBackground if desired
// - Set custom TextAttributes if required
//
//	 Derive from this class for custom caption layouts.
// 
//   If you are drawing custom caption buttons, you must handle WM_NCLBUTTONDOWN & co.
//   yourself. CCaption does not handle the mouse for custom caption buttons. 
//
//	Author: Dave Lorde	(dlorde@cix.compulink.co.uk)
//
//          Copyright January 2000
//
//			- based on a 1997 Microsoft Systems Journal
//            C++ Q&A article by Paul DiLascia. 
//
////////////////////////////////////////////////////////////////
//

#include "Subclass.h"

// forward declarations
class CCaptionBackground;
class CCaptionTextAttributes;


class AFX_EXT_CLASS CCaption : public CSubclassWnd 
{
public:
 	CCaption();
	virtual ~CCaption();

	BOOL Install			(CFrameWnd* pFrameWnd);
	void Uninstall			();

	void SetBackground		(CCaptionBackground* pBackground);
	void SetTextAttributes	(CCaptionTextAttributes* pTextAttributes);
	
	CCaptionBackground*		GetBackground		();
	CCaptionTextAttributes* GetTextAttributes	();

	virtual void Refresh();
 
protected:
	CBitmap	m_bmCaption[2];		// bitmaps for active/inactive captions 
	CSize	m_szCaption;		// size of caption rectangle
	BOOL	m_bActive;			// active/inactive state

	CCaptionBackground*		m_pBackground;
	CCaptionTextAttributes*	m_pTextAttributes;

	DECLARE_DYNAMIC(CCaption);

	// Helpers
	//
	void	Invalidate		();
	CSize	GetFrameSize	() const;
	int		GetLuminosity	(COLORREF color) const;
	int		GetIconWidth	();
	int		GetButtonsWidth	();
	COLORREF GetTextColor	(BOOL bActive);

	// Override these in derived classes if necessary
	//
	virtual void	PaintCaption();
	virtual void	PaintBitmap		(CDC* pDC);
	virtual void	PaintBackground	(CDC* pDC);
	virtual int		PaintIcon		(CDC* pDC = 0);
	virtual int		PaintButtons	(CDC* pDC = 0);
	virtual void	PaintText		(CDC* pDC);
	virtual void	PaintLowerBorder(CDC* pDC);
	virtual CRect	GetCaptionRect	();
	virtual CFont*	GetFont			(BOOL bActive);
	virtual CRect	GetTextRect		();

	// Paul DiLascia says:
	// "These are similar to, but NOT the same as the equivalent CWnd fns. Don't
	// override unless you're a guru, and even THEN I wouldn't recommend it."

	virtual LRESULT WindowProc		(UINT msg, WPARAM wp, LPARAM lp);	
	virtual void	OnNcPaint		(HRGN hUpdateRgn);
	virtual BOOL	OnNcActivate	(BOOL bActive);
	virtual void	OnSetText		(LPCTSTR lpText);
	virtual void	OnColorChange	();
};

#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions