TListBox如何设置不同行不同颜色,请高手指教

解决方案 »

  1.   

    //设置listbox.style=lbOwnerDrawFixed procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    begin
      if index mod 2 =0 then
        TListBox(Control).Canvas.Brush.Color:=clREd
      else
        TListBox(Control).Canvas.Brush.Color:=clBlue;
      TListBox(Control).Canvas.TextRect(Rect,rect.Left,Rect.Top,TListBox(Control).Items[index]);end;
      

  2.   

    //补充楼上的
    type
      TListBoxEx = class(TListBox);procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    begin
      if Odd(Index) then
        TListBox(Control).Canvas.Brush.Color := clRed
      else TListBox(Control).Canvas.Brush.Color := clBlue;
      TListBoxEx(Control).OnDrawItem := nil;
      TListBoxEx(Control).DrawItem(Index, Rect, State);
      TListBoxEx(Control).OnDrawItem := ListBox1DrawItem;
    end;