1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
| unit CouleurListBox;
interface
uses
SysUtils, Classes, Controls, StdCtrls, Types, Graphics, Dialogs;
type
TCouleurListBox = class(TListBox)
private
FCouleur: array[0..1000] of TColor;
FItemEnabled: array[0..1000] of Boolean;
function GetCouleur(Index: Integer): TColor;
procedure PutCouleur(Index: Integer; Value: TColor);
function GetItemEnabled(Index: Integer): Boolean;
procedure PutItemEnabled(Index: Integer; Value: Boolean);
protected
procedure DrawItem(Index: Integer; Rect: TRect; State: TOwnerDrawState); override;
procedure Click; override;
procedure DblClick; override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure KeyPress(var Key: Char); override;
procedure KeyUp(var Key: Word; Shift: TShiftState); override;
// procedure MouseActivate(Button: TMouseButton; Shift: TShiftState; X, Y, HitTest: Integer; var MouseActivate: TMouseActivate); override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
public
constructor Create(AOwner: TComponent); override;
property Couleur[Index: Integer]: TColor read GetCouleur write PutCouleur;
property ItemEnabled[Index: Integer]: Boolean read GetItemEnabled write PutItemEnabled;
published
end;
const
IndexMax: integer=1000;
Ecart: integer= 2;
Epaisseur: integer= 1;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Mes composants', [TCouleurListBox]);
end;
constructor TCouleurListBox.Create(AOwner: TComponent);
var i: integer;
begin
InHerited Create(AOwner);
Style := lbOwnerDrawFixed;
ItemHeight := 13;
for i:=0 to IndexMax do begin
Couleur[i] := ClWhite;
FItemEnabled[i] := True;
end;
end;
function TCouleurListBox.GetCouleur(Index: Integer): TColor;
begin
if (Index<0) or (Index>IndexMax)
then result := ClWhite;
result := FCouleur[Index];
end;
procedure TCouleurListBox.PutCouleur(Index: Integer; Value: TColor);
begin
if (Index<0) or (Index>IndexMax)
then exit;
if Value <> FCouleur[Index] then
begin
FCouleur[Index]:= Value;
Invalidate;
end;
end;
function TCouleurListBox.GetItemEnabled(Index: Integer): Boolean;
begin
if (Index<0) or (Index>IndexMax)
then result := true;
result := FItemEnabled[Index];
end;
procedure TCouleurListBox.PutItemEnabled(Index: Integer; Value: Boolean);
begin
if (Index<0) or (Index>IndexMax)
then exit;
if Value <> FItemEnabled[Index] then
begin
FItemEnabled[Index]:= Value;
Invalidate;
end;
end;
procedure TCouleurListBox.DrawItem(Index: Integer; Rect: TRect; State: TOwnerDrawState);
var carre: TRect;
BackColor: Tcolor;
begin
inherited;
BackColor := Canvas.Brush.Color;
Canvas.FillRect(Rect);
Canvas.Brush.Color := ClSilver;
carre := Rect;
carre.Left := carre.Left + Ecart;
carre.Top := carre.Top + Ecart;
carre.Bottom := carre.Bottom - Ecart;
carre.Right := carre.Left + carre.Bottom - carre.Top;
Canvas. FillRect(carre);
Canvas.Brush.Color := FCouleur[Index];
carre := Rect;
carre.Left := carre.Left + (Ecart+Epaisseur);
carre.Top := carre.Top + (Ecart+Epaisseur);
carre.Bottom := carre.Bottom - (Ecart+Epaisseur);
carre.Right := carre.Left + carre.Bottom - carre.Top;
Canvas. FillRect(carre);
Canvas.Brush.Color := BackColor;
if not FItemEnabled[Index]
then Canvas.Font.Color := ClSilver;
Rect.Left := carre.Right + 2*(Ecart+Epaisseur);
Canvas.TextOut(Rect.Left, Rect.Top, Items[Index]);
end;
procedure TCouleurListBox.Click;
begin
if FItemEnabled[ItemIndex] then inherited;
end;
procedure TCouleurListBox.DblClick;
begin
if FItemEnabled[ItemIndex] then inherited;
end;
procedure TCouleurListBox.KeyDown(var Key: Word; Shift: TShiftState);
begin
if FItemEnabled[ItemIndex] then inherited;
end;
procedure TCouleurListBox.KeyPress(var Key: Char);
begin
if FItemEnabled[ItemIndex] then inherited;
end;
procedure TCouleurListBox.KeyUp(var Key: Word; Shift: TShiftState);
begin
if FItemEnabled[ItemIndex] then inherited;
end;
{
procedure TCouleurListBox.MouseActivate(Button: TMouseButton; Shift: TShiftState; X, Y, HitTest: Integer; var MouseActivate: TMouseActivate);
begin
if FItemEnabled[ItemIndex] then inherited;
end;
//}
procedure TCouleurListBox.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var ColorDialog: TColorDialog;
begin
if (x>0) and (x<ItemHeight)
then begin
ColorDialog := TColorDialog.Create(nil);
if ColorDialog.Execute
then PutCouleur(ItemIndex, ColorDialog.Color);
ColorDialog.Free;
end;
if FItemEnabled[ItemIndex] then inherited;
end;
procedure TCouleurListBox.MouseMove(Shift: TShiftState; X, Y: Integer);
begin
if FItemEnabled[ItemIndex] then inherited;
end;
procedure TCouleurListBox.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if FItemEnabled[ItemIndex] then inherited;
end;
end. |
Partager