แก้ไข

แชร์ผ่าน


Setting the Images for an Individual Item

The different types of images used by the extended combo box item are determined by the values in the iImage, iSelectedImage, and iOverlay members of the COMBOBOXEXITEM structure. Each value is the index of an image in the associated image list of the control. By default, these members are set to 0, causing the control to display no image for the item. If you want to use images for a specific item, you can modify the structure accordingly, either when inserting the combo box item or by modifying an existing combo box item.

Setting the Image for a New Item

If you are inserting a new item, initialize the iImage, iSelectedImage, and iOverlay structure members with the proper values and then insert the item with a call to CComboBoxEx::InsertItem.

The following example inserts a new extended combo box item (cbi) into the extended combo box control (m_comboEx), supplying indices for all three image states:

COMBOBOXEXITEM     cbi = { 0 };
COMBOBOXEXITEM     cbi = { 0 };
CString            str;
int                nItem;

cbi.mask = CBEIF_IMAGE | CBEIF_INDENT | CBEIF_OVERLAY |
CBEIF_SELECTEDIMAGE | CBEIF_TEXT;

cbi.iItem = 0;
cbi.pszText = _T("Item 0");
cbi.iImage = 0;
cbi.iSelectedImage = 1;
cbi.iOverlay = 2;
cbi.iIndent = (0 & 0x03);   //Set indentation according
                     //to item position

nItem = m_ComboBoxEx.InsertItem(&cbi);
ASSERT(nItem == 0);

Setting the Image for an Existing Item

If you are modifying an existing item, you need to work with the mask member of a COMBOBOXEXITEM structure.

To modify an existing item to use images

  1. Declare a COMBOBOXEXITEM structure and set the mask data member to the values you are interested in modifying.

  2. Using this structure, make a call to CComboBoxEx::GetItem.

  3. Modify the mask, iImage, and iSelectedImage members of the newly returned structure, using the appropriate values.

  4. Make a call to CComboBoxEx::SetItem, passing in the modified structure.

The following example demonstrates this procedure by swapping the selected and unselected images of the third extended combo box item:

COMBOBOXEXITEM cbi = {0};
int iImageTemp;

cbi.mask = CBEIF_IMAGE | CBEIF_SELECTEDIMAGE;
cbi.iItem = 0;
m_ComboBoxEx.GetItem(&cbi);

iImageTemp = cbi.iImage;
cbi.iImage = cbi.iSelectedImage;
cbi.iSelectedImage = iImageTemp;
VERIFY(m_ComboBoxEx.SetItem(&cbi));

See also

Using CComboBoxEx
Controls