I have a TLabel
with EllipsisPosition
set to epEndEllipsis
and I need to be able to tell whether the text is currently clipped or not. Besides calculating the area required to display the text myself and comparing it with the actual dimensions of the label, has anyone come up with an easier/more elegant way of doing this?
Actually, calculating the required area in a fail-safe manner also doesn't appear to be as straight forward as it sounds... For instance TCanvas.GetTextHeight
does not take into account linebreaks.
TCustomLabel.DoDrawText
internally uses either DrawTextW
or DrawThemeTextEx
with the DT_CALCRECT
flag to determine whether it should use the ellipsis or not. There's quite a lot of code involved there, all of which is declared private
. Simply duplicating all that code would not exactly qualify as "elegant" in my book...
Any ideas?
(I'm using Delphi 2010 in case anyone comes up with a Delphi-version-specific solution)
Update 1: I now figured out that I can simply call TCustomLabel.DoDrawText(lRect, DT_CALCRECT)
directly (which is merely declared protected
) to let the label perform the required size calculation without having to duplicate its code. I just have to make sure to either temporarily set EllipsisPosition
to epNone
or use a temporary label instance altogether. This is actually not that bad and I might just go with it if noone can think of an even simpler solution.
Update 2: I have now added my solution as a separate answer. It turned out to be rather more straight-forward than I anticipated so there probably is no easier/better way to do it but I'll leave this question open for a little while longer anyway just in case.