A Color Check Box Class | CodeGuru

A Color Check Box Class

–> Environment: VC++ 6 This class resulted out of the lack in changing background color of checkboxes with ordinary MFC methods. It provides the possibility to change the color at any time from within your project. To use it please do the following: Insert the files CColorCheck.cpp and CColorCheck.h in your project Include CColorCheck.h in […]

Written By
CodeGuru Staff
CodeGuru Staff
Jan 28, 2002
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

–>

Environment: VC++ 6

This class resulted out of the lack in changing background color of checkboxes with ordinary MFC methods. It provides the possibility to change the color at any time from within your project.

To use it please do the following:

  1. Insert the files CColorCheck.cpp and CColorCheck.h in your project
  2. Include CColorCheck.h in your dialog header file YourFileDlg.h
    #include "CColorCheck.h"
  3. Add the following code right after the includes:
    class CColorCheck;
  4. Use the Wizard to insert a checkbox in your dialog box.
  5. The check box must be of type OwnerDraw but you won’t find this option in the properties dialog box.

    Open your resource file and look for the definition of the checkbox which will look something like:

    CONTROL  "Check2", IDC_CHECK2, "Button",
                       BS_AUTOCHECKBOX | BS_OWNERDRAW
                       WS_TABSTOP,51,126,41,10

    and insert the option BS_OWNERDRAW as shown above. When you go back to your dialog box you will see that the checkbox now looks like a normal button.

  6. Open the Class Wizard and add a member variable of category “Control” and variable type “CColorCheck” for each CheckBox you want to use. (i.e. m_check1…m_checkX)
  7. Use the member variable to access the methods of the class.

    Overriden Methods are:

       SetCheck(int check) = Sets the button state to checked.
       GetCheck()= Retrieves the button’s state.
       SetBkColor(COLORREF newColor)= Sets the specified color as the background for the check box.

    Other Methods are:

       SetArrowColor(COLORREF newColor)= Sets the Checked-Arrow color.
       SetMyTextColor(COLORREF txtColor)= Sets the caption text color.

  8. Add the ON_BN_CLICKED message handler for each control. This will look like this:

    in YourFileDlg.h:

    afx_msg void OnYourCheckbox();

    in YourFileDlg.cpp:

    ON_BN_CLICKED(IDC_CHECK1, OnYourCheckbox)
    .....
       void CYourDlgClass::OnYourCheckbox()
       {
          m_check1.Toggle();
       }

Downloads

Download source – 3 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.