捐赠 | 广告 | 注册 | 发布 | 上传 | 关于我们    
  沪ICP备05001939号 DELPHI盒子 | 盒子论坛 | 盒子文章 | 盒子问答悬赏 | 最新更新 | 论坛检索 | 下载中心 | 高级搜索    
  精品专区 | 繁體中文 | 奖励公告栏 | 直通车账号登陆 | 关闭GOOGLE广告 | 临时留言    
 
广告
评论:自己写的颜色下拉列表控件
abcjingtong 47546 2013/8/7 10:13:56
多谢站长
ozhy1 47544 2013/8/7 9:38:36
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.
第一页 上一页 下一页 最后页 有 2 条纪录 共1页 1 - 2
 用户名:
 密 码:
自动登陆(30天有效)
 
  DELPHI盒子版权所有 1999-2023 V4.01 粤ICP备10103342号-1 更新RSS列表