CCheckListBox Class

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at CCheckListBox Class.

Provides the functionality of a Windows checklist box.

class CCheckListBox : public CListBox  

Public Constructors

NameDescription
CCheckListBox::CCheckListBoxConstructs a CCheckListBox object.

Public Methods

NameDescription
CCheckListBox::CreateCreates the Windows checklist box and attaches it to the CCheckListBox object.
CCheckListBox::DrawItemCalled by the framework when a visual aspect of an owner-draw list box changes.
CCheckListBox::EnableEnables or disables a checklist box item.
CCheckListBox::GetCheckGets the state of an item's check box.
CCheckListBox::GetCheckStyleGets the style of the control's check boxes.
CCheckListBox::IsEnabledDetermines whether an item is enabled.
CCheckListBox::MeasureItemCalled by the framework when a list box with an owner-draw style is created.
CCheckListBox::OnGetCheckPositionCalled by the framework to get the position of an item's check box.
CCheckListBox::SetCheckSets the state of an item's check box.
CCheckListBox::SetCheckStyleSets the style of the control's check boxes.

A "checklist box" displays a list of items, such as filenames. Each item in the list has a check box next to it that the user can check or clear.

CCheckListBox is only for owner-drawn controls because the list contains more than text strings. At its simplest, a checklist box contains text strings and check boxes, but you do not need to have text at all. For example, you could have a list of small bitmaps with a check box next to each item.

To create your own checklist box, you must derive your own class from CCheckListBox. To derive your own class, write a constructor for the derived class, then call Create.

If you want to handle Windows notification messages sent by a list box to its parent (usually a class derived from CDialog), add a message-map entry and message-handler member function to the parent class for each message.

Each message-map entry takes the following form:

ON_Notification (id, memberFxn)

where id specifies the child window ID of the control sending the notification and memberFxn is the name of the parent member function you have written to handle the notification.

The parent's function prototype is as follows:

afx_msg void memberFxn ( );

There is only one message-map entry that pertains specifically to CCheckListBox (but see also the message-map entries for CListBox):

  • ON_CLBN_CHKCHANGE The user has changed the state of an item's checkbox.

If your checklist box is a default checklist box (a list of strings with the default-sized checkboxes to the left of each), you can use the default CCheckListBox::DrawItem to draw the checklist box. Otherwise, you must override the CListBox::CompareItem function and the CCheckListBox::DrawItem and CCheckListBox::MeasureItem functions.

You can create a checklist box either from a dialog template or directly in your code.

CObject

CCmdTarget

CWnd

CListBox

CCheckListBox

Header: afxwin.h

Constructs a CCheckListBox object.

CCheckListBox();

Remarks

You construct a CCheckListBox object in two steps. First define a class derived from CCheckListBox, then call Create, which initializes the Windows checklist box and attaches it to the CCheckListBox object.

Example

   CCheckListBox myCheckListBox;
   myCheckListBox.Create(LBS_HASSTRINGS | LBS_OWNERDRAWFIXED, 
      CRect(10, 10, 100, 100), this, IDC_MYCHECKLISTBOX);

Creates the Windows checklist box and attaches it to the CCheckListBox object.

virtual BOOL Create(
    DWORD dwStyle,  
    const RECT& rect,  
    CWnd* pParentWnd,  
    UINT nID);

Parameters

dwStyle
Specifies the style of the checklist box. The style must be LBS_HASSTRINGS and either LBS_OWNERDRAWFIXED (all items in the list are the same height) or LBS_OWNERDRAWVARIABLE (items in the list are of varying heights). This style can be combined with other list-box styles except LBS_USETABSTOPS.

rect
Specifies the checklist-box size and position. Can be either a CRect object or a RECT structure.

pParentWnd
Specifies the checklist box's parent window (usually a CDialog object). It must not be NULL.

nID
Specifies the checklist box's control ID.

Return Value

Nonzero if successful; otherwise 0.

Remarks

You construct a CCheckListBox object in two steps. First, define a class derived from CcheckListBox and then call Create, which initializes the Windows checklist box and attaches it to the CCheckListBox. See CCheckListBox::CCheckListBox for a sample.

When Create executes, Windows sends the WM_NCCREATE, WM_CREATE, WM_NCCALCSIZE, and WM_GETMINMAXINFO messages to the checklist-box control.

These messages are handled by default by the OnNcCreate, OnCreate, OnNcCalcSize, and OnGetMinMaxInfo member functions in the CWnd base class. To extend the default message handling, add a message map to the your derived class and override the preceding message-handler member functions. Override OnCreate, for example, to perform needed initialization for a new class.

