创建一个颜色选取下拉框

  • 主题发起人 主题发起人 import
  • 开始时间 开始时间
I

import

Unregistered / Unconfirmed
GUEST, unregistred user!
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.
 

Similar threads

I
回复
0
查看
539
import
I
I
回复
0
查看
515
import
I
I
回复
0
查看
644
import
I
I
回复
0
查看
644
import
I
后退
顶部