I'm trying to make the text of particular items in a CListCtrl bold (based
eventually on the text of the item) and am having some trouble. What I
thought would be a trivial task certainly is not (unless I'm overlooking
something). It seems in order to do this, I have to do custom drawing in
the list control by handling the NM_CUSTOMDRAW notification in the control's
OnNotify() method. So what I did was basically copy and paste the example
code in MSDN's Custom Draw topic into my app. However, what that code does
in my app is make the control's column header text bold, but leaves the list
items unaffected! A couple of odd things I noticed were that I receive a
LOT of CDDS_ITEMPREPAINT notifications, even though my list contains only 4
items, and lplvcd->nmcd.dwItemSpec is always 0. Can someone please tell me
what I'm doing wrong, or if there's an easier way of doing this? Below is
my code.
Thanks!
Chris
BOOL CListCtrl3::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
LPNMLISTVIEW pnm = (LPNMLISTVIEW)lParam;
switch (pnm->hdr.code)
{
case NM_CUSTOMDRAW:
{
LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)lParam;
// CDDS_PREPAINT is the first Custom Draw related notification
received.
// Setting *pResult to CDRF_NOTIFYITEMDRAW will let us
// receive draw notifications for each list item.
if(lplvcd->nmcd.dwDrawStage == CDDS_PREPAINT)
{
*pResult = CDRF_NOTIFYITEMDRAW;
}
// Because of what was done above, for each list item being
drawn,
// we should receive a CDDS_ITEMPREPAINT notification. So the
following
// is used to draw each list item.
else if(lplvcd->nmcd.dwDrawStage == CDDS_ITEMPREPAINT)
{
// The following is supposed to make every other list item's
text bold.
// Instead, the column header text is made bold, but none
of
// the list items are affected!!!
if(!(lplvcd->nmcd.dwItemSpec % 2))
{
CFont font;
font.CreateFont(0, 0, 0, 0, FW_BOLD, FALSE, FALSE,
FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, _T("Arial"));
SelectObject(lplvcd->nmcd.hdc, (HFONT)font);
*pResult = CDRF_NEWFONT;
}
else
*pResult = CDRF_DODEFAULT;
}
return 1;
}
}
return CListCtrl::OnNotify(wParam, lParam, pResult);
}
What I think you want to do is make the CListCtrl object owner drawn. I did
this as follows in my OnCreate():
CListCtrl& ListCtrl = GetListCtrl();
LONG dwStyle;
dwStyle = GetWindowLong(ListCtrl.m_hWnd, GWL_STYLE);
dwStyle |= (LVS_REPORT | LVS_OWNERDRAWFIXED);
SetWindowLong(ListCtrl.m_hWnd, GWL_STYLE, dwStyle);
Then I supplied an override for DrawItem().
The drawing wasn't too difficult.
HTH,
TFM3
Note: spam-resistant e-mail address
Chris Crowley wrote in message ...
>Hello,
>
>I'm trying to make the text of particular items in a CListCtrl bold (based
>eventually on the text of the item) and am having some trouble.
<big snip>
Chris,
I handle the NM_CUSTOMDRAW message directly in my derived list control
class like this:
ON_NOTIFY_REFLECT( NM_CUSTOMDRAW, CustomDrawList)
void CGridListCtl::CustomDrawList(NMLVCUSTOMDRAW *pLVCD,
LRESULT *result)
{
switch( pLVCD->nmcd.dwDrawStage )
{
case CDDS_PREPAINT:
*result = CDRF_NOTIFYITEMDRAW;
break;
case CDDS_ITEMPREPAINT | CDDS_SUBITEM:
if ( pLVCD->iSubItem > 0 )
{
DWORD dwItemFlags =
GetItemData( pLVCD->nmcd.dwItemSpec );
if ( dwItemFlags & ( 1 << pLVCD->iSubItem ) )
{
/* Display it grayed */
pLVCD->clrText =
GetSysColor( COLOR_BTNFACE );
*result = CDRF_DODEFAULT;
}
else
{
*result = CDRF_DODEFAULT;
}
}
else
{
*result = CDRF_DODEFAULT;
}
break;
case CDDS_ITEMPREPAINT:
*result = CDRF_NOTIFYSUBITEMDRAW;
break;
}
}
The only significant issues I can see is that your font object is
temporary - it'll get destroyed when the CFont object goes out of
scope.
Dave
----
My address is altered to discourage junk mail.
Please post responses to the newsgroup thread,
there's no need for follow-up email copies.