lbx.Items.Add('s');
    lbx.Items.Add('b');
    lbx.Items.Add('a');
在我不知道行数的情况下,怎样能让我加如的字显示不同的颜色阿 ?

解决方案 »

  1.   

    richbox才能显示其他颜色
    直接设置color:=cl...
      

  2.   

    道是可以这种效果,不过就是麻烦些,可能有更简单的
    就是拦截listbox的WM_PAINT
       oldmethod(message);//先定义oldmethod:TWndMethod
       a:=ListBox1.ItemRect(1);
       ListBox1.Canvas.Brush.Color:=clred;
       ListBox1.Canvas.Rectangle(a);
       ListBox1.Canvas.TextOut(a.Left,a.Top,'aaa');
    不过这样一点击的话就会失去效果,所以在listbox的onclick事件中也要写相同的代码
    这样就可以实现你的效果,有点麻烦.不过我试了一下效果还可以,要是不拦截消息写在事件里可能产生抖动
      

  3.   

    将ListBox的Style设置为lbOwnerDrawFixed,然后自己写OnDrawItem和OnMeasureItem事件来将你需要的东西‘画’上去。下面为两个OnDrawItem和OnMeasureItem事件的例子,在该例子中ListBox的第一项和第二项的字体颜色分别为红色和蓝色。(注意ListBox的Style属性要设置为lbOwnerDrawFixed)
    //OnDrawItem
    procedure TForm1.ListBoxOnDrawItem(Control: TWinControl; Index: Integer; Rect: TRect;
      State: TOwnerDrawState);
    begin
      with TListBox(Control).Canvas do
      begin
        if Index=0 then
          Font.Color:=clRed
        else if Index=1 then
          Font.Color:=clBlue;
        FillRect(Rect);
        TextOut(Rect.Left+3,Rect.Top+2,TListBox(Control).Items.Strings[Index]);
      end;
    end;//OnMeasureItem
    procedure TForm1.ListBoxOnMeasureItem(Control: TWinControl; Index: Integer;
      var Height: Integer);
    begin
      Height:=TListBox(Control).Canvas.TextHeight('T')+4;
    end;