Reputation: 1391
I was planning to implememt owner-drawn of CListCtrl. I thought that drawing an item is after the item is inserted into the control. So my method is declare a class which is derived from CListCtrl and override its DrawItem() method. The problem is DrawItem is never invoked after inserting an item. Is there anything wrong with my method?
Thank you!
Upvotes: 2
Views: 1757
Reputation: 34365
To get your DrawItem() override called in a CListCtrl derived class, you need to set the style LVS_OWNERDRAWFIXED and also set the control to "Report View" mode. As far as I know, ownerdraw only works in report mode.
Also check that the variable is the correct type in your header file (eg. CMyListCtrl and not CListCtrl) and check the DDX assignment is correct as Mark Ransom suggested.
Upvotes: 1
Reputation: 34365
It could be that you also need to override MeasureItem() and CompareItem(). See the Microsoft article TN014 for more detail.
Here is some code I cut & pasted out of one of my own projects so I know it works:
int CGraphicDroplist::CompareItem(LPCOMPAREITEMSTRUCT lpCompareItemStruct)
{
return 0;
}
void CGraphicDroplist::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
RECT r = lpDrawItemStruct->rcItem;
CDC pDC;
pDC.Attach(lpDrawItemStruct->hDC);
// Put your code to draw the item here.
pDC.Detach();
}
void CGraphicDroplist::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
lpMeasureItemStruct->itemHeight = 100;
}
Upvotes: 0
Reputation: 308452
The problem might be that you are not associating the window with an instance of your class. This is done with DDX_Control inside your dialog's DoDataExchange method. DDX_Control calls CWnd::SubclassWindow, which you can do yourself if you prefer.
Upvotes: 0
Reputation: 34365
You need to set the LBS_OWNERDRAWFIXED style on the control. You can do that in the resource template or programmatically using the ModifyStyle() function.
example:
m_myListbox.ModifyStyle(0, LBS_OWNERDRAWFIXED, 0);
Upvotes: 0