Mega Code Archive

 
Categories / Delphi / Graphic
 

Color ComboBox

Title: Color ComboBox Question: Where can I get a color selection combobox Answer: unit RVColorComboBox; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type tArrColors = record Name : String; Value: TColor; end; type TRVColorComboBox = class(TCustomComboBox) private FArrColors: array[0..17] of tArrColors; FColorSquareWidth: Integer; FDisplayColorNames: Boolean; protected procedure DrawItem(Index: Integer; Rect: TRect; State: TOwnerDrawState); override; public constructor Create(AOwner: TComponent); override; destructor Destroy; override; published property Anchors; property ColorSquareWidth: Integer read FColorSquareWidth write FColorSquareWidth; property DisplayColorNames: Boolean read FDisplayColorNames write FDisplayColorNames; property DropDownCount; property Enabled; property Font; property Hint; property ItemIndex; property ShowHint; property Sorted; property TabOrder; property Tag; property Visible; property OnChange; property OnClick; property OnDblClick; property OnDropDown; property OnEnter; property OnExit; property OnKeyDown; property OnKeyPress; property OnKeyUp; end; procedure Register; implementation procedure Register; begin RegisterComponents('Rendez-vous', [TRVColorComboBox]); end; { TColorComboBox } constructor TRVColorComboBox.Create(AOwner: TComponent); var Idx: Byte; begin inherited Create(AOwner); Parent:=TWinControl(AOwner); Style:=csOwnerDrawVariable; //Programme la matrice avec les noms de couleurs et leurs valeurs FArrColors[ 0].Name:='Noir'; FArrColors[ 0].Value:=clBlack; FArrColors[ 1].Name:='Marron'; FArrColors[ 1].Value:=clMaroon; FArrColors[ 2].Name:='Vert'; FArrColors[ 2].Value:=clGreen; FArrColors[ 3].Name:='Olive'; FArrColors[ 3].Value:=clOlive; FArrColors[ 4].Name:='Marine'; FArrColors[ 4].Value:=clNavy; FArrColors[ 5].Name:='Violet'; FArrColors[ 5].Value:=clPurple; FArrColors[ 6].Name:='Bleu-vert'; FArrColors[ 6].Value:=clTeal; FArrColors[ 7].Name:='Gris'; FArrColors[ 7].Value:=clGray; FArrColors[ 8].Name:='Argent'; FArrColors[ 8].Value:=clSilver; FArrColors[ 9].Name:='Rouge clair'; FArrColors[ 9].Value:=clRed; FArrColors[10].Name:='Vert clair'; FArrColors[10].Value:=clLime; FArrColors[11].Name:='Jaune'; FArrColors[11].Value:=clYellow; FArrColors[12].Name:='Bleu clair'; FArrColors[12].Value:=clBlue; FArrColors[13].Name:='Violet clair'; FArrColors[13].Value:=clFuchsia; FArrColors[14].Name:='Aqua'; FArrColors[14].Value:=clAqua; FArrColors[15].Name:='Gris clair'; FArrColors[15].Value:=clLtGray; FArrColors[16].Name:='Gris fonc'; FArrColors[16].Value:=clDkGray; FArrColors[17].Name:='Blanc'; FArrColors[17].Value:=clWhite; //Ajoute les noms de couleurs au "DropDown" for Idx:=0 to 17 do Items.Add(FArrColors[Idx].Name); FDisplayColorNames:=False; FColorSquareWidth:=0; end; destructor TRVColorComboBox.Destroy; begin inherited; end; procedure TRVColorComboBox.DrawItem(Index: Integer; Rect: TRect; State: TOwnerDrawState); var SpcWd: Integer; Rect1: TRect; Rect2: TRect; Rect3: TRect; Idx : Byte; begin inherited; Canvas.Brush.Color:=clWindow; Canvas.FillRect(Rect); //Contour du carr de couleur if DisplayColorNames then begin if ColorSquareWidth Rect1.Left :=Rect.Left+2; Rect1.Top :=Rect.Top+2; Rect1.Right :=Rect.Left+((Rect.Bottom-Rect.Top)*SpcWd)-2; Rect1.Bottom:=Rect.Bottom-2; end else begin Rect1.Left :=Rect.Left+2; Rect1.Top :=Rect.Top+2; Rect1.Right :=Rect.Right-2; Rect1.Bottom:=Rect.Bottom-2; end; //Carr de couleur Rect2.Left :=Rect1.Left+1; Rect2.Top :=Rect1.Top+1; Rect2.Right :=Rect1.Right-1; Rect2.Bottom:=Rect1.Bottom-1; //Reste du rectangle partir du carr Rect3.Left :=Rect1.Right+1; Rect3.Top :=Rect.Top; Rect3.Right :=Rect.Right; Rect3.Bottom:=Rect.Bottom; Canvas.Brush.Color:=clBlack; //Carr vide avec contour noir Canvas.FillRect(Rect1); //Trouve la bonne couleur for Idx:=0 to 17 do if FArrColors[Idx].Name=Items[Index] then break; Canvas.Brush.Color:=FArrColors[Idx].Value; //Carr plein de couleur Canvas.FillRect(Rect2); if (State*[odSelected,odFocused][]) then begin Canvas.Font.Color:=clHighLightText; Canvas.Brush.Color:=clHighLight; end else begin Canvas.Font.Color:=Font.Color; Canvas.Brush.Color:=Color; end; if DisplayColorNames then begin Canvas.Pen.Color:=Canvas.Brush.Color; Canvas.Rectangle(Rect3.Left,Rect3.Top,Rect3.Right,Rect3.Bottom); Canvas.TextOut(Rect1.Right+1,Rect1.Top,Items[Index]); end; end; end.