TColorComboBox 提示已经存在 我修改了下 保存为 ColorComboBoxzhy.pas
---------- unit ColorComboBoxzhy;
interface
uses SysUtils, Classes, Controls, StdCtrls, Graphics, Types;
type TColorComboBoxzhy = class(TCustomComboBox) private { Private declarations } FSelected: TColor; procedure SetSelected(Value: TColor); function GetSelected: TColor; protected { Protected declarations } function GetColor(AIndex: Integer): TColor; procedure DrawItem(Index: Integer; Rect: TRect; State: TOwnerDrawState); override; procedure CreateWnd; override; procedure Change; override; public { Public declarations } constructor Create(AOwner: TComponent); override; destructor Destroy; override; published { Published declarations } property Selected: TColor read GetSelected write SetSelected; property Items; property ItemIndex default 0; property OnChange; end;
procedure Register;
implementation
procedure Register; begin RegisterComponents('zhy', [TColorComboBoxzhy]); end;
{ TColorComboBox }
procedure TColorComboBoxzhy.Change; begin inherited; FSelected := GetColor(ItemIndex); end;
constructor TColorComboBoxzhy.Create(AOwner: TComponent); begin inherited Create(AOwner); Style := csOwnerDrawFixed; end;
procedure TColorComboBoxzhy.CreateWnd; begin inherited CreateWnd; //Items.Clear; if Items.Count = 0 then begin Items.Add('无'); //clNone Items.Add('黑'); //clBlack Items.Add('白'); //clWhite Items.Add('绿'); //clGreen Items.Add('橄榄'); //clOlive Items.Add('海蓝'); //clNavy Items.Add('紫'); //clPurple Items.Add('青'); //clTeal Items.Add('灰'); //clGray Items.Add('银'); //clSilver Items.Add('红'); //clRed Items.Add('黄绿'); //clLime Items.Add('黄'); //clYellow Items.Add('蓝'); //clBlue Items.Add('紫红'); //clFuchsia Items.Add('浅绿'); //clAqua Items.Add('浅灰'); //clLtGray Items.Add('深灰'); //clDkGray Items.Add('红褐'); //clMaroon end; end;
destructor TColorComboBoxzhy.Destroy; begin
inherited Destroy; end;
procedure TColorComboBoxzhy.DrawItem(Index: Integer; Rect: TRect; State: TOwnerDrawState); var viTextW: Integer; begin inherited; Canvas.Brush.Color := GetColor(Index); viTextW := Canvas.TextWidth('金金'); //Items[Index] Canvas.Rectangle(Rect.Left + 1, Rect.Top + 1, Rect.Right - viTextW - 5, Rect.Bottom - 1); Canvas.Brush.Style := bsClear; Canvas.TextOut(Rect.Right - viTextW - 2, Rect.Top + 1, Items[Index]); end;
function TColorComboBoxzhy.GetColor(AIndex: Integer): TColor; begin Result := clNone; case AIndex of //0: Result := clNone; 1: Result := clBlack; 2: Result := clWhite; 3: Result := clGreen; 4: Result := clOlive; 5: Result := clNavy; 6: Result := clPurple; 7: Result := clTeal; 8: Result := clGray; 9: Result := clSilver; 10: Result := clRed; 11: Result := clLime; 12: Result := clYellow; 13: Result := clBlue; 14: Result := clFuchsia; 15: Result := clAqua; 16: Result := clLtGray; 17: Result := clDkGray; 18: Result := clMaroon; end; end;
function TColorComboBoxzhy.GetSelected: TColor; begin FSelected := GetColor(ItemIndex); Result := FSelected; end;
procedure TColorComboBoxzhy.SetSelected(Value: TColor); var I: Integer; begin for I := 0 to Items.Count - 1 do begin if GetColor(i) = Value then begin ItemIndex := i; Break; end; end; end;
end. |