procedure TScoresListForm.CScoresListBoxDrawItem(AControl: TWinControl; AIndex: Integer; ARect: TRect; AState: TOwnerDrawState);
const
CHECKBOX_STATE: array [TCheckBoxState, Boolean] of TThemedButton = (
(tbCheckBoxUncheckedDisabled, tbCheckBoxUncheckedNormal),
(tbCheckBoxCheckedDisabled, tbCheckBoxCheckedNormal),
(tbCheckBoxMixedDisabled, tbCheckBoxMixedNormal)
);
var
ListBox: TCheckListBox absolute AControl;
var
CheckBoxDetails: TThemedElementDetails;
CheckBoxSize: TSize;
CheckBoxEnabled: Boolean;
var
CheckBoxRect, TextRect: TRect;
TextStyle: TTextStyle;
begin
// item enabled state
CheckBoxEnabled := ListBox.Enabled and ListBox.ItemEnabled[AIndex];
// listbox background (usually white)
ListBox.Canvas.Brush.Color := Windows.GetSysColor(COLOR_WINDOW);
ListBox.Canvas.FillRect(ARect);
// checkbox details
CheckBoxDetails := ThemeServices.GetElementDetails(CHECKBOX_STATE[ListBox.State[AIndex], CheckBoxEnabled]);
CheckBoxSize := ThemeServices.GetDetailSize(CheckBoxDetails);
CheckBoxRect := Bounds(ARect.Left + 4, ARect.Top + (ARect.Height - CheckBoxSize.Height) div 2, CheckBoxSize.Width, CheckBoxSize.Height);
// checkbox drawing
ThemeServices.DrawElement(ListBox.Canvas.Handle, CheckBoxDetails, CheckBoxRect);
// text area
TextRect := ARect;
TextRect.Left := CheckBoxRect.Right + 4;
// text background (in my system blue)
if odSelected in AState then
begin
ListBox.Canvas.Brush.Color := Windows.GetSysColor(COLOR_HIGHLIGHT);
ListBox.Canvas.FillRect(TextRect);
end;
// text properties
TextStyle := ListBox.Canvas.TextStyle;
TextStyle.Layout := tlCenter;
// first item is always bolded
if AIndex = 0 then
ListBox.Canvas.Font.Style := [fsBold];
// text drawing
ListBox.Canvas.Brush.Style := bsClear;
ListBox.Canvas.TextRect(TextRect, TextRect.Left + 4, TextRect.Top, ListBox.Items[AIndex], TextStyle);
// focus rect drawing
if odFocused in AState then
begin
ListBox.Canvas.DrawFocusRect(ARect); // this covers the default focus rect, wide as whole item
ListBox.Canvas.DrawFocusRect(TextRect); // this draws a correct focus rect, only around the text
end;
end;