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
| Unit RTFdansCANVAS;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, stdctrls, comctrls, richedit
type
TVAlignment = (vtaBottom, vtaCenter, vtaTop);
procedure DrawTextArea(Canvas: TCanvas; ARect : TRect;ARichEdit : TRichEdit; VAlignment:TVAlignment );
implementation
function PrintToCanvas(ACanvas : TCanvas; FromChar, ToChar : integer;
ARichEdit : TRichEdit; var ARect : TRect;RenderIt:boolean) : Longint;
var Range : TFormatRange;
begin
//Précaution
SendMessage(ARichedit.Handle, EM_FORMATRANGE, 0, 0);
if not RenderIt then
begin
SetRect(ARect,0,0,ARichEdit.width,ARichEdit.height);
end;
FillChar(Range, SizeOf(TFormatRange), 0);
Range.hdc := ACanvas.handle;
Range.hdcTarget := ACanvas.Handle;
Range.rc.left := ARect.Left * 1440 div Screen.PixelsPerInch;
Range.rc.top := Arect.Top * 1440 div Screen.PixelsPerInch;
Range.rc.right := ARect.Right * 1440 div Screen.PixelsPerInch;
Range.rc.Bottom := ARect.Bottom * 1440 div Screen.PixelsPerInch;
Range.chrg.cpMax := ToChar;
Range.chrg.cpMin := FromChar;
if RenderIt
then Result := SendMessage(ARichedit.Handle, EM_FORMATRANGE, 1, Longint(@Range))
else begin
Range.chrg.cpMax := ARichedit.GetTextLen;
Result := SendMessage(ARichedit.Handle, EM_FORMATRANGE, 0, Longint(@Range));
ARect:=Range.rc;
ARect.Bottom := Range.rc.Bottom * Screen.PixelsPerInch div 1440;
end;
SendMessage(ARichEdit.handle, EM_FORMATRANGE, 0,0);
end;
function GetTextSize(ARichEdit : TRichEdit): TSize;
var ARect:Trect;
begin
PrintToCanvas(Canvas,0,-1,ARichEdit ,ARect,false);
Result.cx := ARect.Right-Arect.Left;
Result.cy := ARect.Bottom-ARect.Top;
end;
procedure RTFPaint(Canvas:TCanvas;ARect:TRect;ARichEdit : TRichEdit);
begin
PrintToCanvas(Canvas,0,-1,ARichEdit,ARect,true);
end;
procedure DrawTextArea(Canvas: TCanvas; ARect : TRect;ARichEdit : TRichEdit; VAlignment:TVAlignment );
var Rect:TRect;
ASize:TSize;
dh,dy:integer;
procedure DrawText;
begin
Canvas.Pen.Color := Canvas.Brush.Color;
Canvas.Rectangle(ARect.Left,ARect.Top,ARect.Right,ARect.Bottom);
Canvas.Brush.Style := bsClear;
RTFPaint(Canvas,Rect,ARichEdit);
Canvas.Brush.Style := bsSolid;
Canvas.Font.Color := clBlack;
end;
begin
ASize:=GetTextSize(ARichEdit);
rect:=ARect;
dh:=(ARect.Bottom-ARect.Top)-ASize.cy;
if dh<0 then dh:=0;
dy:=0;
if VAlignment=vtaBottom then dy:=dh;
if VAlignment=vtaCenter then dy:=dh div 2;
OffsetRect(rect,0,dy);
DrawText;
end;
end. |
Partager