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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
| unit UnitFusionStringGrid;
interface
uses Windows, Classes, Graphics, StdCtrls,SysUtils,Grids;
type
TAlignementVertical=(alVTop,alVCenter,alVBottom);
TAlignementHorizontal=(alHLeft,alHCenter,alHRight);
TJustification=(JustLeft,JustCenter,JustRight);
TZoneFusionnee=record
ColLeft,RowTop,ColRight,RowBottom:Integer;
AlignementHorizontal:TAlignementHorizontal;
AlignementVertical:TAlignementVertical;
TextJustification:TJustification;
State:TGridDrawState;
ZoneValide:Boolean;
end;
TParametresOnDrawCell=record
LeStringGrid:TStringGrid;
ACol,ARow:Integer;
Rect:TRect;
State:TGridDrawState;
end;
//utilitaire pour la visibilité du code
function SetZoneFusionnee(AStringGrid:TStringGrid;ZoneText:string;
ColLeft,RowTop,ColRight,RowBottom:Integer;
AlignementHorizontal:TAlignementHorizontal;
AlignementVertical:TAlignementVertical;
TextJustification:TJustification):TZoneFusionnee;
//Pour dessiner une cellule simple (utilisée par MergedCells une zone)
procedure DessineCellule(ParametresOnDrawCell:TParametresOnDrawCell;
AlignementHorizontal:TAlignementHorizontal;
AlignementVertical:TAlignementVertical;
TextJustification:TJustification);
//Pour dessiner une fusion de cellules
function DessineZone(ParametresOnDrawCell:TParametresOnDrawCell;var ZoneFusionnee:TZoneFusionnee):Boolean;
//pour vérifier si une cellule se trouve dans la zone fusionnee spécifiee;
function CelluleEstDansZone(ACol,Arow:Integer;ZoneFusionnee:TZoneFusionnee):Boolean;
implementation
uses Types;
function CelluleEstDansZone(ACol,Arow:Integer;ZoneFusionnee:TZoneFusionnee):Boolean;
begin
with ZoneFusionnee do
result:=((ACol>=ColLeft) and (ARow>=RowTop) and (ACol<=ColRight) and (ARow<=RowBottom));
end;
function CelluleCouranteEstDansZone(ParametresOnDrawCell:TParametresOnDrawCell;ZoneFusionnee:TZoneFusionnee):Boolean;
begin
with ParametresOnDrawCell do
result:=CelluleEstDansZone(ACol,ARow,ZoneFusionnee);
end;
function ZoneEstValide(AStringGrid:TStringGrid;var AZone:TZoneFusionnee):Boolean;
begin
with AZone do
begin
//on modifie la zone pour qu'elle rentre dans le stringgrid
if ColLeft<0 then ColLeft:=0;
if ColRight>AStringGrid.ColCount-1 then ColRight:=AStringGrid.ColCount-1;
if RowTop<0 then RowTop:=0;
if RowBottom>AStringGrid.RowCount-1 then RowBottom:=AStringGrid.RowCount-1;
//On vérifie aue la zone fusionnée est valide
ZoneValide:=(ColLeft<=ColRight) and (RowTop<=RowBottom);
end;
Result:=AZone.ZoneValide;
end;
function SetZoneFusionnee(AStringGrid:TStringGrid;ZoneText:string;
ColLeft,RowTop,ColRight,RowBottom:Integer;
AlignementHorizontal:TAlignementHorizontal;
AlignementVertical:TAlignementVertical;
TextJustification:TJustification):TZoneFusionnee;
var AZone:TZoneFusionnee;
begin
AZone.ColLeft:=ColLeft;
AZone.RowTop:=RowTop;
AZone.ColRight:=ColRight;
AZone.RowBottom:=RowBottom;
AZone.AlignementHorizontal:=AlignementHorizontal;
AZone.AlignementVertical:=AlignementVertical;
AZone.TextJustification:=TextJustification;
if ZoneEstValide(AStringGrid,AZone) then AStringGrid.Cells[ColLeft,RowTop]:=ZoneText;
result:=AZone;
end;
//Voir FAQ Delphi de www.developpez.com pour cette fonction :
//URL : http://delphi.developpez.com/faq/?page=typechaine#dimensionstexte
Function TextSize(Phrase : string; Police : TFont = nil) : TPoint;
var
DC: HDC;
X: Integer;
Rect: TRect;
C : TBitmap;
begin
C := TBitmap.create;
if police <> nil then C.canvas.Font := police;
Rect.Left := 0;
Rect.Top:=0;
Rect.Right:=0;
Rect.Bottom:=0;
DC := GetDC(0);
C.Canvas.Handle := DC;
DrawText(C.Canvas.Handle, PChar(Phrase), -1, Rect, (DT_EXPANDTABS or DT_CALCRECT));
C.Canvas.Handle := 0;
ReleaseDC(0, DC);
result.X:=Rect.Right-Rect.Left;
result.Y:=Rect.Bottom-Rect.Top;
C.Free;
end;
procedure DessineTexteMultiligne(AString: string;ACanvas:TCanvas;ARect: TRect;
AlignementHorizontal:TAlignementHorizontal;
AlignementVertical:TAlignementVertical;
TextJustification:TJustification);
var
AHeight,AWidth:integer;
Rect,oldClipRect:TRect;
ATop,ALeft,H,W:Integer;
AText:string;
JustificationDuTexte:Integer;
MyRgn:HRGN;
begin
with ACanvas do
begin
Lock;
AHeight:=ARect.Bottom-ARect.Top;
AWidth:=ARect.Right-ARect.Left;
//on calcule la taille du rectangle dans lequel va tenir le texte
W:=TextSize(AString,ACanvas.Font).X;
H:=TextSize(AString,ACanvas.Font).Y;
//on calcule la position (Haut,Gauche) du rectangle dans lequel va tenir le texte
//en fonction de l'alignement horizontal et vertical choisis
ATop:=ARect.Top;
ALeft:=ARect.Left;
case AlignementVertical of
alVBottom : ATop:=ARect.Bottom-H;
alVCenter : ATop:=ARect.Top+(AHeight-H) div 2;
alVTop : ATop:=ARect.Top;
end;
case AlignementHorizontal of
alHLeft : ALeft:=ARect.Left;
alHCenter: ALeft:=ARect.Left+(AWidth-W) div 2;
alHRight : ALeft:=ARect.Right-W;
end;
//Fin du calcul du rectangle, on met le resultat dans Rect
Rect:=Bounds(ALeft,ATop,W,H);
//On rempli le rectangle de la zone sinon on voit le texte que delphi à dessiné
FillRect(ARect);
//On détermine les paramètres de justification à passer à Windows
case TextJustification of
JustLeft : JustificationDuTexte:=DT_LEFT;
JustCenter: JustificationDuTexte:=DT_CENTER;
JustRight : JustificationDuTexte:=DT_RIGHT;
end;
//Si le texte est plus grand que notre zone, on prend cette précaution (Clipping)
with ARect do MyRgn :=CreateRectRgn(Left,Top,Right,Bottom);
SelectClipRgn(Handle,MyRgn);
//On dessine le texte
DrawText(Handle,PChar(AString),-1,Rect,JustificationDuTexte or DT_NOPREFIX or DT_WORDBREAK );
//On a plus besoin de la zone de clipping
SelectClipRgn(Handle,0);
DeleteObject(MyRgn);
Unlock;
end;
end;
procedure DessineCellule(ParametresOnDrawCell:TParametresOnDrawCell;
AlignementHorizontal:TAlignementHorizontal;
AlignementVertical:TAlignementVertical;
TextJustification:TJustification);
var s:string;
begin
with ParametresOnDrawCell do
begin
s:=LeStringGrid.Cells[ACol,ARow];
DessineTexteMultiligne(s,LeStringGrid.Canvas,Rect,AlignementHorizontal,AlignementVertical,TextJustification);
end;
end;
//Pour calculer la position de la cellule (LeftCol,TopRow) dans le StringGrid
// --> voir CalcRect ci dessous
function CalcRectOffset(ASG:TStringGrid):TPoint;
var i,j:integer;
x,y:Integer;
begin
with ASG do
begin
x:=0;
y:=0;
if LeftCol>FixedCols then
for i:=FixedCols to LeftCol-1 do inc(x,ColWidths[i]+GridLineWidth);
if TopRow>FixedRows then
// for j:=FixedRows to TopRow-1 do inc(y,Top+RowHeights[j]+GridLineWidth);
for j:=FixedRows to TopRow-1 do inc(y,RowHeights[j]+GridLineWidth);
end;
Result:=Point(-x,-y);
end;
//Pour calculer le rectangle d'une cellule même s'il n'est pas visible (utilise CalcRectOffset)
function CalcRect(ASG:TStringGrid;ACol,ARow:integer):TRect;
var i,j:integer;
ARect:TRect;
Offsets:TPoint;
begin
Offsets:=CalcRectOffset(ASG);
with ASG do
begin
ARect:=Bounds(Offsets.X,Offsets.Y,0,0);
if ACol>0 then
for i:=0 to ACol-1 do ARect.Left:=Arect.Left+ColWidths[i]+GridLineWidth;
//ARect.Left:=ARect.Left+GridLineWidth;
ARect.Right:=ARect.Left;
ARect.Right:=ARect.Right+ColWidths[ACol];
if ARow>0 then
for j:=0 to ARow-1 do ARect.Top:=Arect.Top+RowHeights[j]+GridLineWidth;
//ARect.Top:=ARect.Top+GridLineWidth;
ARect.Bottom:=ARect.Top;
ARect.Bottom:=ARect.Bottom+RowHeights[ARow];
end;
Result:=ARect;
end;
function DessineZone(ParametresOnDrawCell:TParametresOnDrawCell;
var ZoneFusionnee:TZoneFusionnee):Boolean;
var i,j:Integer;
ARect:TRect;
UneZone:TZoneFusionnee;
DesParametres:TParametresOnDrawCell;
begin
//Initialisations diverses
result:=False;
ARect:=Bounds(0,0,0,0);
//On vérifie que la zone fusionnée est valide
if not ZoneEstValide(ParametresOnDrawCell.LeStringGrid,ZoneFusionnee) then Exit;
//parametres d'états
with ParametresOnDrawCell,ZoneFusionnee do
if ((ACol=ColLeft) and (ARow=RowTop)) then State:=[];
ZoneFusionnee.State:=ZoneFusionnee.State+ParametresOnDrawCell.State;
//Si la cellule courante est dans la zone de fusion, on dessine dans la fusion le texte de la cellule en haut à gauche
if CelluleCouranteEstDansZone(ParametresOnDrawCell,ZoneFusionnee)
then begin
//Calcule le rectangle de la zone fusionnée
with ZoneFusionnee,ParametresOnDrawCell do with LeStringGrid do
begin
ARect.TopLeft:=CalcRect(LeStringGrid,ColLeft,RowTop).TopLeft;
ARect.BottomRight:=CalcRect(LeStringGrid ,ColRight,RowBottom).BottomRight;
end;
//on va passer les parametres de OnDrawCell avec une zone de dessin un peu élargie;
DesParametres:=ParametresOnDrawCell;
DesParametres.Rect:=ARect;
DesParametres.ACol:=ZoneFusionnee.ColLeft;
DesParametres.ARow:=ZoneFusionnee.RowTop;
DesParametres.State:=ZoneFusionnee.State;
DessineCellule(DesParametres,
ZoneFusionnee.AlignementHorizontal,
ZoneFusionnee.AlignementVertical,
ZoneFusionnee.TextJustification);
result:=True;
end;
end;
end. |
Partager