if Listbox.item[i].selected then
Listbox.item[i].font.color := clred;

解决方案 »

  1.   

    我试了一下好像不对啊!我是在listboxonclick事件里添加的。
    if Listbox1.items[listbox1.ItemIndex].selected thenListbox1.items[listbox1.ItemIndex].font.color := clred;
      

  2.   

    用TListView好了,有个HotTrack属性。自己试试看。
    一定要用TListBox的话,就要自己写OnDrawItem。
      

  3.   

    卷起千堆雪tyn (2001-10-25 17:15:00)  
    为了对比,用了两个Listbox构件,Listbox1按一般情况显示,
    Listbox2显示的每一项都可以有自己的字体、大小、颜色。 首先把Listbox2的style属性改为lbOwnerDrawVariable。
    然后分别编写它的OnDrawItem事件和OnDrawItem事件。
    procedure TForm1.ListBox2DrawItem(Control:
     TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    begin
      with ListBox2.Canvas do
      begin
        FillRect(Rect);
        Font.Size := 12;
        if Index mod 2 =0 Then
        begin
          Font.Name := '宋体';
          Font.Color := Clred;
        end
        else
        begin
          Font.Name := '隶书';
          Font.Color := Clgreen;
        end;
    TextOut(Rect.Left+1, Rect.Top+1,
     ListBox2.Items[Index]);
      end;
    end;procedure TForm1.ListBox2MeasureItem
    (Control: TWinControl; Index: Integer;
      var Height: Integer);
    begin
      with ListBox1.Canvas do
      begin
        Font.Size := 12;
        if Index mod 2 =0 Then
        begin
          Font.Name := '黑体';
          Font.Color := Clred;
        end
        else
        begin
          Font.Name := '隶书';
          Font.Color := Clgreen;
        end;
        Height := TextHeight('Wg') + 2;
      end;
    end;
     
    esquel.com (2001-10-25 17:20:00)  
    我是说如何控制Listbox 中每个Items字体颜色及每一个的背景颜色?
    你那方法只能更改字体颜色字体后的背景颜色不可以改变成每个都不同。 
    esquel.com (2001-10-26 17:22:00)  
    请各们大侠帮帮忙!急用!!!!!!!! 
     
    ski (2001-10-27 09:53:00)  
    guanzhu 
     
    blue_morning (2001-10-27 10:48:00)  
    卷起千堆雪tyn已经说得足够清晰了:
      1:Delphi的Listbox 中每个Items字体颜色及每一个的背景颜色是不可能控制的。
      2:所以你要自己画。以上两点说明你要自己在画字体之前先自己画一条背景。
    为什么什么事都要等别人帮你做好。 
     
    yyanghhong (2001-10-27 10:50:00)  
    see
    http://delphi.mychangshu.com/dispdoc.asp?id=256
    i use listbox to list line style and color 
     
    xianjun (2001-10-27 11:45:00)  
    把Listbox的style属性改为lbOwnerDrawVariable以后,要怎么显示你就可以随心所欲了。
    只在两个事件中写代码即可:procedure TForm1.ListBox1MeasureItem(Control: TWinControl; Index: Integer;
      var Height: Integer);
    begin
      Height := Index Mod 3 * 2 + ListBox1.ItemHeight; 
     //你要画的每个Item的高度,根据字体不同而不同,我这是随便取了几个不同的值
    end;procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    begin
      ListBox1.Canvas.Brush.Color := Index * $A0F0F0;
      ListBox1.Canvas.FillRect(Rect);
    //上面的代码是填充Item的背景,下面画出每个Item的文字,可以为每个Item设置不同的字体及着色
      if Index mod 2 = 0 then
      begin
        ListBox1.Canvas.Font.Name := 'Impact';
        ListBox1.Canvas.Font.Color := clLime;
      end
      else
      begin
        ListBox1.Canvas.Font.Name := 'Tahoma';
        ListBox1.Canvas.Font.Color := clRed;
      end;
    //用Canvas的TextOut画出文字:
      ListBox1.Canvas.TextOut(Rect.Left, Rect.Top, ListBox1.Items[Index]);
    end;