Comment préparer les images et les éléments ComboBoxEx
Cette rubrique montre comment ajouter des éléments à un contrôle ComboBoxEx.
Pour ajouter un élément à un contrôle ComboBoxEx, commencez par définir une structure COMBOBOXEXITEM . Ensuite, définissez le membre de masque de la structure pour indiquer les membres que le contrôle doit utiliser. Enfin, définissez les membres spécifiés de la structure sur les valeurs souhaitées et envoyez le message CBEM _ INSERTITEM pour ajouter l’élément au contrôle.
La fonction définie par l’application suivante ajoute 15 éléments à un contrôle ComboBoxEx existant.
Bon à savoir
Technologies
Prérequis
- C/C++
- Windows Programmation de l’interface utilisateur
Instructions
Étape 1 :
Pour ajouter un élément à un contrôle ComboBoxEx, commencez par définir une structure COMBOBOXEXITEM .
COMBOBOXEXITEM cbei;
int iCnt;
typedef struct {
int iImage;
int iSelectedImage;
int iIndent;
LPTSTR pszText;
} ITEMINFO, *PITEMINFO;
ITEMINFO IInf[ ] = {
{ 0, 3, 0, L"first"},
{ 1, 4, 1, L"second"},
{ 2, 5, 2, L"third"},
{ 0, 3, 0, L"fourth"},
{ 1, 4, 1, L"fifth"},
{ 2, 5, 2, L"sixth"},
{ 0, 3, 0, L"seventh"},
{ 1, 4, 1, L"eighth"},
{ 2, 5, 2, L"ninth"},
{ 0, 3, 0, L"tenth"},
{ 1, 4, 1, L"eleventh"},
{ 2, 5, 2, L"twelfth"},
{ 0, 3, 0, L"thirteenth"},
{ 1, 4, 1, L"fourteenth"},
{ 2, 5, 2, L"fifteenth"}
};
Étape 2 :
Définissez le membre de masque de la structure pour indiquer les membres que le contrôle doit utiliser. Notez que le membre Mask de la structure COMBOBOXEXITEM comprend des valeurs d’indicateur qui indiquent au contrôle d’afficher des images pour chaque élément.
// Set the mask common to all items.
cbei.mask = CBEIF_TEXT | CBEIF_INDENT |
CBEIF_IMAGE| CBEIF_SELECTEDIMAGE;
Étape 3 :
Définissez les membres spécifiés de la structure sur les valeurs souhaitées, puis envoyez le message CBEM _ INSERTITEM pour ajouter l’élément au contrôle.
for(iCnt=0;iCnt<MAX_ITEMS;iCnt++){
// Initialize the COMBOBOXEXITEM struct.
cbei.iItem = iCnt;
cbei.pszText = IInf[iCnt].pszText;
cbei.cchTextMax = sizeof(IInf[iCnt].pszText);
cbei.iImage = IInf[iCnt].iImage;
cbei.iSelectedImage = IInf[iCnt].iSelectedImage;
cbei.iIndent = IInf[iCnt].iIndent;
// Tell the ComboBoxEx to add the item. Return FALSE if
// this fails.
if(SendMessage(hwndCB,CBEM_INSERTITEM,0,(LPARAM)&cbei) == -1)
return FALSE;
Étape 4 :
Assignez la liste d’images existante au contrôle ComboBoxEx et définissez la taille du contrôle.
// Assign the existing image list to the ComboBoxEx control
// and return TRUE.
// g_himl is the handle to the existing image list
SendMessage(hwndCB,CBEM_SETIMAGELIST,0,(LPARAM)g_himl);
// Set size of control to make sure it's displayed correctly now
// that the image list is set.
SetWindowPos(hwndCB,NULL,20,20,250,120,SWP_NOACTIVATE);
return TRUE;
Exemple complet
// AddItems - Uses the CBEM_INSERTITEM message to add items to an
// existing ComboBoxEx control.
BOOL WINAPI AddItems(HWND hwndCB)
{
// Declare and init locals.
COMBOBOXEXITEM cbei;
int iCnt;
typedef struct {
int iImage;
int iSelectedImage;
int iIndent;
LPTSTR pszText;
} ITEMINFO, *PITEMINFO;
ITEMINFO IInf[ ] = {
{ 0, 3, 0, L"first"},
{ 1, 4, 1, L"second"},
{ 2, 5, 2, L"third"},
{ 0, 3, 0, L"fourth"},
{ 1, 4, 1, L"fifth"},
{ 2, 5, 2, L"sixth"},
{ 0, 3, 0, L"seventh"},
{ 1, 4, 1, L"eighth"},
{ 2, 5, 2, L"ninth"},
{ 0, 3, 0, L"tenth"},
{ 1, 4, 1, L"eleventh"},
{ 2, 5, 2, L"twelfth"},
{ 0, 3, 0, L"thirteenth"},
{ 1, 4, 1, L"fourteenth"},
{ 2, 5, 2, L"fifteenth"}
};
// Set the mask common to all items.
cbei.mask = CBEIF_TEXT | CBEIF_INDENT |
CBEIF_IMAGE| CBEIF_SELECTEDIMAGE;
for(iCnt=0;iCnt<MAX_ITEMS;iCnt++){
// Initialize the COMBOBOXEXITEM struct.
cbei.iItem = iCnt;
cbei.pszText = IInf[iCnt].pszText;
cbei.cchTextMax = sizeof(IInf[iCnt].pszText);
cbei.iImage = IInf[iCnt].iImage;
cbei.iSelectedImage = IInf[iCnt].iSelectedImage;
cbei.iIndent = IInf[iCnt].iIndent;
// Tell the ComboBoxEx to add the item. Return FALSE if
// this fails.
if(SendMessage(hwndCB,CBEM_INSERTITEM,0,(LPARAM)&cbei) == -1)
return FALSE;
}
// Assign the existing image list to the ComboBoxEx control
// and return TRUE.
// g_himl is the handle to the existing image list
SendMessage(hwndCB,CBEM_SETIMAGELIST,0,(LPARAM)g_himl);
// Set size of control to make sure it's displayed correctly now
// that the image list is set.
SetWindowPos(hwndCB,NULL,20,20,250,120,SWP_NOACTIVATE);
return TRUE;
}