Come preparare elementi e immagini ComboBoxEx
In questo argomento viene illustrato come aggiungere elementi a un controllo ComboBoxEx.
Per aggiungere un elemento a un controllo ComboBoxEx, definire innanzitutto una struttura COMBOBOXEXITEM. Impostare quindi il membro mask della struttura per indicare quali membri si desidera utilizzare il controllo. Impostare infine i membri specificati della struttura sui valori desiderati e inviare il messaggio CBEM_INedizione Standard RTITEM per aggiungere l'elemento al controllo.
La funzione definita dall'applicazione seguente aggiunge 15 elementi a un controllo ComboBoxEx esistente.
Informazioni importanti
Tecnologie
Prerequisiti
- C/C++
- Programmazione dell'interfaccia utente di Windows
Istruzioni
Passaggio 1:
Per aggiungere un elemento a un controllo ComboBoxEx, definire innanzitutto una struttura 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"}
};
Passaggio 2:
Impostare il membro mask della struttura per indicare quali membri si desidera utilizzare il controllo. Si noti che il membro mask della struttura COMBOBOXEXITEM include valori di flag che indicano al controllo di visualizzare immagini per ogni elemento.
// Set the mask common to all items.
cbei.mask = CBEIF_TEXT | CBEIF_INDENT |
CBEIF_IMAGE| CBEIF_SELECTEDIMAGE;
Passaggio 3:
Impostare i membri specificati della struttura sui valori desiderati e quindi inviare il messaggio CBEM_INedizione Standard RTITEM per aggiungere l'elemento al controllo.
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;
Passaggio 4:
Assegnare l'elenco di immagini esistente al controllo ComboBoxEx e impostare le dimensioni del controllo.
// 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;
Esempio completo
// 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;
}
Argomenti correlati