I am using the CComboBoxEx control in MFC to implement an address box for a browser application which shows the address and the related site icon.
According this link: http://msdn.microsoft.com/en-us/library/bb775788(v=vs.85).aspx, calling CComboBoxEx::SetItem with iItem of -1 will modify the item currently displayed in the edit control. Here is the code segment I use to
HICON hIcon=LoadIcon(....); //load the new icon from somewhere
imagelist.Replace(1,hIcon); //replace the existing icon in the image list.
int nImage=1;
item.mask = CBEIF_IMAGE|CBEIF_SELECTEDIMAGE ;
item.iItem = -1;
item.iImage = nImage;
item.iSelectedImage = nImage;
SetItem(&item);
I found that ocassionally the icon doesn't update after SetItem is called. It still displays the previous icon after the new icon is set. Please note that the image index never changes. I am only updating the actual icon inside the image list.
Interestingly, I found that if I use mouse to click inside the combobox andn then click inside some other control so that the combobox loses focus, the icon will update. I could programmatically do that but I feel that's an awkard workaround.
Other than that, calling Invalidate or RedrawWindow on the combobox won't get the new icon to show up when it doesn't update.
Any experience or tips on this will be greatly appreciated. Thanks a lot.
Probably you need to call CComboBoxEx::SetImageList again.
I once encountered a similar problem. Later I found that the cause is the CImageList when I created it.
After I changed
m_pImgLst->Create(32,20,ILC_COLOR32,2,2);
to
m_pImgLst->Create(32,20,ILC_COLOR16,2,2);
the phenomenon of updating until WM_KILLFOCUS
received disappeared. But it's a pity that the image is a little less beautiful than before.
来源:https://stackoverflow.com/questions/6862404/mfc-ccomboboxex-icon-update-issue