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:
The
CreateCheckboxImageList function creates an imagelist of
16 images, as defined by this
enum
:
enum { COLD_UNUSED_1 = 0,
COLD_UNCHECKED_NORMAL,
COLD_CHECKED_NORMAL,
COLD_TRISTATE_NORMAL,
COLD_UNUSED_2,
COLD_UNCHECKED_DISABLED,
COLD_CHECKED_DISABLED,
COLD_TRISTATE_DISABLED,
HOT_UNUSED_1,
HOT_UNCHECKED_NORMAL,
HOT_CHECKED_NORMAL,
HOT_TRISTATE_NORMAL,
HOT_UNUSED_2,
HOT_UNCHECKED_DISABLED,
HOT_CHECKED_DISABLED,
HOT_TRISTATE_DISABLED };
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;
imagelist.Attach(hil);
CreateCheckboxImageList Demo
The
CreateCheckboxImageList demo app displays four imagelists of
different sizes:
When
Enable Visual Themes checkbox is unchecked, checkboxes
are created without visual themes:
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
Version 1.0 - 2007 July 15
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.