该控件源代码中加入彩色串的代码如下:
procedure TColorListBox.AddString(const strText : String; clTextColor : TColor);
begin
  Items.AddObject(strText, TObject(clTextColor));
end;
其中Items为其父类TListBox中属性,但我直接使用Listbox控件
  ListBox1.Items.AddObject('hello',TObject(clRed));
只是加入了黑色串,是我理解错了吗?请高手指教!

解决方案 »

  1.   

    “Items为其父类TListBox中属性”这个Items是继承自TListBox的TColorListBox类的属性....没有用过TColorListBox这个东西,我想它可能已经重载Items.AddObject这个方法了吧..
      

  2.   

    这个控件作为ColorListBox 不只是Items.AddObject(strText, TObject(clTextColor)); 肯定还作了很多处理工作,  如果你只是ListBox1.Items.AddObject('hello',TObject(clRed));
    肯定是不行的,  你先把控件的源码看懂了再说,  我想这个控件的源码不会太复杂
      

  3.   

    控件的源代码不长,好像没有像你们说的从载和另外处理,源代码付上
    unit ColorListBox;interfaceuses
      Windows, Messages, SysUtils, Classes, Controls, StdCtrls, Graphics;type
      TColorListBox = class(TListBox)
      private
        { Private declarations }
      protected
        { Protected declarations }
        procedure DrawItem(Index : Integer; Rect : TRect;
                           State : TOwnerDrawState); override;
      public
        { Public declarations }
        constructor Create(AOwner : TComponent); override;
        destructor  Destroy; override;
        procedure   AddString(const strText : String; clTextColor : TColor);
        procedure   SetColor(const AIndex : Integer; const AColor : TColor);
        function    GetColor(const AIndex : Integer) : TColor;
      published
        { Published declarations }
      end;procedure Register;implementationprocedure Register;
    begin
      RegisterComponents('Samples', [TColorListBox]);
    end;constructor TColorListBox.Create(AOwner : TComponent);
    begin
      Inherited;  Style := lbOwnerDrawFixed;
    end;destructor TColorListBox.Destroy;
    begin
      Inherited;
    end;procedure TColorListBox.DrawItem(Index : Integer; Rect : TRect;
                                     State : TOwnerDrawState);
    var
      SaveFontColor : TColor;
      SaveBrushStyle : TBrushStyle;
    begin
      SaveFontColor := Canvas.Font.Color;
      SaveBrushStyle := Canvas.Brush.Style;  Canvas.FillRect(Rect);  Canvas.Brush.Style := bsClear;
      Canvas.Font.Color := TColor(Items.Objects[Index]);
      Canvas.TextOut(Rect.Left, Rect.Top, Items.Strings[Index]);  Canvas.Font.Color := SaveFontColor;
      Canvas.Brush.Style := SaveBrushStyle;
    end;procedure TColorListBox.SetColor(const AIndex : Integer; const AColor : TColor);
    begin
      if (AIndex < 0) or (AIndex >= Items.Count) then
        Exit;  Items.Objects[AIndex] := TObject(AColor);
      Invalidate;
    end;function TColorListBox.GetColor(const AIndex : Integer) : TColor;
    begin
      if (AIndex < 0) or (AIndex >= Items.Count) then
        Result := 0
      else
        Result := TColor(Items.Objects[AIndex]);
    end;procedure TColorListBox.AddString(const strText : String; clTextColor : TColor);
    begin
      Items.AddObject(strText, TObject(clTextColor));
    end;
    end.
      

  4.   

    怎么会没有处理, 下面的就是处理
    procedure TColorListBox.DrawItem(Index : Integer; Rect : TRect;
                                     State : TOwnerDrawState);
    var
      SaveFontColor : TColor;
      SaveBrushStyle : TBrushStyle;
    begin
      SaveFontColor := Canvas.Font.Color;
      SaveBrushStyle := Canvas.Brush.Style;  Canvas.FillRect(Rect);  Canvas.Brush.Style := bsClear;
      Canvas.Font.Color := TColor(Items.Objects[Index]); //AddObject 的作用就在这了
      Canvas.TextOut(Rect.Left, Rect.Top, Items.Strings[Index]);  Canvas.Font.Color := SaveFontColor;
      Canvas.Brush.Style := SaveBrushStyle;
    end;
      

  5.   

    因为对象指针和色彩值一样,都是占用4个字节,所以可以把色彩值强制赋给Objects[x]。
    但是这样一来,TColorListBox就无法使用Items.Objects属性来存储你自己的信息,局限不小。