//---------------------------------------------------------------------------
//
in source...
void
__fastcall TForm1::WMNotify(TMessage &Msg)
{
LPNM_LISTVIEW lpnm = (NM_LISTVIEW *)Msg.LParam;
// see if the message is a Custom Draw message
if (lpnm->hdr.code == NM_CUSTOMDRAW)
{
// see if the message is from the ListView
if (lpnm->hdr.hwndFrom == ListView1->Handle)
{
LPNMLVCUSTOMDRAW lplvcd = (NMLVCUSTOMDRAW *)Msg.LParam;
switch (lplvcd->nmcd.dwDrawStage)
{
// prior to painting
case CDDS_PREPAINT:
{
// tell Windows we want notification
// of each item being drawn
Msg.Result = CDRF_NOTIFYITEMDRAW;
return;
}
// prior to each item being drawn
case CDDS_ITEMPREPAINT:
{
// extract relevant info from the
// custom draw structure
int Index = lplvcd->nmcd.dwItemSpec;
TListItem *Item =
ListView1->Items->Item[Index];
HDC Hdc = lplvcd->nmcd.hdc;
RECT IconRect, CaptionRect;
// get the rectangles for the icon and caption
IconRect.left = LVIR_ICON;
CaptionRect.left = LVIR_LABEL;
SNDMSG(ListView1->Handle, LVM_GETITEMRECT,
Index, (LPARAM)&IconRect);
SNDMSG(ListView1->Handle, LVM_GETITEMRECT,
Index, (LPARAM)&CaptionRect);
unsigned int DrawStyle = ILD_TRANSPARENT;
// if selected
if (lplvcd->nmcd.uItemState & CDIS_SELECTED)
{
DrawStyle = ILD_SELECTED;
SetTextColor(Hdc, ColorToRGB(clHighlightText));
// fill the caption rectangle
LOGBRUSH lb;
lb.lbStyle = BS_SOLID;
lb.lbColor = ColorToRGB(clHighlight);
HBRUSH HBrush = CreateBrushIndirect(&lb);
FillRect(Hdc, &CaptionRect, HBrush);
DeleteObject(HBrush);
}
// calculate the icon's horizontal position
int IconLeft = IconRect.left + 0.5 *
(IconRect.right - IconRect.left -
ImageList1->Width);
// draw the icon
ImageList_Draw((HIMAGELIST)ImageList1->Handle,
Item->ImageIndex, Hdc,
IconLeft, IconRect.top,
DrawStyle);
// draw the caption
DrawText(Hdc, Item->Caption.c_str(),
Item->Caption.Length(), &CaptionRect,
DT_CENTER | DT_WORDBREAK);
// tell Windows we drew the item manually
Msg.Result = CDRF_SKIPDEFAULT;
return;
}
// otherwise have Windows draw the item
default: Msg.Result = CDRF_DODEFAULT;
return;
}
}
}
// otherwise, let TForm handle the message
TForm::Dispatch(&Msg);
}
|