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
| type
TAlignementVertical=(alVTop,alVCenter,alVBottom);
TAlignementHorizontal=(alHLeft,alHCenter,alHRight);
TJustification=(JustLeft,JustCenter,JustRight);
...
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; |
Partager