Board index » delphi » ListBox [ Every line has a different color ]

ListBox [ Every line has a different color ]

Hi all,
I tried some methods and setting the OwnerDraw and writing an OnDrawItem
event causes the event to be fired continuously! Is this normal. 'Coz as
you'd guess the form flickers and some of the other Labels' Font.Color's
and some Brush.Color's change too with that flickering !...

Is there a *calm and smooth* method you'd suggest...

Cheers,

Mert Inceiplik

 

Re:ListBox [ Every line has a different color ]


In message <39EE9EE1.82FD9...@otonom.com.tr>, Mert Inceiplik stated:

Quote
> I tried some methods and setting the OwnerDraw and writing an OnDrawItem
> event causes the event to be fired continuously! Is this normal.

Nope.
Quote
> Is there a *calm and smooth* method you'd suggest...

The following code works for me:

procedure TFrmAlignText.ListBoxDrawItem(Control: TWinControl;
  Index: Integer; Rect: TRect; State: TOwnerDrawState);
var
  horzOffset: integer;
  vertOffset: integer;
begin
  // ListBox.Style is set to lbOwnerDrawFixed.
  //
  with ListBox.Canvas do begin
    // vertOffset added to Rect.Top causes the string
    // to be vertically centered in the rectangle.
    //
    vertOffset  := (((Rect.Bottom - Rect.Top)
                   - TextExtent(ListBox.Items[Index]).CY) div 2);

    // TextWidth('Mi') div 4 gives (roughly)
    // 1/2 of an average character width.
    //
    horzOffset := TextWidth('Mi') div 4;

    if not (odSelected in State) then begin
      if Odd(Index) then begin
        Brush.Color := clBtnFace;
        Font.Color  := clBtnText
      end
      else begin
        Font.Color  := clFuchsia;
      end;
    end;

    FillRect(Rect);

    TextOut(Rect.Left + horzOffset,
            Rect.Top  + vertOffset,
            ListBox.Items[Index]);
  end;
end;

--
Regards
Ralph (TeamB)
===

Other Threads