procedure TMainForm.aListBoxDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
i: Integer;
position: Integer;
str: string;
begin
str := aListBox.Items.Strings[Index];
position := AnsiPos('* ', str);
if position > 0 then
begin
aListBox.Canvas.FillRect(Rect);
aListBox.Canvas.Font.Color := clRed;
aListBox.Canvas.TextOut(Rect.Left + 1, Rect.Top + 1, '* ');
end;
i := aListBox.Canvas.TextWidth('* ');
str := Copy(str, position + 2, Length(str));
aListBox.Canvas.Font.Color := clBlack;
aListBox.Canvas.TextOut(Rect.Left + 2 + i, Rect.Top + 1, str);
//aListBox.Canvas.DrawFocusRect(Rect);
end;
效果图如下:

本文介绍了一个使用Delphi实现的自定义列表绘制方法。通过重写aListBoxDrawItem过程,能够改变列表项中特定字符的显示样式,例如将星号(*)显示为红色,并调整其余文字的位置以确保整体布局美观。
2100

被折叠的 条评论
为什么被折叠?



