Le titre n'est pas très explicatif , loin s'en faut , mais la place manque

Voilà mon objectif : Faire en sorte que les ligne sélectionnée (multiselect) soit d'une couleur différente que la sélection d'une ligne (en effet , avez vous remarqué que la couleur de la ligne en cours (rowselect) était la même que celle des lignes sélectionnées . A moins que je n'ai loupé quelque chose il n'y a pas moyen d'indiquer une autre couleur .

Voilà donc ma 'solution' : J'ai dérivé un TCustomGrid (je n'ai aucun mérite , il suffit de regarder dans l'unité DBGrids) pour faire un TPersoDBGrid

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
194
195
196
 
unit persodbgrid;
 
{$mode objfpc}{$H+}
 
interface
 
uses
  Classes, SysUtils, Graphics, StdCtrls, Grids, Db, DBGrids, LCLType ;
 
type TPersoDBGrid  = class(TCustomDBGrid)
 private
     FSelection : TStringList;  // pour l'instant je n'utilise pas la TBookmarkList 'officielle'
     FCouleurSelection : TColor;                   // couleur d'une ligne sélectionnée
     FCouleurSelectionHighLighted : TColor;  // couleur d'une ligne/cellule selectionnée et focalisée  
     procedure SetCouleurSelection(const AValue: TColor);  
     procedure SetHCouleurSelection(const AValue: TColor);
 public
    property BorderColor;
    property Canvas;
    property DefaultTextStyle;
    property EditorBorderStyle;
    property EditorMode;
    property ExtendedColSizing;
    property FastEditing;
    property FocusColor;
    property FocusRectVisible;
    property GridLineColor;
    property GridLineStyle;
    property SelectedColor;
    property SelectedRows;
    //------------------- mon truc a moi --------------------------------------------------------
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    property ListSelection:  TStringList read FSelection;
   //----------------------------------------------------------------------------
  published
    property Align;
    property AlternateColor;
    // ------------------------------------------------------------------------------
    property CouleurSelection : TColor read FCouleurSelection write SetCouleurSelection;
    property CouleurSelectionHightLighted : TColor read 
FCouleurSelectionHighLighted write SetHCouleurSelection;
   //---------------------------------------------------------------------------------
    property Anchors;
    property AutoAdvance default aaRightDown;
    property AutoEdit;
    property AutoFillColumns;
    //property BiDiMode;
    property BorderSpacing;
    property BorderStyle;
    property Color;
    property Columns; // stored false;
    property Constraints;
    property DataSource;
    property DefaultDrawing;
    property DefaultRowHeight;
    property DragCursor;
    //property DragKind;
    property DragMode;
    property Enabled;
    property FixedColor;
    property FixedHotColor;
    property Flat;
    property Font;
    property HeaderHotZones;
    property HeaderPushZones;
    //property ImeMode;
    //property ImeName;
    property Options;
    property OptionsExtra;
    //property ParentBiDiMode;
    property ParentColor default false;
    property ParentFont;
    //property ParentShowHint;
    property PopupMenu;
    property ReadOnly;
    property Scrollbars default ssBoth;
    property ShowHint;
    property TabOrder;
    property TabStop;
    property TitleFont;
    property TitleStyle;
    property UseXORFeatures;
    property Visible;
    property OnCellClick;
    property OnColEnter;
    property OnColExit;
    property OnColumnMoved;
    property OnColumnSized;
//    property OnDrawColumnCell;    // je l'enleve pour faire le comportement du composant
    property OnDblClick;
    property OnDragDrop;
    property OnDragOver;
    property OnEditButtonClick;
    //property OnEndDock;
    property OnEndDrag;
    property OnEnter;
    property OnExit;
    property OnFieldEditMask;
    property OnKeyDown;
    property OnKeyPress;
    property OnKeyUp;
    property OnMouseDown;
    property OnMouseMove;
    property OnMouseUp;
    property OnPrepareCanvas;
    property OnSelectEditor;
    //property OnStartDock;
    property OnStartDrag;
    property OnTitleClick;
    property OnUserCheckboxBitmap;
    property OnUTF8KeyPress;
  protected
    { Déclarations protégées }
    procedure KeyDown(var Key: Word; Shift: TShiftState); override;
    procedure PersoDrawColumnCell(Sender : TObject;const Rect: TRect; DataCol: Integer;
                                   Column: TColumn; State: TGridDrawState);
    procedure BasculeSelection;
  end;
 
procedure Register;
 
implementation
 
procedure Register;
begin
  RegisterComponents('Data Controls',[TPersoDBGrid]);
end;
 
constructor TPersoDBGrid.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FSelection:=TStringList.Create;
  OnDrawColumnCell:=@Persodrawcolumncell;  //<- c'est ici que je change le comportement
end;
 
destructor TPersoDBGrid.Destroy;
begin
   FSelection.Free;
   inherited Destroy;
end;
 
procedure TPersoDBGrid.SetCouleurSelection(const AValue: TColor);
begin
  if FCouleurSelection=AValue then exit;
  FCouleurSelection:=AValue;
  Invalidate;
end;
 
procedure TPersoDBGrid.SetHCouleurSelection(const AValue: TColor);
begin
  if FCouleurSelectionHighLighted=AValue then exit;
  FCouleurSelectionHighLighted:=AValue;
  Invalidate;
end;
 
procedure TPersoDBGrid.BasculeSelection;
var
   i: integer;
begin
   BeginUpdate; // ? nécessaire ?
   try
      i:= FSelection.IndexOf(self.Datasource.Dataset.Bookmark);
      if i >= 0 then  FSelection.Delete(i)
                else  FSelection.Add(self.Datasource.Dataset.Bookmark);
   finally
     EndUpdate;
   end;
end;
 
 
procedure TPersoDBGrid.KeyDown(var Key: Word; Shift: TShiftState);
begin
   case key of
// .... pour simplifier j'ai oté certaines choses, peut être trop ?
      VK_SPACE :    BasculeSelection;
   end; // Fin du Case
   inherited KeyDown(Key,Shift);
end;
 
 
procedure TPersoDBGrid.PersoDrawColumnCell(Sender : TObject;const Rect: TRect; DataCol: Integer;
  Column: TColumn; State: TGridDrawState);
var i : LongInt;
begin
i:= FSelection.IndexOf(self.Datasource.Dataset.Bookmark);
if (i>=0) then
  begin
    if (gdSelected in State)
      then Self.Canvas.Brush.Color:= FCouleurSelectionHighLighted
      else Self.Canvas.Brush.Color:= FCouleurSelection;
    Canvas.FillRect(Rect);
    DefaultDrawColumnCell(Rect,DataCol,Column,State);
  end;
end;
J'en viens à ma question car je suis perfectionniste et que je butte
(PS : je vous rassure ça fonctionne)

Vous constaterez que j'ai shunter l'évènement OnDrawdataCell (dans le cas contraire faites moi confiance , je l'ai fait ) . Maintenant je voudrais également permettre de rajouter cette possibilté onDrawdatacell tout en gardant le comportement décrit plus haut . COMMENT FAIRE ?

- si je remets property OnDrawColumnCell; dans la partie published , je n'ai plus la partie PersoDrawColumnCell au cas ou je mets un évenement particulier , si je n'en mets pas , en posant la grille sur une forme je me retrouve avec un <unpublished> (qu'il faut remplacer par aucun pour avoir le comportement prévu)

Bref mon perfectionnisme me perdra , je suis sur que ce n'est qu'un problème de déclaration mais lequel ?