Click here to Skip to main content
15,881,880 members
Articles / Desktop Programming / MFC
Article

Colored/Blinking Controls and Dialogs with any Font

Rate me:
Please Sign up or sign in to vote.
4.91/5 (17 votes)
24 Aug 2000CPOL 229.8K   5.1K   42   67
The simplest way to change color, font or set blinking mode for any standard control
  • Download source files - 7 Kb
  • Download demo project - 46 Kb
  • 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)


    Written By
    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.

    Comments and Discussions

     
    GeneralRe: Help VC7: Pin
    Yury Goltsman12-Nov-02 9:32
    Yury Goltsman12-Nov-02 9:32 
    GeneralFixed: Help VC7: Pin
    BAHE13-Nov-02 22:48
    BAHE13-Nov-02 22:48 
    GeneralRe: Fixed: Help VC7: Pin
    Yury Goltsman14-Nov-02 10:18
    Yury Goltsman14-Nov-02 10:18 
    GeneralTransparent Group box and Static Text Pin
    George Clarence23-Jan-02 19:14
    George Clarence23-Jan-02 19:14 
    GeneralProblem with VS.NET beta 2 Pin
    Guyver1-Jan-02 5:27
    Guyver1-Jan-02 5:27 
    GeneralRe: Problem with VS.NET beta 2 Pin
    8-Jul-02 2:05
    suss8-Jul-02 2:05 
    GeneralNot Working with a Button Pin
    18-Oct-01 1:54
    suss18-Oct-01 1:54 
    GeneralRe: Not Working with a Button Pin
    Yury Goltsman18-Dec-01 3:04
    Yury Goltsman18-Dec-01 3:04 
    GeneralNot Working with a Button Pin
    18-Oct-01 1:53
    suss18-Oct-01 1:53 
    GeneralDoes not work with CCheckListBox Pin
    28-Sep-01 0:24
    suss28-Sep-01 0:24 
    GeneralRe: Does not work with CCheckListBox Pin
    Yury Goltsman18-Dec-01 2:58
    Yury Goltsman18-Dec-01 2:58 
    QuestionHow to write to author? Pin
    12-Dec-00 8:43
    suss12-Dec-00 8:43 
    GeneralCDateTimeCtrl Pin
    J. De Mulder27-Aug-00 1:59
    professionalJ. De Mulder27-Aug-00 1:59 
    GeneralRe: CDateTimeCtrl Pin
    Yury Goltsman28-Aug-00 1:52
    Yury Goltsman28-Aug-00 1:52 
    Generalcolored/blinking controls and dialogs... Pin
    Leon Ungier25-May-00 12:59
    sussLeon Ungier25-May-00 12:59 
    GeneralIf a job's not worth doing at all, it's not worth doing well. Pin
    suaverico11-Apr-00 6:00
    suaverico11-Apr-00 6:00 
    GeneralIf a remark's not worth saying at all, it's not worth saying well. Pin
    Felix Rivera25-Aug-00 4:19
    Felix Rivera25-Aug-00 4:19 

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

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