Board index » delphi » Sizing of TLabel by Font Size?

Sizing of TLabel by Font Size?

How can you size a TLabel object automatically regardless of font being
used?  It seems that Autosize only works if you change the Text.  Is there
another way.  I am having trouble with truncation of text when a larger
font is used than the font I on the computer that I designed the form on.

gjack...@webstar.net

 

Re:Sizing of TLabel by Font Size?


"Gregory E. Jackson" <gjack...@webstar.net> wrote:

Quote
> How can you size a TLabel object automatically regardless of font being
> used?  It seems that Autosize only works if you change the Text.  Is there
> another way.  I am having trouble with truncation of text when a larger
> font is used than the font I on the computer that I designed the form on.

Gregory,

I ran into this same problem recently and devised the following solution,
and although there may be other ways to solving the problem, there may be
other or better ways to accomplish it.

In a form, I have a TLabel component called "Comments" and an array of text
lines, Txt. In the FormCreate method, a TSize and other working variables
are needed.
   var Size : TSize;
       i,MaxLineLength,StringHeight : Integer;
       s : String;

The first thing to do is to assign the TLabel font to the TLabel's Canvas
font since the GetTextExtentPoint32 seems only to work on the Canvas
handle and the canvas font doesn't seem to get set correctly otherwise.

  Comments.Canvas.Font.Assign(Comments.Font);
  s := '';
  MaxLineWidth := 0;
  StringHeight := 0;
  for i := 1 to 6 do begin
     s := s + Txt[i] + crlf;
     if GetTextExtentPoint32(Comments.Canvas.Handle,
             @Txt[i][1],Length(Txt[i]),Size) then begin
       LineLength := Size.cx;
       LineHeight := Size.cy;
       Inc(StringHeight,LineHeight);
       if LineLength>MaxLineWidth then
           MaxLineWidth := LineLength;
     end;
  end;

In that section of code each line of text is appended to a string to be
assigned to the TLabel.Caption property. The length and height in pixels of
each text line is computed by GetTextExtentPoint32 and the total string
height is accumulated and the maximum line length is determined.

In the next section, "Comments" is adjusted according to those values.
Since this TLabel component is in a TPanel component, "Panel1", its
dimensions also need adjustment. (MaxLong is a function in the Turbo Power
SysTools library.)  OldHeight & OldWidth are used to adjust the dimensions
of further panels and bevels that I left out for brevity.

  with Comments do begin
     OldHeight := Height;
     OldWidth  := Width;
     Caption := s;
     Height := StringHeight;
     MaxLineWidth := StUtils.MaxLong(MaxLineWidth,Width);
     Width := MaxLineWidth;
  end;
  with Panel1 do begin
     Height := StringHeight;
     Width := MaxLineWidth;
  end;

This technique has worked 100% for me using Delphi-2.01 on both Win95 and
Win-NT for a variety of font sizes and styles.

Good luck,
   Don

  +--------------------------------------------------------+
  | Donald Moe                 don...@ix.netcom.com        |
  | Jupiter, Florida                                       |
  +--------------------------------------------------------+

Re:Sizing of TLabel by Font Size?


"Gregory E. Jackson" <gjack...@webstar.net> wrote:

Quote
>How can you size a TLabel object automatically regardless of font being
>used?  It seems that Autosize only works if you change the Text.  Is there
>another way.  I am having trouble with truncation of text when a larger
>font is used than the font I on the computer that I designed the form on.

Set the form's Scaled property to TRUE, and it's AUTOSCROLL property
to FALSE.

The form will then scale all it's child controls by the ratio of the
font pixels per inch the form was designed with and the font pixels
per inch discovered at runtime.

-Danny Thorpe
 dtho...@subliminal.com

Re:Sizing of TLabel by Font Size?


Dear Danny,

Obviously, I'm missing something. I need a D1 and D2 solution.  I have
tested your suggestion using D1.02 on Win 95a.

I design in Super VGA (800 x 600).  My solution is complicated (I wrote a
generic scaling unit) and ugly - but it works. I've discovered the Scaleby
function which is OK but incomplete - but an automatic solution like yours
is what I want!

Unfortunately, I can't get it to work.  The problem seems to be the Forms
PixelsPerInch property remains at 96 - whether it runs in SuperVGA or VGA
(as tested in the CreateForm and ShowForm procedures).

Does your method work? :-) Please give more details.

--
Paul Motyer  12:01 Tues, 10th September, 1996 EST
pa...@linuxserver.pccity.com.au
SoftStuff, Croydon, Vic,  Australia, 3136.

dtho...@subliminal.com wrote in article <50vgmj$...@news.scruz.net>...

Quote
> "Gregory E. Jackson" <gjack...@webstar.net> wrote:

> >How can you size a TLabel object automatically regardless of font being
> >used?  It seems that Autosize only works if you change the Text.  Is
there
> >another way.  I am having trouble with truncation of text when a larger
> >font is used than the font I on the computer that I designed the form
on.

> Set the form's Scaled property to TRUE, and it's AUTOSCROLL property
> to FALSE.

> The form will then scale all it's child controls by the ratio of the
> font pixels per inch the form was designed with and the font pixels
> per inch discovered at runtime.

> -Danny Thorpe
>  dtho...@subliminal.com

Re:Sizing of TLabel by Font Size?


Quote
Don Moe (don...@ix.netcom.com) wrote:

: "Gregory E. Jackson" <gjack...@webstar.net> wrote:

: > How can you size a TLabel object automatically regardless of font being
: > used?  It seems that Autosize only works if you change the Text.  Is there
: > another way.  I am having trouble with truncation of text when a larger
: > font is used than the font I on the computer that I designed the form on.

: Gregory,

<Don Moes ideas snipped to keep it short>

I needed to do something similar back when I used VB. Heres what I did:

Determine the ratio of FontSize to Label size (height & width). In my case
this ratio was constant. When the fontsize changes, change the label size
appropriately.

Don't know if it'll work in Delphi.

Lottsa Luck

Robert

 ========================================================================
   Robert Jones
   SK Computer Solutions
   Internet:                                 Fido-Net:
    robe...@scs.unr.edu                          Robert Jones
    rob...@alpinecom.com                            1:213/742

  *Programming is an art form that fights back.*
  *Debugging: The process of removing bugs.*
  *Programming: The process of putting them in.*
 =========================================================================

Other Threads