How can I find the necessary height of a TLabel?
Posted on 1998-06-27
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
Font := Label1.Font;
H := TextHeight('0');
CLen := StrLen(C);
R := Rect(8, 0, 0, H);
DrawText(Label1.Canvas.Handle, C, CLen, R, DT_CalcRect Or DT_WORDBREAK or DT_LEFT);
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???