Apply the following window styles to a checklist-box control:

  • WS_CHILD Always

  • WS_VISIBLE Usually

  • WS_DISABLED Rarely

  • WS_VSCROLL To add a vertical scroll bar

  • WS_HSCROLL To add a horizontal scroll bar

  • WS_GROUP To group controls

  • WS_TABSTOP To allow tabbing to this control

Called by the framework when a visual aspect of an owner-drawn checklist box changes.

virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);

Parameters

lpDrawItemStruct
A long pointer to a DRAWITEMSTRUCT structure that contains information about the type of drawing required.

Remarks

The itemAction and itemState members of the DRAWITEMSTRUCT structure define the drawing action that is to be performed.

By default, this function draws a default checkbox list, consisting of a list of strings each with a default-sized checkbox to the left. The checkbox list size is the one specified in Create.

Override this member function to implement drawing of owner-draw checklist boxes that are not the default, such as checklist boxes with lists that aren't strings, with variable-height items, or with checkboxes that aren't on the left. The application should restore all graphics device interface (GDI) objects selected for the display context supplied in lpDrawItemStruct before the termination of this member function.

If checklist box items are not all the same height, the checklist box style (specified in Create) must be LBS_OWNERVARIABLE, and you must override the MeasureItem function.

Call this function to enable or disable a checklist box item.

void Enable(
    int nIndex,  
    BOOL bEnabled = TRUE);

Parameters

nIndex
Index of the checklist box item to be enabled.

bEnabled
Specifies whether the item is enabled or disabled.

Retrieves the state of the specified check box.

int GetCheck(int nIndex);

Parameters

nIndex
Zero-based index of a check box that is contained in the list box.

Return Value

The state of the specified check box. The following table lists possible values.

ValueDescription
BST_CHECKEDThe check box is checked.
BST_UNCHECKEDThe check box is not checked.
BST_INDETERMINATEThe check box state is indeterminate.

Call this function to get the checklist box's style.

UINT GetCheckStyle();

Return Value

The style of the control's check boxes.

Remarks

For information on possible styles, see SetCheckStyle.

Call this function to determine whether an item is enabled.

BOOL IsEnabled(int nIndex);

Parameters

nIndex
Index of the item.

Return Value

Nonzero if the item is enabled; otherwise 0.

Called by the framework when a checklist box with a nondefault style is created.

virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);

Parameters

lpMeasureItemStruct
A long pointer to a MEASUREITEMSTRUCT structure.

Remarks

By default, this member function does nothing. Override this member function and fill in the MEASUREITEMSTRUCT structure to inform Windows of the dimensions of checklist-box items. If the checklist box is created with the LBS_OWNERDRAWVARIABLE style, the framework calls this member function for each item in the list box. Otherwise, this member is called only once.

The framework calls this function to get the position and size of the check box in an item.

virtual CRect OnGetCheckPosition(
    CRect rectItem,  
    CRect rectCheckBox);

Parameters

rectItem
The position and size of the list item.

rectCheckBox
The default position and size of an item's check box.

Return Value

The position and size of an item's check box.

Remarks

The default implementation only returns the default position and size of the check box ( rectCheckBox). By default, a check box is aligned in the upper-left corner of an item and is the standard check box size. There may be cases where you want the check boxes on the right, or want a larger or smaller check box. In these cases, override OnGetCheckPosition to change the check box position and size within the item.

Sets the state of the specified check box.

void SetCheck(
    int nIndex,  
    int nCheck);

Parameters

nIndex
Zero-based index of a check box that is contained in the list box.

nCheck
The button state for the specified check box. See the Remarks section for possible values.

Remarks

The following table lists possible values for the nCheck parameter.

ValueDescription
BST_CHECKEDSelect the specified check box.
BST_UNCHECKEDClear the specified check box.
BST_INDETERMINATESet the specified check box state to indeterminate.

This state is only available if the check box style is BS_AUTO3STATE or BS_3STATE. For more information, see Button Styles.

Call this function to set the style of check boxes in the checklist box.

void SetCheckStyle(UINT nStyle);

Parameters

nStyle
Determines the style of check boxes in the checklist box.

Remarks

Valid styles are:

  • BS_CHECKBOX

  • BS_AUTOCHECKBOX

  • BS_AUTO3STATE

  • BS_3STATE

For information on these styles, see Button Styles.

MFC Sample TSTCON
CListBox Class
Hierarchy Chart
CListBox Class

Show: