Click here to Skip to main content
11,635,053 members (68,397 online)
Click here to Skip to main content

Colored/Blinking Controls and Dialogs with any Font

, 24 Aug 2000 CPOL 177K 3.8K 41
Rate this:
Please Sign up or sign in to vote.
The simplest way to change color, font or set blinking mode for any standard control
<!-- Download Links -->
  • Download source files - 7 Kb
  • Download demo project - 46 Kb
  • <!-- Article image -->

    Sample Image - CFCtrl.gif

    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.3

    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. You can also use it for the whole dialog (see "About" dialog in system menu).

    Usage:

    1. Include ColorCtrl.h in your project.
    2. Create a control with dialog editor.
    3. Add a member variable for this control using the class wizard.
    4. 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 the default color use CLR_DEFAULT as the argument. To set system colors use the macro CC_SYSCOLOR(index), where "index" is one of system color IDs (see help on ::GetSysColor). This macro doesn't call ::GetSysColor, but decorates the index for further usage.

    Use the following functions to change the background pattern:

    void SetBkPattern(UINT nResID);
    void SetBkPattern(HBITMAP);
    void SetBkBlinkPattern(UINT nResID1, UINT nResID2);
    void SetBkBlinkPattern(HBITMAP, HBITMAP);
    

    To remove a pattern use NULL as the argument.

    The HBITMAP argument should not be attached to any CBitmap class (Use CBitmap::Detach() to detach it). Don't call DeleteObject() for any HBITMAP you send to these functions. For Windows 9x the size of the bitmap is restricted to 8*8 pixels.

    Use the folowing functions to start/stop blinking:

    void StartBlink(int iWho, UINT nDelay);
    void StopBlink(int iWho);
    UINT GetDelay();
    

    The argument can be one of :

    • CC_BLINK_TEXT
    • CC_BLINK_BK
    • CC_BLINK_BOTH

    the 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

    If you want to use only Solid colors - call UseSolidColors(TRUE). If you have problems with transparency of the text background call ForceOpaque(), but take note: if you use a pattern or a non-solid color in low-color resolution you'll have a rectangle under text that is different from another background.

    There are two derived classes CColorCtrlEx and CBlinkCtrlEx that allow you to preset the control colors on a template level.

    e.g. CColorCtrlEx<CStatic, RGB(255,0,0), RGB(0,255,0)> m_static; will create a 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. This class supports combinations of the folowing styles:

    • Bold
    • Italic
    • Underline
    • Strikeout

    Usage:

    1. Include FontCtrl.h in your project.
    2. Create a control with the dialog editor.
    3. Add a member variable for this control using the class wizard.
    4. 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);
    

    The 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 the resulting font will have a combination of the styles and height specified in ChangeFontHeight (if not equal to zero).

    There is derived class CFontCtrlEx, that allow you to preset font style and height on a template level. E.g. CFontCtrlEx<CStatic, FC_FONT_BOLD|FC_FONT_UNDERLINE, 30> m_static; will create a static control with bold, underlined text and text height equal to 30.

    There are also four classes for basic styles: CBoldCtrl, CItalicCtrl, CUnderlineCtrl, CStrikeoutCtrl

    Warning! Don't use these five classes together with one control.

    If your control doesn't contain a font (e.g. you create it in code by a call to Create function) CFontCtrl cannot change font style/height. In this case use one of the SetFont functions to set the font to your control. If you create the control on base of one of five derived classes - don't worry - the predefined style/height will be added to selected font.

    Common Notes

    Any function of any class can be called even before the 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;
    

    Visit my Home Page

    License

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

    Share

    About the Author

    Yury Goltsman
    Software Developer (Senior)
    Israel Israel
    Yury is Software Engineer since 1988.
    His programming experience includes C#/VB.NET, WPF, C/C++(MFC/STL), Borland Delphi & C++ (VCL), JavaScript, HTML, CSS, XML, SQL, VB6, DirectX, Flash.
    He has worked on PCs (DOS/Win3.1-Vista) and PocketPCs (WinCE).

    Yury was born in Ukraine, but currently based in Jerusalem.

    You may also be interested in...

    Comments and Discussions

     
    QuestionDoes not work on VS 2013 Pin
    Member 114979784-Mar-15 2:40
    memberMember 114979784-Mar-15 2:40 
    GeneralPorting to 2008 Pin
    ElBuitre26-Apr-08 8:43
    memberElBuitre26-Apr-08 8:43 
    GeneralRe: Porting to 2008 Pin
    L94397318-Aug-08 2:15
    memberL94397318-Aug-08 2:15 
    GeneralI have an error when trying to build the demo program, could you help me to resolve it? [modified] Pin
    huannd26-Mar-08 0:15
    memberhuannd26-Mar-08 0:15 
    GeneralFix to compile under VS2005 Sp1 Pin
    baemayr3-Mar-08 10:59
    memberbaemayr3-Mar-08 10:59 
    GeneralRe: Fix to compile under VS2005 Sp1 Pin
    Yury Goltsman4-Mar-08 10:13
    memberYury Goltsman4-Mar-08 10:13 
    GeneralRe: Fix to compile under VS2005 Sp1 Pin
    baemayr4-Mar-08 10:50
    memberbaemayr4-Mar-08 10:50 
    Generalproblem with visual studio 2005 Pin
    Ing.Raiz824-Oct-07 3:18
    memberIng.Raiz824-Oct-07 3:18 
    GeneralRe: problem with visual studio 2005 Pin
    Ing.Raiz824-Oct-07 3:54
    memberIng.Raiz824-Oct-07 3:54 
    AnswerRe: problem with visual studio 2005 Pin
    Ing.Raiz825-Oct-07 3:51
    memberIng.Raiz825-Oct-07 3:51 
    GeneralDoesn&#8217;t support colored control Check Box in Push Button Mode. Pin
    RAlexR19-Aug-07 16:39
    memberRAlexR19-Aug-07 16:39 
    GeneralRe: Doesn&#8217;t support colored control Check Box in Push Button Mode. Pin
    Yury Goltsman20-Aug-07 21:42
    memberYury Goltsman20-Aug-07 21:42 
    QuestionWorks for embedded Visual C++? Pin
    ddonate23-Mar-06 23:12
    memberddonate23-Mar-06 23:12 
    Generalhelp.. Pin
    AmrutaM13-Mar-06 1:21
    memberAmrutaM13-Mar-06 1:21 
    GeneralVC8 Pin
    kelaklub12-Feb-06 17:51
    memberkelaklub12-Feb-06 17:51 
    GeneralRe: VC8 Pin
    philippef12-Dec-06 23:49
    memberphilippef12-Dec-06 23:49 
    GeneralWorked great in VC6, Thanks! Pin
    Catrick29-Nov-05 8:46
    memberCatrick29-Nov-05 8:46 
    GeneralNeed a simple way to change Back Color of CEdit &amp; CStatic controls Pin
    vikas amin23-Sep-05 2:19
    membervikas amin23-Sep-05 2:19 
    GeneralRe: Need a simple way to change Back Color of CEdit &amp; CStatic controls Pin
    Mircea Puiu23-Sep-05 2:47
    memberMircea Puiu23-Sep-05 2:47 
    GeneralChanging Cedit/Cstatic Pin
    jj92118-Oct-04 3:05
    memberjj92118-Oct-04 3:05 
    GeneralRe: Changing Cedit/Cstatic Pin
    Yury Goltsman20-Oct-04 9:19
    memberYury Goltsman20-Oct-04 9:19 
    GeneralChanging CStatic to CColorCtrl&lt;CStatic&gt; Pin
    jj92115-Oct-04 0:57
    memberjj92115-Oct-04 0:57 
    GeneralThx!!! Pin
    Michael L.5-Sep-04 11:49
    memberMichael L.5-Sep-04 11:49 
    GeneralChange textColor of disabled CEdit controls Pin
    Gregor Schiller19-Jul-04 6:00
    memberGregor Schiller19-Jul-04 6:00 
    GeneralRe: Change textColor of disabled CEdit controls Pin
    Yury Goltsman19-Jul-04 8:57
    memberYury Goltsman19-Jul-04 8:57 
    GeneralRe: Change textColor of disabled CEdit controls Pin
    JuSung Kim16-Jul-07 15:34
    memberJuSung Kim16-Jul-07 15:34 
    GeneralAttaching CFCtrl class to other class Pin
    Josiemaran28-Mar-04 16:17
    memberJosiemaran28-Mar-04 16:17 
    GeneralRe: Attaching CFCtrl class to other class Pin
    Yury Goltsman29-Mar-04 9:04
    memberYury Goltsman29-Mar-04 9:04 
    GeneralAdding Bitmap Pin
    Josiemaran28-Mar-04 15:34
    memberJosiemaran28-Mar-04 15:34 
    GeneralRe: Adding Bitmap Pin
    Yury Goltsman29-Mar-04 8:50
    memberYury Goltsman29-Mar-04 8:50 
    GeneralColor pushlike radio button Pin
    Josiemaran24-Mar-04 20:26
    memberJosiemaran24-Mar-04 20:26 
    GeneralRe: Color pushlike radio button Pin
    Yury Goltsman29-Mar-04 8:35
    memberYury Goltsman29-Mar-04 8:35 
    GeneralSupport for VS2003 (VC++7.1) added (read inside) Pin
    Yury Goltsman18-Feb-04 2:39
    memberYury Goltsman18-Feb-04 2:39 
    QuestionCan this be added to the View class......... Pin
    no_life1-Jan-04 11:40
    memberno_life1-Jan-04 11:40 
    AnswerRe: Can this be added to the View class......... Pin
    Yury Goltsman18-Feb-04 2:34
    memberYury Goltsman18-Feb-04 2:34 
    GeneralGreat! Thank you Pin
    Klaus Nowikow28-Nov-03 6:03
    memberKlaus Nowikow28-Nov-03 6:03 
    GeneralGood Job!, but ... Pin
    AntonDel25-Sep-03 1:04
    memberAntonDel25-Sep-03 1:04 
    GeneralRe: Good Job!, but ... Pin
    Yury Goltsman25-Sep-03 8:49
    memberYury Goltsman25-Sep-03 8:49 
    GeneralForceOpaque? Pin
    Ralph Shnelvar28-Sep-04 10:48
    memberRalph Shnelvar28-Sep-04 10:48 
    GeneralRe: ForceOpaque? Pin
    Yury Goltsman28-Sep-04 21:30
    memberYury Goltsman28-Sep-04 21:30 
    QuestionDoes it work with a CMonthCalCtrl? Pin
    VanHlebar14-Nov-02 14:01
    sussVanHlebar14-Nov-02 14:01 
    AnswerRe: Does it work with a CMonthCalCtrl? Pin
    Yury Goltsman23-Nov-02 9:42
    memberYury Goltsman23-Nov-02 9:42 
    GeneralChanges for .NET Pin
    Yury Goltsman12-Nov-02 9:21
    memberYury Goltsman12-Nov-02 9:21 
    GeneralMFC Classwizard Pin
    Brian Rhodes18-Sep-02 5:23
    memberBrian Rhodes18-Sep-02 5:23 
    GeneralRe: MFC Classwizard Pin
    NightBlade21-Sep-02 12:12
    memberNightBlade21-Sep-02 12:12 
    GeneralRe: MFC Classwizard Pin
    Yury Goltsman22-Sep-02 8:01
    memberYury Goltsman22-Sep-02 8:01 
    QuestionCould you update the demo project for the .NET-Compiler? Pin
    andy_lfx16-Jul-02 14:36
    sussandy_lfx16-Jul-02 14:36 
    AnswerRe: Could you update the demo project for the .NET-Compiler? Pin
    Yury Goltsman22-Sep-02 8:14
    memberYury Goltsman22-Sep-02 8:14 
    GeneralHelp VC7: Pin
    Bahe11-Nov-02 6:30
    sussBahe11-Nov-02 6:30 
    GeneralRe: Help VC7: Pin
    Yury Goltsman12-Nov-02 9:32
    memberYury Goltsman12-Nov-02 9:32 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    | Advertise | Privacy | Terms of Use | Mobile
    Web04 | 2.8.150728.1 | Last Updated 25 Aug 2000
    Article Copyright 2000 by Yury Goltsman
    Everything else Copyright © CodeProject, 1999-2015
    Layout: fixed | fluid