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
| unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids,ExtCtrls;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure FormCreate(Sender: TObject);
private
{ Déclarations privées }
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure GetSGTopLeftCoords(const SG:TStringGrid;var C,R:Integer);
var I,J:Integer;Rc:TRect;
begin
C:=0;R:=0;
with SG do
begin
for I := 0 to ColCount - 1 do for J := 0 to RowCount - 1 do
begin
Rc := CellRect(I,J);
if (Rc.Left <> 0) or (Rc.Top <> 0) or (Rc.Right <> 0) or (Rc.Bottom <> 0)
then begin
C := I;
R := J;
Exit
end
end
end
end;
// Coordonnées pixels absolues de la case (I,J)
procedure GetSGAbsRect(const SG:TStringGrid;const I,J:Integer;var R:TRect);
var U,V:Integer;
begin
FillChar(R,SizeOf(R),0);
with SG do
begin
for U:=0 to I-1 do R.Left := R.Left + ColWidths[u] + GridLineWidth;
for V:=0 to J-1 do R.Top := R.Top + RowHeights[V] + GridLineWidth;
R.Right := R.Left + ColWidths[i];
R.Bottom := R.Top + RowHeights[J];
end;
end;
// Coordonnées pixels relatives de la case (I,J)
procedure GetSGRect(const SG:TStringGrid;const I,J:Integer;var Rect:TRect);
var RectTL : TRect;T,L:Integer;
begin
GetSGTopLeftCoords(SG,T,L);
GetSGAbsRect(SG,T,L,RectTL); // Coords pix. abs. de la case en haut à gauche
GetSGAbsRect(SG,I,J,Rect); // Coords pix. abs. de la case (I,J)
// Calcul des coordonnées pixels relatives de la case (I,J)
Rect.Left := Rect.Left - RectTL.Left;
Rect.Top := Rect.Top - RectTL.Top;
Rect.Right := Rect.Right - RectTL.Left;
Rect.Bottom := Rect.Bottom - RectTL.Top;
end;
function MergedCells(ASG:TStringGrid; ACol,ARow,ACol1,ARow1,ACol2,ARow2:Integer; ACurrentState: TGridDrawState):Boolean;
var
x1,y1,x2,y2:Integer;
Rect,Rect1,Rect2:TRect;
begin
with ASG, Canvas do
begin
//Initialisations diverses
Result := False;
x1 := ACol1;
y1 := ARow1;
x2 := ACol2;
y2 := ARow2;
//On vérifie que la zone fusionnée est valide
if x1 < 0 then x1 := 0;
if x2 > ColCount-1 then x2 := ColCount-1;
if y1 < 0 then y1 := 0;
if y2 > RowCount-1 then y2 := RowCount-1;
if (x1 > x2) or (y1 > y2) then
begin
result := false;
exit;
end;
if ((ACol>=ACol1) and (ACol<=ACol2) and (ARow>=ARow1) and (ARow<=ARow2)) then
begin
GetSGRect(ASG,ACol1,ARow1,Rect1);
GetSGRect(ASG,ACol2,ARow2,Rect2);
Rect.Left := Rect1.Left;
Rect.Top := Rect1.Top;
Rect.Right := Rect2.Right;
Rect.Bottom := Rect2.Bottom;
FillRect(Rect);
// Le bloc qui suit pour imiter le "look" par défaut
if GridLineWidth = 1 then
begin
Pen.Color := clBlack;Pen.Width := 1;
Canvas.Rectangle(Rect.Left-1,Rect.Top-1,Rect.Right+1,Rect.Bottom+1);
end;
//
Frame3D(ASG.Canvas, Rect, clBtnHighlight, clBtnShadow, 1);
InflateRect(Rect,-2,-2);
DrawText(ASG.Canvas.Handle, PChar(Cells[ACol1,ARow1]), -1, Rect ,DT_CENTER or DT_NOPREFIX or DT_VCENTER or DT_SINGLELINE );
Result := True;
end;
end;
end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
with Sender as TStringGrid do
if gdFixed in State then // Si cellule fixe :
if not MergedCells(StringGrid1,ACol,ARow,1,0,3,0,State)
and not MergedCells(StringGrid1,ACol,ARow,4,0,6,0,State)
then begin // Si cellule fixe pas fusionnée :
FillRect(Canvas.Handle,Rect,Canvas.Brush.Handle);
// Le bloc qui suit pour imiter le "look" par défaut
if GridLineWidth = 1 then
begin
Canvas.Pen.Color := clBlack;Canvas.Pen.Width := 1;
Canvas.Rectangle(Rect.Left-1,Rect.Top-1,Rect.Right+1,Rect.Bottom+1);
end;
//
Frame3D(Canvas, Rect, clBtnHighlight, clBtnShadow, BevelWidth);
InflateRect(Rect,-2,-2);
DrawText(Canvas.Handle, PChar(Cells[ACol,ARow]), -1, Rect ,DT_CENTER or DT_NOPREFIX or DT_VCENTER or DT_SINGLELINE );
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
StringGrid1.Cells[1,0]:='Hello1';
StringGrid1.Cells[2,0]:='';
StringGrid1.Cells[3,0]:='';
StringGrid1.Cells[4,0]:='Hello2';
StringGrid1.Cells[5,0]:='';
StringGrid1.Cells[6,0]:='';
end;
end. |
Partager