Solved

How can I find the necessary height of a TLabel?

Posted on 1998-06-27
2
221 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now