Accueil > Programmes > Apicrypt > Programmation > StringGrid en couleur
DELPHI
StringGrid en couleur
(Color StringGrid)
dimanche 12 décembre 2010, par
Le composant Delphi StringGrid est un tableau croisant des lignes (ROW) et des colonnes (COL formant des Cellules (CELL) dont le contenu est indépendant d’une base de donnée.
COMMANDES DE BASE
Compter les Lignes | RowCount |
Compter les Colonnes | ColCount |
Largeur d’une colonne i | ColWidths[ i ] |
Valeur d’une cellule | Cells [ col, row ] |
La couleur des lignes se définit dans l’évènement,
StringGrid1DrawCell
DrawCell réalise automatiquement une boucle qui pointe les cellules l’une après l’autre en commençant en haut et à gauche (ligne 1) et en terminant en bas et à droite.
Pendant ce parcours chaque cellule est interrogée et les valeurs de aCol et aRow etc ... sont régulièrement incrémentées
procedure TForm1.StringGrid1DrawCell(Sender : TObject ; ACol, ARow : Integer ;
Rect : TRect ; State : TGridDrawState) ;
ACol | Entier : Abscisse de la cellule = Numéro de la Colonne |
---|
ARow | Entier : Ordonnée de la Cellule = Numéro de la ligne |
---|
Rect | Coordonnées du rectangle encadrant la ligne |
---|
State | Status de la cellule (fixe, sélectionnée ... ) |
---|
INITIALISATION DES COULEURS
Cette routine placée en début de procédure va s’appliquer automatiquement à chaque cellule
Et les colorer en Texte Noir sur fond Blanc par défaut
// Initialisation des Couleurs par défaut
with (Sender as TStringGrid) do begin
- Canvas.Font.Color := clBlack ;
- Canvas.Brush.Color := clWhite ;
- Canvas.Font.Style := [] ;
end ;
La mise en page se fait par l’intermédiaire du CANVAS
Il suffit maintenant de placer des Sauts Conditionnels IF dans la suite de la procédure DrawCell pour intercepter les valeurs de cellules qui nécessitent une coloration différente
if IsNeg (StringGrid1.Cells[1,arow]) then
- begin
- StringGRid1.Canvas.Font.Style := [fsBOLD] ;
- StringGrid1.Canvas.Font.Color := clWhite ;
- StringGrid1.Canvas.Brush.Color := clBLUE ;
- end
IsNeg est une routine qui teste si la valeur affichée est inférieure à la norme
Si la première cellule de la ligne contient une valeur "Négative" (cf Image), alors le test renvoie un indicateur qui colore la ligne.
Enfin, on applique les couleurs du canvas (FillRect) et on redessine le texte dans la cellule (TextOut)
StringGrid1.Canvas.FillRect( Rect ) ;
StringGrid1.Canvas.TextOut(Rect.Left, Rect.Top, StringGrid1.Cells[ACol, ARow]) ;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
{
L'évènement Draw Cell est une boucle automatique qui
-> parcoure les cellules de gauche à droite et de haut en bas
-> En modifant à chaque cycle les arguments
-> La cellule en cours est pointée par CELL[ ACol,ARow ]
}
function IsNEG ( S: string) : boolean;
Begin
result := false;
s := lowercase(s);
if (POS('négati',s)>0) or ( POS('negati',S)>0 ) then result := true;
End;
function IsPOS ( S: string) : boolean;
Begin
result := false;
s := lowercase(s);
if (POS('posit',s)>0) or ( POS('quelque',S)>0 ) or (pos('présence',s)>0) then
result := true;
End;
function IsTITRE ( S: string) : boolean;
Begin
result := false;
s := lowercase(s);
if (POS('urines',s)>0) then
result := true;
End;
BEGIN
// Initialisation des Couleurs par défaut
with (Sender as TStringGrid) do begin
Canvas.Font.Color := clBlack;
Canvas.Brush.Color := clWhite;
Canvas.Font.Style := [];
end;
// Fixed
if (ARow = 0) then begin
StringGrid1.Canvas.Brush.Color := clBLACK;
StringGrid1.Canvas.Font.Color := clWHITE;
StringGrid1.Canvas.Font.Style := [fsBOLD];
end
else begin
{ NEGATIF }
if IsNeg (StringGrid1.Cells[1,arow]) then
begin
StringGRid1.Canvas.Font.Style := [fsBOLD];
StringGrid1.Canvas.Font.Color := clWhite;
StringGrid1.Canvas.Brush.Color := clBLUE;
end
{ POSITIF }
else if IsPOS(StringGrid1.Cells[1, arow]) then
begin
StringGRid1.Canvas.Font.Style := [fsBOLD];
StringGrid1.Canvas.Font.Color := clWhite;
StringGrid1.Canvas.Brush.Color := clNAVY;
end
{ TITRE }
else if IsTITRE (StringGrid1.Cells[1,arow]) then
begin
StringGRid1.Canvas.Font.Style := [fsbold,fsUnderline];
StringGrid1.Canvas.Brush.Color := clYELLOW;
end
{ NORMAL }
Else if StringGrid1.Cells[5, arow] = 'Normal' then
begin
StringGrid1.Canvas.Font.Color := clBlack;
StringGrid1.Canvas.Brush.Color := clLime;
end
{ BAS }
else if StringGrid1.Cells[5, arow] = 'BAS' then
begin
StringGRid1.Canvas.Font.Style := [fsBOLD];
StringGrid1.Canvas.Font.Color := clWhite;
StringGrid1.Canvas.Brush.Color := clGreen; // $000080FF;
end
{ HAUT }
else if StringGrid1.Cells[5, arow] = 'HAUT' then
begin
panel_message.Caption := StringGrid1.Cells[0, arow] + ' = ' + StringGrid1.Cells[1, arow];
StringGRid1.Canvas.Font.Style := [fsBOLD];
StringGrid1.Canvas.Font.Color := clWhite;
StringGrid1.Canvas.Brush.Color := clRED;
end;
StringGrid1.Canvas.FillRect(Rect);
StringGrid1.Canvas.TextOut(Rect.Left, Rect.Top, StringGrid1.Cells[ACol, ARow]);
end;
end;