In a Delphi 10.4.2 Win32 VCL Application on Windows 10, I have a TButton
with WordWrap = True
and Caption = 'SAVE TO INTERNET BOOKMARKS FOLDER...'
:
As you can see from the screenshot, the height of the button does not automatically fit its Caption
text.
Is there a feature in TButton
to achieve this automatically, or do I have to adjust this manually?
No, there is no automatic adjustment of the height of a TButton
offered by the VCL.
If you change the font of the button's caption, or make it multi-line, you typically have to adjust the button's height yourself explicitly.
As a comparison, TEdit
does have an AutoSize
property. This doesn't map to a feature of the Win32 EDIT control (like a window style), but is implemented purely in the VCL (see Vcl.StdCtrls.TCustomEdit.AdjustHeight()
).
However, I just discovered that the underlying Win32 BUTTON control does offer this functionality, via the BCM_GETIDEALSIZE
message or Button_GetIdealSize()
macro:
uses
..., Winapi.CommCtrl;
var
S: TSize;
begin
S.cx := Button1.Width;
if Button_GetIdealSize(Button1.Handle, S) then
Button1.Height := S.cy;
This will set the height given the button's current text and desired width. If S
is initially zeros, you get the button's preferred width and height.
It is not uncommon that a Win32 control offers more functionality than its VCL wrapper exposes, so it is often a good idea to have a look at the Win32 documentation, particularly the messages you can send to the control (and also its styles).