TcxCustomListView.OnCustomDrawItem Event
Occurs when an item in a list view is rendered.
Declaration
property OnCustomDrawItem: TLVCustomDrawItemEvent read; write;
Remarks
The OnAdvancedCustomDrawItem event fires for each individual item of the control before its default painting takes place.
The Sender parameter of the event specifies the control whose item is being painted. Use the Canvas property of the object representing this parameter to obtain the canvas used to paint. The Item parameter enables you to determine the currently processed item. Use it to get the bounding rectangle of the item (the DisplayRect method) and other information needed when painting (caption text, image indexes, etc). The State parameter can be used to determine the state of the painted item. The cdsSelected and cdsHot values included into this parameter’s value indicate that the item is selected or hot-tracked respectively.
The following code paints the label background for the items using the texture, loaded from a bitmap. Note, that two different textures are used according to the item’s selection state:
procedure TForm1.cxListView1CustomDrawItem(Sender: TCustomListView; Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
var
ASelectedBitmap, ABitmap: TBitmap;
ARect: TRect;
ABrush: HBRUSH;
begin
ASelectedBitmap := TBitmap.Create;
ABitmap := TBitmap.Create;
ASelectedBitmap.LoadFromFile('C:\IMAGES\red.BMP');
ABitmap.LoadFromFile('C:\IMAGES\blue.BMP');
ARect := Item.DisplayRect(drLabel);
if cdsSelected in State then
ABrush := CreatePatternBrush(ASelectedBitmap.Handle)
else
ABrush := CreatePatternBrush(ABitmap.Handle);
FillRect(Sender.Canvas.Handle, ARect, ABrush);
SetBkMode(Sender.Canvas.Handle, TRANSPARENT);
SetTextColor(Sender.Canvas.Handle, clWhite);
SelectObject(Sender.Canvas.Handle, Sender.Canvas.Font.Handle);
Sender.Canvas.TextOut(ARect.Left + 1, ARect.Top + 1, Item.Caption);
ABitmap.Free;
ASelectedBitmap.Free;
DefaultDraw := True;
end;
The following image demonstrates the result of the code execution: