Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Height of TButton with Multiline caption does not fit its caption-text

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...':

image

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?

like image 840
user1580348 Avatar asked Dec 23 '22 15:12

user1580348


1 Answers

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).

like image 103
Andreas Rejbrand Avatar answered Jan 30 '23 03:01

Andreas Rejbrand