Customized Check Boxes

    

Environment: VC6 SP4, Win 2000 SP2

Owner-Drawn push buttons have been well covered until now, but what about check boxes?

At first I was surprised that, although being one of the basic controls under Windows, I couldn't find anything useful about customized check boxes. I soon realised why: Windows doesn't support Owner-Drawn check boxes. You can assign the BS_OWNERDRAW style to them, but this won't be any help. Then you would receive WM_DRAWITEM messages for the default, pushed, and disabled states, just like for an ordinary Owner-Drawn push button.

A check box, however, has more states; first, it can be checked or unchecked; then, the left mouse button or space bar can be pressed in any of the basic states; and, it can also be disabled in any of them. That's six different states. So, is it impossible to make customized check boxes? No, the fact that we can't rely on WM_DRAWITEM only means that there will be more work for you. You have to determine yourself when to redraw the check box and in which state to do it. But that is where CCheckBox comes in: It does all this for you internally. You only have to provide a bitmap with all six possible check mark states to it.

About CCheckBox

CCheckBox is a class derived from the MFC CButton class. Besides CTor and Dtor, it only has one public member:

static bool drawFocus;  // true  => draw focus rectangle (default)
                        // false => don't draw focus rectangle

How to Integrate CCheckBox into Your Application

  1. Prepare a bitmap with all six possible check mark states. RGB(255,0,255) is the transparency color.
  2. Declare a CCheckBox member variable in your CDialog derive class:

    CCheckBox   m_cb_1;
  3. Call the CCheckBox's constructor from the constructor of your dialog:

    CYourDialog::CYourDialog () :
        m_cb_1 (IDB_STATES, 13)
    { }      // IDB_STATES: ID of bitmap resource,
             // prepared in step 1
             // 13: dimension (width=height) of check mark
    
  4. Now, attach the check box to CCheckBox, either in OnInitDialog:

    // Subclass IDC_CBOX (= ID of check box)
    m_cb_1.SubclassDlgItem(ID_CBOX, this);
    
    or in DoDataExchange:

    // Subclass IDC_CBOX
    DDX_Control(pDX, IDC_CBOX, m_cb_1);
    

Downloads

SDK Downloads *

Download demo project - 28 Kb
Download source - 8 Kb
SDK demo project - 16 Kb
SDK demo application - 16 Kb


* Under SDK Downloads you can find another example of CCheckBox, made with pure Windows APIs. For usage (either in SDK or in interaction with MFC), see the accompanying "howto.html" file or the SDK demo project. However, the interface (public methods) of this class is almost the same as in the one presented above.
Just take a look at how you could subclass controls in an object-oriented way—without any MFC!



Comments

  • Good Job!!! another way!

    Posted by Legacy on 01/26/2003 12:00am

    Originally posted by: Tom

    U do a good job! here I do another way!

    =======================================
    SkinMagic SDK Library for C/C++
    Better solution for skinnable program
    http://www.appspeed.com/
    =======================================

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

Top White Papers and Webcasts

  • Hurricane Sandy was one of the most destructive natural disasters that the United States has ever experienced. Read this success story to learn how Datto protected its partners and their customers with proactive business continuity planning, heroic employee efforts, and the right mix of technology and support. With storm surges over 12 feet, winds that exceeded 90 mph, and a diameter spanning more than 900 miles, Sandy resulted in power outages to approximately 7.5 million people, and caused an estimated $50 …

  • You probably have several goals for your patient portal of choice. Is "community" one of them? With a bevy of vendors offering portal solutions, it can be challenging for a hospital to know where to start. Fortunately, YourCareCommunity helps ease the decision-making process. Read this white paper to learn more. "3 Ways Clinicians can Leverage a Patient Portal to Craft a Healthcare Community" is a published document owned by www.medhost.com

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds