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
| procedure TFenetrePrincipale.StringGrid_planningDrawCell(Sender: TObject;
ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
var
i: Integer;
Row1,Row2, dessine: integer;
fusion: boolean;
begin
// fusion des cellules
// on recherche les cellules identiques a fusionner, puis on fusionne
Row1 := 1; Row2 := 1;
fusion := false;
if ACol = 0 then
for i:=1 to StringGrid_Planning.RowCount - 1 do
begin
if StringGrid_Planning.Cells[0,i] = StringGrid_Planning.Cells[0,i+1] then
begin
inc(Row2);
fusion := true;
end
else
begin
inc(Row1); inc(Row2);
fusion := false;
end;
if fusion = false then
begin
if (Row1 <> Row2) and (Row1 <> (Row2+1)) then
begin
MergedCells(StringGrid_Planning,ACol,ARow,0,Row1-1,0,Row2-1,State);
Row1 := Row2;
end;
end;
end;
end;
{*------------------------------------------------------------------------------
Fusionner des cellules
------------------------------------------------------------------------------*}
procedure MergedCells(AStringGrid:TStringGrid;CurrentCol,CurrentRow,Col1,Row1,Col2,Row2:Integer;CurrentState: TGridDrawState);
var x1,y1,x2,y2:Integer;
X,Y,i: Integer;
ARect:TRect;
begin
//Initialisations diverses
ARect:=Bounds(0,0,0,0);
x1:=Col1;
y1:=Row1;
x2:=Col2;
y2:=Row2;
//On vérifie que la zone fusionnée est valide
if x1<0 then x1:=0;
if x2>AStringGrid.ColCount-1 then x2:=AStringGrid.ColCount-1;
if y1<0 then y1:=0;
if y2>AStringGrid.RowCount-1 then x2:=AStringGrid.RowCount-1;
if (x1>x2) or (y1>y2) then
begin
Exit;
end;
for i:= Row1 to Row2-1 do
begin
ARect.Left:=AStringGrid.CellRect(1,i).Left;
ARect.Top:=AStringGrid.CellRect(1,i).Top;
ARect.Right:=AStringGrid.CellRect(AStringGrid.ColCount,i).Right;
ARect.bottom:=AStringGrid.CellRect(AStringGrid.ColCount,i).Bottom;
// bordures
AStringGrid.Canvas.Pen.Color := clWhite;
AStringGrid.Canvas.Polyline([ point(Arect.left, Arect.bottom) , point(Arect.Right, Arect.bottom) ]);
end;
//Si la cellule courante est la dernière de la zone de fusion, on dessine dans la fusion le texte de la cellule en haut à gauche
if ((CurrentCol=Col2) and (CurrentRow=Row2))
then begin
ARect.Left:=AStringGrid.CellRect(Col1,Row1).Left;
ARect.Top:=AStringGrid.CellRect(Col1,Row1).Top;
ARect.Right:=AStringGrid.CellRect(Col2,Row2).Right;
ARect.bottom:=AStringGrid.CellRect(Col2,Row2).Bottom;
//alignement horizontal
X:=ARect.Left + (ARect.Right-ARect.Left-AStringGrid.Canvas.TextWidth(AStringGrid.Cells[CurrentCol,CurrentRow])) div 2;
// alignement vetical
Y:=ARect.Top + (ARect.Bottom-ARect.Top-AStringGrid.Canvas.TextHeight(AStringGrid.Cells[CurrentCol,CurrentRow])) div 2;
AStringGrid.Canvas.TextRect(ARect, X, Y, AStringGrid.Cells[Col1,Row1]);
end;
end; |
Partager