该控件源代码中加入彩色串的代码如下:
procedure TColorListBox.AddString(const strText : String; clTextColor : TColor);
begin
Items.AddObject(strText, TObject(clTextColor));
end;
其中Items为其父类TListBox中属性,但我直接使用Listbox控件
ListBox1.Items.AddObject('hello',TObject(clRed));
只是加入了黑色串,是我理解错了吗?请高手指教!
procedure TColorListBox.AddString(const strText : String; clTextColor : TColor);
begin
Items.AddObject(strText, TObject(clTextColor));
end;
其中Items为其父类TListBox中属性,但我直接使用Listbox控件
ListBox1.Items.AddObject('hello',TObject(clRed));
只是加入了黑色串,是我理解错了吗?请高手指教!
肯定是不行的, 你先把控件的源码看懂了再说, 我想这个控件的源码不会太复杂
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.
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;
但是这样一来,TColorListBox就无法使用Items.Objects属性来存储你自己的信息,局限不小。