Solved

How can I find the necessary height of a TLabel?

Posted on 1998-06-27
2
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: 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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

737 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