精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>● Delphi>>控件开发和使用>>创建一个颜色选取下拉框。

主题:创建一个颜色选取下拉框。
发信人: lihai155(天琴)
整理人: kingron(2000-12-27 18:05:38), 站内信件
unit ColorComboBox;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TIdent = Record
    Color:Integer;
    Name:String;
  end;  
  TColorComboBox = class(TCustomComboBox)
  private
    { Private declarations }
    ColorArray:array of TIdent;
    function GetSelectedColor: TColor;
    procedure SetSelectedColor(Value: TColor);
  protected
    { Protected declarations }
    procedure CreateWnd; override;
    procedure DrawItem(Index: Integer; Rect: TRect; State: TOwnerDrawState); override;
    procedure Click;override;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    { Published declarations }
    property SelectedColor: TColor read GetSelectedColor write SetSelectedColor;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Samples', [TColorComboBox]);
end;

{ TColorComboBox }

procedure TColorComboBox.Click;
Var
  ColorDlg:TColorDialog;
begin
  if ItemIndex=High(ColorArray) then
  Begin
    ColorDlg:=TColorDialog.Create(Self);
    if ColorDlg.Execute then
      ColorArray[ItemIndex].Color:=ColorDlg.Color;
    ColorDlg.Free;  
  End;
  inherited;
end;

constructor TColorComboBox.Create(AOwner: TComponent);
begin
  inherited;
  Style := csOwnerDrawFixed;
  Height:=25;
  itemHeight:=19;
  Font.Charset := GB2312_CHARSET;
  Font.Name := '宋体';
  Font.Height:=-15;
  SetLength(ColorArray,10);
  ColorArray[0].Color:=clRed;
  ColorArray[0].Name:='红色';
  ColorArray[1].Color:=clGreen;
  ColorArray[1].Name:='绿色';
  ColorArray[2].Color:=clBlue;
  ColorArray[2].Name:='蓝色';
  ColorArray[3].Color:=clYellow;
  ColorArray[3].Name:='黄色';
  ColorArray[4].Color:=clWhite;
  ColorArray[4].Name:='白色';
  ColorArray[5].Color:=clMaroon;
  ColorArray[5].Name:='褐色';
  ColorArray[6].Color:=clNavy;
  ColorArray[6].Name:='深蓝色';
  ColorArray[7].Color:=clSilver;
  ColorArray[7].Name:='银灰色';
  ColorArray[8].Color:=clAqua;
  ColorArray[8].Name:='浅蓝色';
  ColorArray[9].Color:=clWhite;
  ColorArray[9].Name:='定制';

end;

procedure TColorComboBox.CreateWnd;
var
  Index: integer;
begin
  inherited CreateWnd;
  Items.BeginUpdate;
  for Index := 0 to High(ColorArray) do
    Items.Add(ColorArray[Index].Name);
  Items.EndUpdate;
  ItemIndex := 0;
end;

destructor TColorComboBox.Destroy;
begin
  inherited;

end;

procedure TColorComboBox.DrawItem(Index: Integer; Rect: TRect;
  State: TOwnerDrawState);
var
  Square: TRect;
begin
  TControlCanvas(Canvas).UpdateTextFlags;
  if odSelected in State then
  begin
    Canvas.Brush.Color := clHighlight;
  end;
  Canvas.FillRect(Rect);

  Canvas.Brush.Color := clBlack;
  Square := Rect;
  Square.Left := Square.Left + 1;
  Square.Right := Square.Left + ItemHeight -2;
  Square.Top := Square.Top + 1;
  Square.Bottom := Square.Bottom - 1;
  Canvas.FrameRect(Square);

  Canvas.Brush.Color := ColorArray[Index].Color;
  InflateRect(Square, -1, -1);
  Canvas.FillRect(Square);

  if odFocused in State then
  begin
    Canvas.Brush.Color := clHighlightText;
    Canvas.DrawFocusRect(Rect);
  end;

  Inc(Rect.Left, ItemHeight);
  InflateRect(Rect, 0, -1);
  if odSelected in State then
  begin
    Canvas.Pen.Color := clHighlightText;
    Canvas.Brush.Color := clHighlight;
  end
  else
  begin
    Canvas.Pen.Color := Font.Color;
    Canvas.Brush.Color := Color;
  end;
  DrawText(Canvas.Handle, PChar(Items[Index]), Length(Items[Index]), Rect, DT_VCENTER or DT_LEFT or DT_SINGLELINE);
end;

function TColorComboBox.GetSelectedColor: TColor;
begin
  Result := ColorArray[ItemIndex].Color;
end;

procedure TColorComboBox.SetSelectedColor(Value: TColor);
var
  Index: Integer;
begin
  for Index := 0 to High(ColorArray) do
    if ColorArray[Index].Color = Value then
    begin
      ItemIndex := Index;
      Exit;
    end;
end;

end.
//有什么改进意见欢迎回复,谢谢!


----
问彩云何处飞,愿成风永追随。
有奇缘能相聚,死也无悔。
我柔情深似海,你痴心可问天。
誓相守长缱绻,岁岁年年。

[关闭][返回]