I want to owner draw a CComboBoxEx m_ctlCombo with text and images.
I create the image list and create COMBOBOXEXITEM and insert them into m_ctlCombo. It's all good.
Code:
// Add strings to the combo box.
for (int idx = 0; idx < 3; idx++)
{
// Each item has text, an lParam with extra data, and an image.
cbI.mask = CBEIF_TEXT | CBEIF_IMAGE | CBEIF_SELECTEDIMAGE;
cbI.pszText = _T("test"); // this will be different for each item
cbI.cchTextMax = sizeof(_T("test"));
cbI.iItem = idx; // Add the item to the end of the list.
cbI.iImage = idx; // Image to display.
cbI.iSelectedImage = idx;
cbI.iOverlay = 2;
cbI.iIndent = 0;
// Add the item to the combo box drop-down list.
int test = m_ctlCombo.InsertItem(&cbI); // works!
}
My question is how do I get the COMBOBOXEXITEM items in my ownerdrawn function from which I can extract the text "test" to draw.
Code:
// COwnerDraw
LRESULT DrawItem(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
LPDRAWITEMSTRUCT lpDrawItemStruct = (LPDRAWITEMSTRUCT) lParam;
if( lpDrawItemStruct->itemID == -1 )
return 0;
CDCHandle dc = lpDrawItemStruct->hDC;
const int nIndex = lpDrawItemStruct->itemID;
COMBOBOXEXITEM cbei;
GetItem(&cbei); // this Does Not work to get items with text
LPCTSTR lpszText = cbei.pszText; // How?
RECT rc = lpDrawItemStruct->rcItem; // this works to get rect
CImageList pImageList = GetImageList(); // this works to get image list
// ...
// code to draw text and images
// ...
}
// COwnerDraw
LRESULT DrawItem(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
LPDRAWITEMSTRUCT lpDrawItemStruct = (LPDRAWITEMSTRUCT) lParam;
if( lpDrawItemStruct->itemID == -1 )
return 0;
CDCHandle dc = lpDrawItemStruct->hDC;
const int nIndex = lpDrawItemStruct->itemID;
COMBOBOXEXITEM cbei;
GetItem(&cbei); // this Does Not work to get items with text
LPCTSTR lpszText = cbei.pszText; // How?
RECT rc = lpDrawItemStruct->rcItem; // this works to get rect
CImageList pImageList = GetImageList(); // this works to get image list
}
Why don't you use DRAWITEMSTRUCT itemData to get the item text?
Have a look at the owner draw comobox example.
I'm working with the CComboBoxEx class which uses strings and images stored in COMBOBOXEXITEM items. The CComboBox class which you referenced just uses strings.
I want to be able to access the COMBOBOXEXITEMS in my owner draw function to get the images and strings which I inserted into my owner drawn control using ...
Bookmarks