Solved

How can I find the necessary height of a TLabel?

Posted on 1998-06-27
2
224 Views
Last Modified: 2010-08-05
I am using a TLabel component with WordWrap=True and AutoSize=False.  (Screen space is limited).  The label is Red.  Since the text in the box can be different things, the actual number of lines of text changes.  What happens now is that sometimes the box is not full and so there is a blank line of red where there could have been extra text.  What I want to be able to do is find out how much space (or how many lines) the text occupies so I can change the Label.Height property so that blank red area is hidden.

I have been looking at the StdCtrls.Pas library code file and put together some code like this:

  with Label1.Canvas do
    begin
      Font := Label1.Font;
      H := TextHeight('0');
      StrPCopy(C, Label1.Caption);
      CLen := StrLen(C);
      R := Rect(8, 0, 0, H);
      DrawText(Label1.Canvas.Handle, C, CLen, R, DT_CalcRect Or DT_WORDBREAK or DT_LEFT);
    end;

As I understand it, it is SUPPOSED to return the size of the area needed in R but I'm getting some other data.  Any ideas???

Thanks!
/MP
0
Comment
Question by:mikepj
2 Comments
 
LVL 3

Accepted Solution

by:
AndreasSchindler earned 200 total points
ID: 1356075
const
  Alignments: array[TAlignment] of Word = (DT_LEFT, DT_RIGHT, DT_CENTER);
  WordWraps: array[Boolean] of Word = (0, DT_WORDBREAK);

var rect:TRect;
    Text:String;
    Drawstyle:Integer;
    h:Integer;
begin
    Rect := Label1.ClientRect;
    Text:=LAbel1.Caption;
    DrawStyle := DT_EXPANDTABS or WordWraps[LAbel1.WordWrap] or Alignments[LAbel1.Alignment];
    h:=DrawText(LAbel1.Canvas.Handle, PChar(Text), Length(Text), Rect, DrawStyle or DT_CALCRECT);
    LAbel1.height:=h;
end;

0
 
LVL 2

Author Comment

by:mikepj
ID: 1356076
Thank you Andreas,

After a bit of work I got the following to work...  (There was something wrong with the DrawStyle arrays and I don't need them so I took them out).

Thank you for your help; the code works great!
/MP

var
  A: array[0..100] of Char;
  rect:TRect;
  Text:String;
  Drawstyle:Integer;
  h:Integer;
begin
  Label1.Caption:=Memo1.Lines[0];
  Rect := Label1.ClientRect;
  Text:=LAbel1.Caption;
  StrPCopy(A, Text);
  DrawStyle := DT_EXPANDTABS or DT_WordBreak Or DT_Left;
  h:=DrawText(LAbel1.Canvas.Handle, A, Length(Text), Rect, DrawStyle or DT_CALCRECT);
  LAbel1.height:=h;
end;

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question