Button width (and height) calculation

Report problems with documented functionality
just me
Posts: 9529
Joined: 02 Oct 2013, 08:51
Location: Germany

Button width (and height) calculation

Post by just me » 17 Nov 2020, 09:26

The second button control does not use the specified width:

Code: Select all

#NoEnv
Gui, Margin, 150, 20
Gui, Add, Button, w100, Common Button
Gui, Add, Button, w100 hwndHBTN, TextIsWiderThan100Pixel
ControlGet, Styles1, Style, , , ahk_id %HBTN%
Gui, Add, Button, w100 hwndHBTN, Text Is Wider Than 100 Pixel
ControlGet, Styles2, Style, , , ahk_id %HBTN%
Gui, Show, , Button Test
; MsgBox, 0, Styles, %Styles1%`n%Styles2%
Return
GuiClose:
ExitApp
I think it should.

See also: Button height calculation! Still true!

joefiesta
Posts: 498
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: Button width (and height) calculation

Post by joefiesta » 17 Nov 2020, 12:15

1. This certainly points out that the documentation for -Wrap (and word wrapping in general) is sparse, at best. Nowhere does it discuss how the wrapping is done (splitting a SPACE or, I assume, TAB positions. And if it Splits at other positions they are not defined.

2. One can force the width of 100 as shown below for Button2.

Code: Select all

#NoEnv
text2 := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
Gui, Margin, 150, 20
Gui, Add, Button, w100, Common Button
Gui, Add, Button, w100 hwndHBTN vButton2               ; will maintain width 100
Gui, Add, Button, w100 hwndHBTN -Wrap, %text2%
Gui, Add, Button, w100 hwndHBTN +Wrap, %text2%
ControlGet, Styles1, Style, , , ahk_id %HBTN%
Gui, Add, Button, w100 hwndHBTN, Text Is Wider Than 100 Pixel
ControlGet, Styles2, Style, , , ahk_id %HBTN%
GuiControl,, Button2, %text2%
Gui, Show, , Button Test
Return
GuiClose:
ExitApp


User avatar
TheDewd
Posts: 1513
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Button width (and height) calculation

Post by TheDewd » 17 Nov 2020, 13:18

@joefiesta,

Nice workaround! Setting the button text AFTER it's created. :thumbup:


I did find that setting the height of the button will make the text wrap:

Code: Select all

#SingleInstance, Force

ButtonText := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

Gui, Margin, 10, 10

Gui, Add, Button, w100 h24, % ButtonText
Gui, Add, Button, w100, % ButtonText
Gui, Show, w240 h240, Button Test
return

GuiClose:
	ExitApp
return

joefiesta
Posts: 498
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: Button width (and height) calculation

Post by joefiesta » 19 Nov 2020, 09:30

You must realize, in terms of setting the height and word wrap, that the text may be wrapped (always wrapped, I don't know, didn't test) but simply may not be visible if the height is not sufficient.

lexikos
Posts: 9635
Joined: 30 Sep 2013, 04:07
Contact:

Re: Button width (and height) calculation

Post by lexikos » 30 Jan 2021, 18:44

It's apparently intentional.
// Even if either height or width was already explicitly specified above, it seems best to
// override it if DrawText() says it's not big enough. REASONING: It seems too rare that
// someone would want to use an explicit height/width to selectively hide part of a control's
// contents, presumably for revelation later. If that is truly desired, ControlMove or
// similar can be used to resize the control afterward. In addition, by specifying BOTH
// width and height/rows, none of these calculations happens anyway, so that's another way
// this override can be overridden. UPDATE for v1.0.44.10: The override is now not done for Edit
// controls because unlike the other control types enumerated above, it is much more common to
// have an Edit not be tall enough to show all of it's initial text. This fixes the following:
// Gui, Add, Edit, r2 ,Line1`nLine2`nLine3`nLine4
// Since there's no explicit width above, the r2 option (or even an H option) would otherwise
// be overridden in favor of making the edit tall enough to hold all 4 lines.
// Another reason for not changing the other control types to be like Edit is that backward
// compatibility probably outweighs any value added by changing them (and the added value is dubious
// when the comments above are carefully considered).

Post Reply

Return to “Bug Reports”