DELPHI AREA
MESSAGE BOARD
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Possible new function for Preview.

 
   Reply to topic    DELPHI AREA Forum Index -> DELPHI AREA's Products
View previous topic :: View next topic  
Author Message
rgesswein
Member


Joined: 21 May 2003
Posts: 2

PostPosted: 21/05/03 21:10    Post subject: Possible new function for Preview. Reply with quote

I've found the following function very handy for when I need to place RTF precisely on a canvas (and I won't be letting it span pages.) The function allows me to get the print rectangle so I can, for example, put the RTF so the last line sits at the bottom of the page.

Note this was pulled mostly from the Delphi forums, probably Peter Below's source originally. Feel free to use/include as you see fit.

Code:

function TPrintPreview.getRichTextRect(var Rect: TRect; RichEdit: TCustomRichEdit; pOffset: PInteger=nil): Integer;
var
  Range: TFormatRange;
  OldMap: Integer;
  SaveRect: TRect;
  SaveIndex: Integer;
begin
  Result := -1;
  FillChar(Range, SizeOf(TFormatRange), 0);
  Range.hdc := Canvas.Handle;
  Range.hdcTarget := Range.hdc;
  Range.rc.Left := ConvertUnit(Rect.Left, Units, mmTWIPS);
  Range.rc.Top := ConvertUnit(Rect.Top, Units, mmTWIPS);
  Range.rc.Right := ConvertUnit(Rect.Right, Units, mmTWIPS);
  Range.rc.Bottom := ConvertUnit(Height, Units, mmTWIPS);
  Range.rcPage := Range.rc;
  Range.chrg.cpMax := -1;
  if pOffset = nil then
    Range.chrg.cpMin := 0
  else
    Range.chrg.cpMin := pOffset^;
  SaveRect := Range.rc;
  SaveIndex := SaveDC(Range.hdc);
  OldMap := SetMapMode(Range.hdc, MM_TEXT);
  RichEdit.Perform(EM_FORMATRANGE, 0, 0);
  try
    // Measure the text to find out how high the format rectangle
    // will be. The call sets fmtrange.rc.bottom to the actual height
    // required, if all characters in the selected range will fit into
    // a smaller rectangle,
    Range.chrg.cpMin := RichEdit.Perform( EM_FORMATRANGE, 0, Integer(@Range));
    if Range.chrg.cpMin >= RichEdit.GetTextLen then
    begin
      Rect.Bottom := ConvertUnit(Range.rc.Bottom, mmTWIPS, Units);
      Result := Rect.Bottom;
    end;
  finally
    RichEdit.Perform(EM_FORMATRANGE, 0, 0);
    RestoreDC(Range.hdc, SaveIndex);
    SetMapMode(Range.hdc, OldMap);
  end;
end;
Back to top
View user's profile
Kambiz
Administrator


Joined: 07 Mar 2003
Posts: 198

PostPosted: 21/05/03 21:48    Post subject: Reply with quote

Thank you very much for the code.
In the next release I'll add it to the component's interface.

Cheers,
Kambiz
Back to top
View user's profile Send e-mail Visit poster's website
Display posts from previous:   
   Reply to topic    DELPHI AREA Forum Index -> DELPHI AREA's Products All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB 2.0.6 © 2001, 2002 phpBB Group