?
Solved

How can I find the necessary height of a TLabel?

Posted on 1998-06-27
2
Medium Priority
?
227 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 600 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

Independent Software Vendors: 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

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…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month14 days, 6 hours left to enroll

801 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