Author: Tomas Rutkauskas
How to display a long text in a TLabel with an end ellipsis
Answer:
You must turn off AutoSize for the label, of course. Otherwise the current content
of the label determines where the string will be trimmed.
1 { ... }2 var3 S: string;
4 R: TRect;
5 { ... }6 begin7 {Get the text you want into S}8 S := 'this is way too long to fit into a label - trim it down';
9 {Make it unique, because DrawText is going to change it}10 UniqueString(S);
11 {Get the rectangle available for drawing}12 R := Label1.ClientRect;
13 {Make sure you're using the same font the label will use}14 Label1.Canvas.Font := Label1.Font;
15 {Ask Windows to trim it up with out replacing "&" by an underscore}16 DrawText(Label1.Canvas.Handle, PChar(S), Length(S), R, DT_END_ELLIPSIS or17 DT_MODIFYSTRING or DT_NOPREFIX);
18 {Now plug that trimmed text into Label1}19 Label1.Caption := S;
20 { ... }