First: Set CheckListBox's Property Style to lbOwnerDrawVariable or lbOwnerDrawFixed;Then in OnDrawItem Event Fill the Code Below: procedure TForm1.CheckListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState); begin with CheckListBox1 do begin case (Index Mod 5) of 0: Canvas.Brush.Color := clRed; 1: Canvas.Brush.Color := clBlue; 2: Canvas.Brush.Color := clWhite; 3: Canvas.Brush.Color := clYellow; 4: Canvas.Brush.Color := clSkyBlue; end; Canvas.FillRect(Rect); Canvas.TextOut(Rect.Left, Rect.Top, Items[Index]); end; end;
type TCheckListBoxEx = class(TCheckListBox);procedure TForm1.CheckListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState); begin TCheckListBox(Control).OnDrawItem := nil; try if Odd(Index) then TCheckListBoxEx(Control).Canvas.Font.Color := clRed else TCheckListBoxEx(Control).Canvas.Font.Color := clGreen; TCheckListBoxEx(Control).DrawItem(Index, Rect, State); finally TCheckListBox(Control).OnDrawItem := CheckListBox1DrawItem; end; end;procedure TForm1.FormCreate(Sender: TObject); begin CheckListBox1.Style := lbOwnerDrawVariable; end;
To sedszx (湘江) : case (Index Mod 5) of // 设置 画布 的 刷子填充色 0: Canvas.Brush.Color := clRed; 1: Canvas.Brush.Color := clBlue; 2: Canvas.Brush.Color := clWhite; 3: Canvas.Brush.Color := clYellow; 4: Canvas.Brush.Color := clSkyBlue; end; Canvas.FillRect(Rect); // 将 当前 项 填充背景色 Canvas.TextOut(Rect.Left, Rect.Top, Items[Index]); // 重画 Items 的 Text
procedure TForm1.CheckListBox1DrawItem(Control: TWinControl;
Index: Integer; Rect: TRect; State: TOwnerDrawState);
begin
with CheckListBox1 do
begin
case (Index Mod 5) of
0: Canvas.Brush.Color := clRed;
1: Canvas.Brush.Color := clBlue;
2: Canvas.Brush.Color := clWhite;
3: Canvas.Brush.Color := clYellow;
4: Canvas.Brush.Color := clSkyBlue;
end;
Canvas.FillRect(Rect);
Canvas.TextOut(Rect.Left, Rect.Top, Items[Index]);
end;
end;
TCheckListBoxEx = class(TCheckListBox);procedure TForm1.CheckListBox1DrawItem(Control: TWinControl;
Index: Integer; Rect: TRect; State: TOwnerDrawState);
begin
TCheckListBox(Control).OnDrawItem := nil;
try
if Odd(Index) then
TCheckListBoxEx(Control).Canvas.Font.Color := clRed
else TCheckListBoxEx(Control).Canvas.Font.Color := clGreen;
TCheckListBoxEx(Control).DrawItem(Index, Rect, State);
finally
TCheckListBox(Control).OnDrawItem := CheckListBox1DrawItem;
end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
CheckListBox1.Style := lbOwnerDrawVariable;
end;
0: Canvas.Brush.Color := clRed;
1: Canvas.Brush.Color := clBlue;
2: Canvas.Brush.Color := clWhite;
3: Canvas.Brush.Color := clYellow;
4: Canvas.Brush.Color := clSkyBlue;
end;
Canvas.FillRect(Rect); // 将 当前 项 填充背景色
Canvas.TextOut(Rect.Left, Rect.Top, Items[Index]); // 重画 Items 的 Text