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

How to calculate the TextWidth for a TppLabel.Caption

edited March 2006 in General
Hi,

I need to print a rectangle round some TppLabel.Caption. I tried the using
TppLabel.Width but this isn't very accurate.
The rectangles width has to be the TextWidth(TppLabel.Caption) + 10. How do
I do this in reportbuilder?

Greetings,
Filip Moons

Comments

  • edited March 2006
    Hi Filip,

    You can try using the TCanvas.TextWidth routine in Delphi. The canvas of a
    TBitmap will give you the width in screen pixels and the Printer.Canvas will
    it to you in printer pixels. Then you will need to convert that value into
    your own measurement units.

    --
    Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com

    Best Regards,

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

    It won't work. The calculated length is always short. I get the same results
    using both bitmap/printer methods. What's wrong?

    TppChildReport.Units = utScreenPixels

    ABitMap := TBitMap.Create;
    try
    ABitMap.Canvas.Font.Style := ppAYPLDOS_OMS_PLC.Font.Style;
    ABitMap.Canvas.Font.Name := ppAYPLDOS_OMS_PLC.Font.Name;
    ppAYPLDOS_LINE_PLC.Width :=
    ABitMap.Canvas.TextWidth(ppAYPLDOS_OMS_PLC.Caption);
    ppline.Width := ABitMap.Canvas.TextWidth(ppAYPLDOS_OMS_PLC.Caption);
    ppbox.Width := ABitMap.Canvas.TextWidth(ppAYPLDOS_OMS_PLC.Caption);
    finally
    FreeAndNil(ABitMap);
    end;

    TppChildReport.Units = utPrinterPixels

    TppChildReport.Printer.Canvas.Font.Style := ppAYPLDOS_OMS_PLC.Font.Style;
    TppChildReport.Printer.Canvas.Font.Name := ppAYPLDOS_OMS_PLC.Font.Name;
    ppline.Width :=
    TppChildReport.Printer.Canvas.TextWidth(ppAYPLDOS_OMS_PLC.Caption);
    ppbox.Width :=
    TppChildReport.Printer.Canvas.TextWidth(ppAYPLDOS_OMS_PLC.Caption);

    Greetings,
    Filip Moons

  • edited March 2006
    In my testing the following code produced a line that matched the width of a
    TppLabel exactly (printer and screen).

    uses
    ppPrintr,
    ppUtils,
    ppTypes;

    procedure TForm1.ppHeaderBand1BeforePrint(Sender: TObject);
    var
    liTextWidth: Integer;
    liTextWidthMM: Integer;
    lCanvas: TCanvas;
    begin

    lCanvas := ppPrinter.Canvas;

    lCanvas.Font := ppLabel1.Font;

    liTextWidth := lCanvas.TextWidth(ppLabel1.Text);

    liTextWidthMM := ppToMMThousandths(liTextWidth, utPrinterPixels,
    pprtHorizontal, ppPrinter);

    ppLine1.Width := ppFromMMThousandths(liTextWidthMM, utInches,
    pprtHorizontal, ppPrinter);

    end;

    --
    Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited March 2006
    Ok, your solution works. Thanks!

    Greetings,
    Filip Moons

This discussion has been closed.