Please note that I have found that I need to add COMBOBOXEXITEM
values much like LVITEM
based on this book: Image Lists and ComboBoxEx Controls | Programming Windows with MFC, Second Edition (flylib.com)
FYI, I am getting the above error on the SetImageList
call:
// Add color icons to combobox
for (int nCount = 0; nCount < m_colorBarTemplateFiles.GetCount(); nCount++) {
CBitmap colorTemplateBitmap;
const CString fname = colorBarTemplateDirectory + "\\" + m_colorBarTemplateImageFileNames[nCount];
HANDLE colorTemplateImageHandle = LoadImage(0, fname, IMAGE_BITMAP, 16, 16, LR_LOADFROMFILE);
colorTemplateBitmap.FromHandle((HBITMAP)colorTemplateImageHandle);
m_colorBarTemplateImages.Add(&colorTemplateBitmap, (COLORREF)0xFFFFFF);
}
SetImageList(&m_colorBarTemplateImages);
In the book I link, the author writes this code (BTW, I have purchased the book which will hopefully come with the CD, but it won't arrive for another 3 weeks):
m_il.Create (IDB_IMAGES, 16, 1, RGB (255, 0, 255));
SetImageList (&m_il);
But unfortunately, on the website the code does not have an *.rc file:
So I don’t know how he sets up IDB_IMAGES
. As far as I understand, the IDB_IMAGES is a big bitmap that is partitioned out into different icons, but it is not clear to met how to set that up in MFC.
This is afxcmn2.inl Line 334:
_AFXCMN_INLINE CImageList* CComboBoxEx::SetImageList(_In_ CImageList* pImageList)
{ ASSERT(::IsWindow(m_hWnd)); return CImageList::FromHandle((HIMAGELIST) ::SendMessage(m_hWnd, CBEM_SETIMAGELIST, 0, (LPARAM)pImageList->GetSafeHandle())); }
So somehow I am not creating the handle properly. I have also checked out other posts like:
visual c++ - How to add Images to CListCtrl in MFC - Stack Overflow
ccombobox - MFC CComboBoxEx icon update issue - Stack Overflow
CImageList Class | Microsoft Docs
CComboBoxEx Class | Microsoft Docs
Do you have any suggestions? TIA.
UPDATE:
Please note that I just learned that I need to be able to add these colors dynamically at runtime so it turns out I won't be able to work with *.rc
files and CBitmap
s. Instead, I'll have to research adding a colored region to the ComboBoxEx
in OnPaint
or OnDraw
somehow using using something like this:
COLORREF itemColor = colorArray[subitem][item];
CRect rect;
GetSubItemRect(item, subitem, LVIR_LABEL, rect);
CDC* pDc = GetDC();
pDc->FillRect(rect, &CBrush(itemColor));
ReleaseDC(pDc);
So I'll keep you posted on what I figure out.