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
| procedure TForm1.StringGrid2DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure WriteText(StringGrid: TStringGrid; ACanvas: TCanvas; const ARect: TRect;
const Text: string; Format: Word);
const
DX = 2;
DY = 2;
var
S: array[0..255] of Char;
begin
with Stringgrid, ACanvas, ARect do
begin
case Format of
DT_LEFT: ExtTextOut(Handle, Left + DX, Top + DY,
ETO_OPAQUE or ETO_CLIPPED, @ARect, StrPCopy(S, Text), Length(Text), nil);
DT_RIGHT: ExtTextOut(Handle, Right - TextWidth(Text) - 3, Top + DY,
ETO_OPAQUE or ETO_CLIPPED, @ARect, StrPCopy(S, Text),
Length(Text), nil);
DT_CENTER: ExtTextOut(Handle, Left + (Right - Left - TextWidth(Text)) div 2,
Top + DY, ETO_OPAQUE or ETO_CLIPPED, @ARect,
StrPCopy(S, Text), Length(Text), nil);
end;
end;
end;
procedure Display(StringGrid: TStringGrid; const S: string; Alignment: TAlignment);
const
Formats: array[TAlignment] of Word = (DT_LEFT, DT_RIGHT, DT_CENTER);
begin
WriteText(StringGrid, StringGrid.Canvas, Rect, S, Formats[Alignment]);
end;
// Définit les conditions d'alignement pour chaque colonne
begin
// Ecrit le contenu des cellules d'entête sur 2 lignes
with StringGrid2, Canvas do
if ACol in [1..4] then
DrawText(Canvas.Handle, PChar(Cells[ACol,ARow]), -1, Rect, DT_NOPREFIX or DT_WORDBREAK );
// Les colonnes 0, 2, 3 et 4 ont leur contenu centrée
if ACol in [0, 2, 3, 4] then
Display(StringGrid2, StringGrid2.Cells[ACol, ARow], taCenter);
// La colonne 1 a son contenu aligné à gauche
if ACol in [1] then
Display(StringGrid2, StringGrid2.Cells[ACol, ARow], taLeftJustify);
end; |
Partager