Pour pouvoir comparer les différences de fonctionnement d’un TStringGrid sous Firemonkey par rapport a celle de la VCL, j’ai codé un petit test simple et amusant.
J’ai implémenté un rapide algo Diamant-Carré ou chaque valeur correspond a une cellule de la StringGrid.
Voici 2 images du programme :
J’ai utilisé pour cette démo Delphi XE Berlin.
L’algo Diamant-Carré est un peu personnalisé pour un besoin d’avoir un ratio de 2:1 et d’y ajouter une peu de valeur aléatoire.
L’essentiel de mon algo Diamant-Carré:
procedure TForm1.AlgoDiamantCarre; function Carre(const AA, AB, AC, AD, APas: integer): integer; begin Result := Trunc((AA + AB + AC + AD) / 4) + RandomRange(-APas, APas); end; function Diamant(const AA, AB, AC, APas: integer): integer; begin Result := Carre(AA, AB, AC, AC, APas); end; procedure DiamantCarre(const AX, AY, APas: integer); var LA, LB, LC, LD, LE: integer; begin LA := StringGrid1.Cells[AX - APas, AY - APas].ToInteger; LB := StringGrid1.Cells[AX + APas, AY - APas].ToInteger; LC := StringGrid1.Cells[AX - APas, AY + APas].ToInteger; LD := StringGrid1.Cells[AX + APas, AY + APas].ToInteger; LE := Carre(LA, LB, LC, LD, APas); StringGrid1.Cells[AX, AY] := IntToStr(LE); StringGrid1.Cells[AX - APas, AY] := IntToStr(Diamant(LA, LC, LE, APas)); StringGrid1.Cells[AX, AY - APas] := IntToStr(Diamant(LA, LB, LE, APas)); StringGrid1.Cells[AX + APas, AY] := IntToStr(Diamant(LB, LD, LE, APas)); StringGrid1.Cells[AX, AY + APas] := IntToStr(Diamant(LC, LD, LE, APas)); end; var LX, LY, LPas, LMoitie: integer; begin LPas := TAILLEY; LMoitie := TAILLEY div 2; repeat LY := LMoitie; while LY < TAILLEY do begin LX := LMoitie; while LX < TAILLEX do begin DiamantCarre(LX, LY, LMoitie); Inc(LX, LPas); end; Inc(LY, LPas); end; LPas := LPas div 2; LMoitie := LPas div 2; until (LPas < 2); end; |
Et l’appel de la fonction :
Randomize; // Défini les valeurs du double carré initiale. StringGrid1.Cells[0, 0] := IntToStr(Random(256)); StringGrid1.Cells[TAILLEY, 0] := IntToStr(Random(256)); StringGrid1.Cells[TAILLEX, 0] := IntToStr(Random(256)); StringGrid1.Cells[0, TAILLEY] := IntToStr(Random(256)); StringGrid1.Cells[TAILLEY, TAILLEY] := IntToStr(Random(256)); StringGrid1.Cells[TAILLEX, TAILLEY] := IntToStr(Random(256)); AlgoDiamantCarre; StringGrid1.Repaint; |
La routine d’affichage personnalisé d’une cellulle de la StringGrid:
procedure TForm1.StringGrid1DrawColumnCell(Sender: TObject; const Canvas: TCanvas; const Column: TColumn; const Bounds: TRectF; const Row: Integer; const Value: TValue; const State: TGridDrawStates); var LB: TBrush; LS: string; LI: Integer; begin LS := Value.AsString; if not TryStrToInt(LS, LI) then LI := 255; Bounds.Inflate(4, 4); LB := Tbrush.Create(TBrushKind.Solid, MakeColor(LI, LI, LI)); try Canvas.FillRect(Bounds, 0, 0, [], 1, LB); if CheckBox1.IsChecked then Column.DefaultDrawCell(Canvas, Bounds, Row, Value, State); finally LB.free; end; end; |