Colored/Blinking Controls and Dialogs with any Font

.
To see more of Yury's work, be sure to visit his home page.

Environment: Windows 9x and Windows NT, Visual C++ 6

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 CColorCtrl and CFontCtrl. 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.

CColorCtrl v1.2

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 WM_CTLCOLOR 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 CCtrlClass m_ctrl 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 CLR_DEFAULT as argument. To set system colors use macro CC_SYSCOLOR(index), where "index" is one of system color IDs (see help on ::GetSysColor). This macro doesn't call ::GetSysColor, 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 :

  • CC_BLINK_TEXT
  • CC_BLINK_BK
  • CC_BLINK_BOTH

Argument "nDelay" can be one of :

  • CC_BLINK_NOCHANGE - doesn't change blinking speed
  • CC_BLINK_FAST
  • CC_BLINK_NORMAL - default
  • CC_BLINK_SLOW
  • any other value specified in miliseconds

In version 1.2 two derived classes CColorCtrlEx and CBlinkCtrlEx 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.

CFontCtrl v1.1

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 CCtrlClass m_ctrl 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 "fAdd" and "fRemove" can be combined from the following values:

  • FC_FONT_BOLD
  • FC_FONT_ITALIC
  • FC_FONT_UNDERLINE
  • FC_FONT_STRIKEOUT

When you use functions SetFont together with ChangeFont* in any order - resulting font will have combination of styles, and height specified in ChangeFontHeight (if not equal to zero).

In version 1.1 derived class CFontCtrlEx 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 Create function) CFontCtrl cannot change font style/height. In this case use one of SetFont 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



Comments

  • Blinking ListControl Items

    Posted by Legacy on 01/07/2004 12:00am

    Originally posted by: Meherman

    I want to give my list contol a blinking effect.Every individual item of list control can blink (with any color).how can i achieve this funcionality

    Reply
  • compile error

    Posted by Legacy on 10/21/2003 12:00am

    Originally posted by: Chandu

    I am using Visual C++.NET edition on Windows 2000.
    When I use the ColorCtrl.h file in one of my dialog based appliations. It is giving the following error in BEGIN_TEMPLATE_MESSAGE_MAP of ColorCtrl.h

    error C2039: '_GetBaseMessageMap' : is not a member of 'CColorCtrl<BASE_TYPE>'.
    I don't know much about templates. Please help.

    • RE: complie error

      Posted by zyijing on 02/21/2005 02:36pm

      That is becuase BEGIN_MESSAGE_MAP macro is different in visaul c++ 6.0 and Visaul c++.Net. I changed the code in templdef.h to match with new defination. #ifdef _AFXDLL #define BEGIN_TEMPLATE_MESSAGE_MAP(theTemplArgs, theClass, baseClass) \ template \ const AFX_MSGMAP* PASCAL theClass::GetThisMessageMap()\ { return &theClass::messageMap;} \ template \ const AFX_MSGMAP* theClass::GetMessageMap() const \ { return &theClass::messageMap; } \ template \ AFX_COMDAT AFX_DATADEF const AFX_MSGMAP theClass::messageMap = \ { &baseClass::GetThisMessageMap, &theClass::_messageEntries[0] }; \ template \ AFX_COMDAT const AFX_MSGMAP_ENTRY theClass::_messageEntries[] = \ { \ I do not know much about templates either. Hopefully by posting here, somebody will correct me if I am wrong!

      Reply
    Reply
  • Problem with SetBkColor Method

    Posted by Legacy on 10/20/2003 12:00am

    Originally posted by: Manoj

    Hi Yury,
    
    

    I liked the article very much. I am trying to use your class in my trial application but after calling SetBkColor() method, my edit box's color is not changing. What could be the possible reason? Am I supposed to handle any specific Window's Message?

    Reply
  • How to change the button style to BS_OWNERDRAW

    Posted by Legacy on 07/22/2003 12:00am

    Originally posted by: Sylvain MARECHAL

    Hello,

    I currently workin with propertysheet in win32 (not MFC).
    I try to set the button style to BS_OWNERDRAW in order to redraw it with another color.

    I put the following code in the first Property Sheet winproc:

    static BOOL WINAPI FirstPageProc(HWND hDlg, UINT nMsg, WPARAM wParam, LPARAM lParam )
    {
    switch (nMsg)
    {
    case WM_INITDIALOG :
    {
    HWND hWndButton = GetDlgItem(GetParent(hDlg), IDCANCE);
    if( hWndButton )
    {
    LONG dwStyle = GetWindowLong( hWndButton, GWL_STYLE );
    dwStyle = dwStyle | BS_OWNERDRAW;
    SetWindowLong( hWndButton, GWL_STYLE, dwStyle );
    LONG dwStyle2 = GetWindowLong( hWndButton, GWL_STYLE );
    assert( dwStyle == dwStyle2 );
    }

    // etc ...

    I notice with spy that the style isn't keep by the button window.
    why ? Where should I set this code ?

    Thankx in advance,

    Sylvain

    Reply
  • How to change the scrollbar color in an multiline edit control ?

    Posted by Legacy on 07/22/2003 12:00am

    Originally posted by: Sylvain MARECHAL

    Changing the color work for me, but I need to change the scrollbar color too. Is it possible ?

    Thankx,

    Sylvain

    Reply
  • Class Wizard?

    Posted by Legacy on 09/11/2002 12:00am

    Originally posted by: Brian Rhodes

    Is there a way to include this code and still be able to use the Class Wizard (under Visual C++)?

    -Brian

    Reply
  • .NET Compiler-Error

    Posted by Legacy on 07/15/2002 12:00am

    Originally posted by: Christian Egging

    Hello

    Nice class, but you can not compile it with the .NET-Compiler.

    Reply
  • Transparency not working on XP

    Posted by Legacy on 06/13/2002 12:00am

    Originally posted by: Karu

    If I set the back color to CLR_NONE , I get a black back ground on windows XP.
    Any ideas ??

    Reply
  • Very nice!

    Posted by Legacy on 02/01/2002 12:00am

    Originally posted by: Adrian

    Your code helps me a great deal!
    Thank you!

    Reply
  • Great class - question

    Posted by Legacy on 01/16/2002 12:00am

    Originally posted by: Djibril

    Hello,

    This class is great. Thank you very much.

    I tried to use it with a CDateTimeCtrl but it is not working. Just trying to change background.
    Works fine for every else I tried to use it with.

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Stories about devastating cyberattacks are plaguing the news. Why? The DNS protocol is easy to exploit. See your network the way hackers do—as an easy target. Learn how you can effectively secure your DNS infrastructure today.

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds