Home General
New Blog Posts: Merging Reports - Part 1 and Part 2

Getting the Width of an Autosize Label @ Runtime

edited July 2004 in General
Hi,

I am hoping to find a way to determine the width of an autosized TppLabel in
the BeforePrint event at runtime.

I'm printing address labels, and if any address lines are to long to fit on
the label at the standard font size, I want to decrease the font size until
it does fit. I've tried just changing the font size in the event, but the
the TppLabel component does not resize itself at print time.

Is there any way that I can get a handle to the actual canvas that the
TppReport is drawing on? Then I could use TCanvas.TextWidth('zyx') to get
the width information I need.

thanks,
Bill.

Comments

  • edited July 2004
    Hi Bill,

    It is possible to access the Printer canvas simply by including the
    ppPrintr.pas file in your uses clause, then accessing the ppPrinter.Canvas
    property. If you prefer to use screen units, you can get access to the
    canvas used with the screen device by accessing the
    TppViewer.ScreenDevice.Canvas property. You will need to include the
    ppViewr.pas file in your uses clause and type cast Report.PreviewForm.Viewer
    as a TppViewer.

    --
    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited July 2004
    Hi Nico,

    Thanks for your quick response! I'm certain that i'll find uses for the
    various canvases.
    However I've managed to get the TppLabel to "AdjustBounds" at runtime by
    setting
    the TppReport.Printing property to false momentarily during the BeforePrint
    event like this:

    procedure TMyDataModule.ReportBeforePrint(Sender:TObject);
    begin
    MyLabel.Caption := 'Some Extra Long Line Of Text';
    MyLabel.Font.Height := cStandardHeight;

    // Cause TppLabel.AdjustBounds to fire.
    Report.Printing := False;
    try
    MyLabel.AutoSize := False;
    MyLabel.AutoSize := True;
    finally
    Report.Printing := True;
    end;

    if MyLabel.Width > cMaxLabelWidth then begin
    MyLabel.Font.Height := CalculateASmallerFontHeightHere();
    end;

    end;

    It seems to work, but do you forsee any problems with this "hack" technique
    of setting
    Report.Printing manually?

    thanks,
    Bill.


  • edited July 2004
    Hi Bill,

    The method you use below is probably not the safest way to go about changing
    the properties of a report component. By setting the Report.Printing
    property yourself, you may be altering some of the report engine processes
    which in the future as your report becomes more sophisticated, could cause
    issues. For now, I would say if it works, don't fix it, but keep this in
    mind if other issues begin to arise in the future.

    --
    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
This discussion has been closed.