quinta-feira, 13 de janeiro de 2011

Como mudar a cor de fundo em linhas diferentes de texto em um TListBox

Depois de inserir um TListBox em seu form, você deve muda a propriedade Style do TListBox para lbOwnerDrawFixed. Se você não muda a propriedade Style, o evento OnDrawItem nunca vai ser chamado. Inclua o seguinte código no Evento OnDrawItem de seu TListBox:

procedure TForm1.ListBox1DrawItem
(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
myColor: TColor;
myBrush: TBrush;
begin
myBrush := TBrush.Create;
with (Control as TListBox).Canvas do
begin
if not Odd(Index) then
myColor := clSilver
else
myColor := clYellow;

myBrush.Style := bsSolid;
myBrush.Color := myColor;
Windows.FillRect(handle, Rect, myBrush.Handle);
Brush.Style := bsClear;
TextOut(Rect.Left, Rect.Top,
(Control as TListBox).Items[Index]);
MyBrush.Free;
end;
end;

Nenhum comentário:

Postar um comentário