Solved

How can I find the necessary height of a TLabel?

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 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

12 Experts available now in Live!

Get 1:1 Help Now