ListBox中,用不同的颜色绘制一行文字

来源:互联网 发布:mac 安装ruby2.2.2 编辑:程序博客网 时间:2024/04/30 03:01
 
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;

效果图如下:

原创粉丝点击