Click here to Skip to main content
12,352,091 members (59,592 online)
Click here to Skip to main content
Add your own
alternative version

Stats

145.9K views
1.8K downloads
92 bookmarked
Posted

CreateCheckboxImageList - a non-MFC function to create an imagelist of themed checkboxes

, 15 Apr 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
CreateCheckboxImageList is a function that creates an imagelist of themed or non-themed checkboxes, suitable for use in tree controls, list controls, and list boxes.

Introduction

Checkbox images are often useful in situations such as tree controls and list controls, where using an actual checkbox control would be difficult or would result in creation of large number of child controls. I have used an earlier version of CreateCheckboxImageList in my XHtmlTree article. In this version I have removed MFC dependencies and added option to specify size of checkbox images. I am indebted to David Yuheng Zhao for his excellent Visual Style article, which I used as basis for uxtheme.dll wrapper.

CreateCheckboxImageList API

Here is the CreateCheckboxImageList function:
//=============================================================================
//
// CreateCheckboxImageList()
//
// Purpose:     Create themed checkbox image list
//
// Parameters:  hDC              - device context for drawing
//              nSize            - height and width of images
//              crBackground     - fill color
//              bUseVisualThemes - TRUE = use themes if enabled
//
// Returns:     HIMAGELIST       - imagelist handle; NULL if create fails
//
The CreateCheckboxImageList function creates an imagelist of 16 images, as defined by this enum:
enum { COLD_UNUSED_1 = 0,        // 0000
       COLD_UNCHECKED_NORMAL,    // 0001
       COLD_CHECKED_NORMAL,      // 0010
       COLD_TRISTATE_NORMAL,     // 0011

       COLD_UNUSED_2,            // 0100
       COLD_UNCHECKED_DISABLED,  // 0101
       COLD_CHECKED_DISABLED,    // 0110
       COLD_TRISTATE_DISABLED,   // 0111

       HOT_UNUSED_1,             // 1000
       HOT_UNCHECKED_NORMAL,     // 1001
       HOT_CHECKED_NORMAL,       // 1010
       HOT_TRISTATE_NORMAL,      // 1011

       HOT_UNUSED_2,             // 1100
       HOT_UNCHECKED_DISABLED,   // 1101
       HOT_CHECKED_DISABLED,     // 1110
       HOT_TRISTATE_DISABLED };  // 1111
The first 8 images are "cold", the next 8 "hot". To switch from cold to hot, you can OR the state with 8. To switch from normal to disabled, you can OR the state with 4.

Using CreateCheckboxImageList in an MFC App

It is easy to use CreateCheckboxImageList with MFC by simply attaching the HIMAGELIST to a CImageList:
HIMAGELIST hil = 0;
hil = HDCheckboxImageList::CreateCheckboxImageList(pDC->m_hDC, 16,
        GetSysColor(COLOR_WINDOW), TRUE);
CImageList imagelist;    // usually this will be class variable
imagelist.Attach(hil);

CreateCheckboxImageList Demo

The CreateCheckboxImageList demo app displays four imagelists of different sizes:

screenshot

When Enable Visual Themes checkbox is unchecked, checkboxes are created without visual themes:

screenshot

How to use

Step 1 - Add Files

To integrate CreateCheckboxImageList into your app, you first need to add following files to your project:

  • CreateCheckboxImageList.cpp
  • CreateCheckboxImageList.h
  • CXDC.h
  • XVisualStyles.h

The .cpp file should be set to Not using precompiled header in Visual Studio. Otherwise, you will get error

fatal error C1010: unexpected end of file while looking for precompiled header directive

Step 2 - Add Header File to Your Source Module

In the module where you want to use CreateCheckboxImageList, include header file CreateCheckboxImageList.h .

Step 3 - Add Code

In OnInitDialog or other initialization routine, add code to create imagelist, and then draw appropriate image in DrawItem() or OnPaint() function.
The file XVisualStyles.h includes header files uxtheme.h and tmschema.h. These files are found in Windows Platform SDK. Normally, at this point I would insert the download url for Platform SDK, but its location changes often, so it's best to let google find it for you: http://www.google.com/search?q=%22platform+sdk%22.

Revision History

Version 1.1 - 2008 April 15

  • Initial public release

Version 1.0 - 2007 July 15

  • Embedded in XHtmlTree

Usage

This software is released into the public domain. You are free to use it in any way you like, except that you may not sell this source code. If you modify it or extend it, please to consider posting new code here for everyone to share. This software is provided "as is" with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.

License

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

Share

About the Author

Hans Dietrich
Software Developer (Senior) Hans Dietrich Software
United States United States
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.

Recently, I have moved to Los Angeles where I am doing consulting and development work.

For consulting and custom software development, please see www.hdsoft.org.






You may also be interested in...

Comments and Discussions

 
QuestionTried in an sample mfc application to set header checkbox. Header chekbox is set but alignment is not proper only in xp? Pin
sdileep112-Aug-15 1:22
membersdileep112-Aug-15 1:22 
QuestionNo CheckBoxes drawn with VS2010 RC1 Pin
pscholl17-Feb-10 3:33
memberpscholl17-Feb-10 3:33 
AnswerRe: No CheckBoxes drawn with VS2010 RC1 Pin
Hans Dietrich17-Feb-10 3:59
mentorHans Dietrich17-Feb-10 3:59 
AnswerRe: No CheckBoxes drawn with VS2010 RC1 Pin
pscholl18-Feb-10 0:48
memberpscholl18-Feb-10 0:48 
GeneralRe: No CheckBoxes drawn with VS2010 RC1 Pin
Hans Dietrich18-Feb-10 4:46
mentorHans Dietrich18-Feb-10 4:46 
GeneralRe: No CheckBoxes drawn with VS2010 RC1 Pin
pscholl18-Feb-10 6:34
memberpscholl18-Feb-10 6:34 
QuestionHave you ever tried to use the CXVisualStyles on Windows 2000? Pin
poxui30-Sep-08 12:49
memberpoxui30-Sep-08 12:49 
QuestionOut of sincere interest... Pin
Johann Gerell16-Apr-08 20:37
memberJohann Gerell16-Apr-08 20:37 
GeneralRe: Out of sincere interest... Pin
Hans Dietrich16-Apr-08 23:37
mvpHans Dietrich16-Apr-08 23:37 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160621.1 | Last Updated 15 Apr 2008
Article Copyright 2008 by Hans Dietrich
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid