
Introduction
In this article I want to introduce two template classes, that can help you in simple dialog development. Very frequently we try to set different color or font to static, editbox or another control in our dialog. To simplify this work I wrote two classes
and
. These classes have two advantages. First of all, you don’t need to throw out your beloved class. Because these classes are templates – they can be attached to any existing CWnd-based class. Second, there is no painting of any kind in these classes.
Control works in one of two modes:
- Simple Colored Mode
- Blinking Mode
You can customise:
- Text color(s)
- Background color(s)
- Blinking delay
If your control uses
message for painting (as almost all standard windows controls do), you can use this template. Use it also for whole dialog (see "About" dialog in system menu).
Usage:
Include ColorCtrl.h in your project.
Create control with dialog editor. Add member variable for this control with class wizard.
Replace
with
CColorCtrl<CCtrlClass> m_ctrl |
e.g. replace
CEdit m_edit;
CStatic m_static;
|
with
CColorCtrl<CEdit> m_edit;
CColorCtrl<CStatic> m_static;
|
Use the following functions to change colors:
void SetTextColor(COLORREF);
COLORREF GetTextColor();
void SetTextBlinkColors(COLORREF, COLORREF);
void SetBkColor(COLORREF);
COLORREF GetBkColor();
void SetBkBlinkColors(COLORREF, COLORREF);
|
To set default color use
as argument. To set system colors use macro
, where "
" is one of system color IDs (see help on
). This macro doesn’t call
, but decorates index for further usage.
Use the folowing functions to start/stop blinking:
void StartBlink(int iWho, UINT nDelay);
void StopBlink(int iWho);
UINT GetDelay();
|
Argument can be one of :
Argument "
" can be one of :
-
– doesn’t change blinking speed
-
-
– default
-
- any other value specified in miliseconds
In version 1.2 two derived classes
and
were added, that allow you to preset control colors on template level. E.g.
CColorCtrlEx<CStatic, RGB(255,0,0), RGB(0,255,0)> m_static; |
will create static control with initial red text and green background.
Warning! Don’t use these two classes together with one control.
Use this control if you want to change font style or font height of your control.
Class supports combinations of the folowing styles:
- Bold
- Italic
- Underline
- Strikeout
Usage:
Include FontCtrl.h in your project.
Create control with dialog editor. Add member variable for this control with class wizard.
Replace
with
CFontCtrl<CCtrlClass> m_ctrl |
e.g. replace
CEdit m_edit;
CStatic m_static;
|
with
CFontCtrl<CEdit> m_edit;
CFontCtrl<CStatic> m_static;
|
Use the following functions to change font style and height:
void ChangeFontStyle(int fAdd, int fRemove = 0, BOOL fRedraw = TRUE);
void ChangeFontHeight(int nHeight, BOOL fRedraw = TRUE);
void SetFont(CFont* pFont, BOOL bRedraw = TRUE);
void SetFont(LOGFONT& lf, BOOL bRedraw = TRUE);
|
Arguments "
" and "
" can be combined from the following values:
When you use functions
together with
in any order – resulting font will have combination of styles, and height specified in
(if not equal to zero).
In version 1.1 derived class
was added, that allow you to preset font style and height on template level. E.g.
CFontCtrlEx<CStatic,
FC_FONT_BOLD|FC_FONT_UNDERLINE, 30> m_static; |
will create static control with bold, underlined text and text height equal to 30.
Also four classes for basic styles added:
CBoldCtrl, CItalicCtrl, CUnderlineCtrl, CStrikeoutCtrl |
Warning! Don’t use these five classes together with one control.
If your control doesn’t contain font (e.g. you create it in code by call to
function)
cannot change font style/height. In this case use one of
functions to set font to your control. If you create control on base of one of five derived classes – don’t worry – predefined style/height will be added to selected font.
Common Notes
Any function of any class can be called even before window created.
You can use both classes together for single control:
CFontCtrl<CColorCtrl<CStatic> > m_static;
// or
CColorCtrl<CFontCtrl<CStatic> > m_static;
// or
typedef CFontCtrl<CStatic> CFontStatic;
CColorCtrl<CFontStatic> m_static;
// or even
typedef CFontCtrlEx<CStatic,
FC_FONT_BOLD|FC_FONT_UNDERLINE,
30> CBoldUnderlineStatic;
CColorCtrlEx<CBoldUnderlineStatic,
RGB(255,0,0), RGB(0,255,0)> m_static;
|
Downloads
Download demo project – 43 Kb
Download source – 7 Kb