by henriqueltrindade » October 31st, 2011, 1:33 pm
Height of the rectangle is always greater than the size of the text of richedit, see the code:
function GetPreciseTextLen(ARichEditCtrl : TCustomRichEdit): integer;
var
gtlex : TGetTextLengthEx;
begin
with gtlex do
begin
// flags := GTL_PRECISE;
flags := GTL_DEFAULT;
codepage := CP_UTF8;
end;
Result := ARichEditCtrl.Perform(EM_GETTEXTLENGTHEX,WPARAM(@gtlex), 0 );
end;
Evento NewPage:
procedure TMainForm.PrintPreviewNewPage(Sender: TObject);
var
R: TRect;
MaxLen: Integer;
begin
with PrintPreview do
begin
Canvas.Pen.Width := 0;
Canvas.Brush.Style := bsCLear;
// Draws a frame with 1cm margin
R := PageBoundsAfterMargin;
RichEdit1.Text := Trim( RichEdit1.Text );
RichEdit.PageRect := R;
MaxLen := GetPreciseTextLen( RichEdit1 );
MaxLen := ConvertY( MaxLen, mmPixel, Units );
SetRect(R, 1000, 1000, PaperWidth - 1000, MaxLen );
InflateRect( R, 300, 300 );
Canvas.Rectangle(R.Left, R.Top, R.Right, R.Bottom);
PaintRichText( R, TCustomRichEdit( RichEdit1 ), 0, nil );
end;
end;
The space increases if you have more lines
How to calculate the rectangle exactly the size of the text richedit?