如何准备 ComboBoxEx 项和图像
本主题演示了如何向 ComboBoxEx 控件添加项目。
要在 ComboBoxEx 控件中添加一个项目,请首先定义一个 COMBOBOXEXITEM 结构。 然后,设置结构中的 mask 成员,以指明想让控件使用哪些成员。 最后,将结构中的指定成员设置为所需的值,并发送 CBEM_INSERTITEM 消息,以便将项目添加到控件中。
以下应用程序定义的函数将在现有的 ComboBoxEx 控件中添加 15 个项目。
需要了解的事项
技术
先决条件
- C/C++
- Windows 用户界面编程
说明
步骤 1:
要在 ComboBoxEx 控件中添加一个项目,请首先定义一个 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"}
};
步骤 2:
设置结构中的 mask 成员,以指明想让控件使用哪些成员。 请注意,COMBOBOXEXITEM 结构中的 mask 成员包含用于指示控件为每个项目显示图像的标志值。
// Set the mask common to all items.
cbei.mask = CBEIF_TEXT | CBEIF_INDENT |
CBEIF_IMAGE| CBEIF_SELECTEDIMAGE;
步骤 3:
将结构中的指定成员设置为所需的值,然后发送 CBEM_INSERTITEM 消息,以便将项目添加到控件中。
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;
步骤 4:
将现有图像列表分配给 ComboBoxEx 控件并设置控件大小。
// 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;
完整示例
// 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;
}
相关主题