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:

  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


Comments

  • Thank you

    Posted by madwoodster on 10/05/2007 11:25am

    Just a note to say thank you both to the author and those who made comments, as this was just what I was looking for.

    Reply
  • Toggle()

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

    Originally posted by: rookie

    I got an error :'Toggle' : is not a member of 'CColorCheck'
    Does Toggle is sudo code? or you did put this member function with the source?

    • simple...

      Posted by atiadi on 09/22/2004 05:06am

      void CColorCheck::Toggle()
      {
      	if 	(checkFlag == 1)
      		checkFlag = 0;
      	else
      		checkFlag = 1;
      }

      Reply
    Reply
  • Good effort . . .

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

    Originally posted by: Santosh Deshmukh

    Pretty kool. Good effort in implementing and explaining as well. One small confusing thing is that of the classwizard, a small note expected.

    Note : You need to go to the dlg header file and manually change the checkbox variable type from CButton to CColorCheck.

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

Top White Papers and Webcasts

  • Live Event Date: September 16, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you starting an on-premise-to-cloud data migration project? Have you thought about how much space you might need for your online platform or how to handle data that might be related to users who no longer exist? If these questions or any other concerns have been plaguing you about your migration project, check out this eSeminar. Join our speakers Betsy Bilhorn, VP, Product Management at Scribe, Mike Virnig, PowerSucess Manager and Michele …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds