Adjust text inside a button control? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Adjust text inside a button control?

03 May 2023, 04:10

Hey folks!

I have a script with many Gui buttons. A short example here:

Code: Select all

#Requires AutoHotkey v2.0.2
#SingleInstance Force
MyGui := Gui()
MyGui.Add("Button", "w250", "More than one word button name.")  ;Looks bad for my use
MyGui.Add("Button", "wp", "OK.")                                ; looks ugly for my use
MyGui.Add("Button", "wp", "More than one word button name.                (COW)")                       ;Look fine for my use but really ugly use of spacing
MyGui.Add("Button", "wp", "OK.                                                              (WOW)")     ;Look fine for my use but really ugly use of spacing
MyGui.Show()
I've encountered an issue with button naming that has forced me to set the width of all buttons in my script according to the one with the longest name.
However, this has resulted in some buttons having different heights, which is not ideal. Ideally, I'd like all buttons to have the same width and height.

I'm wondering if there is a more user-friendly way to achieve this instead of using space, as shown in the example above?
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Adjust text inside a button control?

03 May 2023, 04:18

No sure if this is what you mean, but you can use the h option to set the height.
In this example I've set it to 40:

Code: Select all

#Requires AutoHotkey v2.0.2
#SingleInstance Force
MyGui := Gui()
MyGui.Add("Button", "w250 h40", "More than one word button name.")  ;Looks bad for my use
MyGui.Add("Button", "wp h40", "OK.")                                ; looks ugly for my use
MyGui.Add("Button", "wp h40", "More than one word button name.                (COW)")                       ;Look fine for my use but really ugly use of spacing
MyGui.Add("Button", "wp h40", "OK.                                                              (WOW)")     ;Look fine for my use but really ugly use of spacing
MyGui.Show()
Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: Adjust text inside a button control?

03 May 2023, 04:52

What I want is for it to look like in lines 6 and 7 of my code. I don't want to change the height or width.
As you can see, I'm using too much space so that "OK" starts from the beginning of the button and "(WOW)" is at the end. That's the look I want.
However, I'm wondering if there are any other magical techniques, like "wp," for moving text inside a button to where I want it.

See the image. I want to here display the naming as shown in the last two buttons.
image.png
image.png (5.48 KiB) Viewed 1018 times
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Adjust text inside a button control?

03 May 2023, 05:09

It's still not clear to me what you need. Do you want all buttons to be left alligned ? In that case you could add Left in the options:

Code: Select all

#Requires AutoHotkey v2.0.2
#SingleInstance Force
MyGui := Gui()
MyGui.Add("Button", "w250 left", " More than one word button name.")  ;Looks bad for my use
MyGui.Add("Button", "wp left", " OK.")                                ; looks ugly for my use
MyGui.Add("Button", "wp", "More than one word button name.                (COW)")                       ;Look fine for my use but really ugly use of spacing
MyGui.Add("Button", "wp", "OK.                                                              (WOW)")     ;Look fine for my use but really ugly use of spacing
MyGui.Show()
niCode
Posts: 317
Joined: 17 Oct 2022, 22:09

Re: Adjust text inside a button control?

03 May 2023, 05:21

WalkerOfTheDay wrote:
03 May 2023, 05:09
It's still not clear to me what you need. Do you want all buttons to be left alligned ? In that case you could add Left in the options:
I believe they were just hoping there was a special command or something that would automagically fill any extra room with spaces.

As a pretend example:

Code: Select all

MyGui.Add("Button", "wp", "OK." A_Fill "(WOW)")     ;Look fine for my use but really ugly use of spacing
Where A_Fill is a pretend built-in variable for guis that adds the correct amount of space between those two strings to push them apart so it looks like the first string is left-aligned and the second string is right-aligned, all in the same button.
User avatar
mikeyww
Posts: 27305
Joined: 09 Sep 2014, 18:38

Re: Adjust text inside a button control?

03 May 2023, 05:31

I think the idea is to right-align text within a button control, though I do not know a way to do that. Someone here may know. There are some posts about how to determine the character width based on the font and font size, so you might be able to use that approach to compute the amount of space needed, if you also determine the width of a space.
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Adjust text inside a button control?

03 May 2023, 06:26

mikeyww wrote:
03 May 2023, 05:31
I think the idea is to right-align text within a button control, though I do not know a way to do that. Someone here may know. There are some posts about how to determine the character width based on the font and font size, so you might be able to use that approach to compute the amount of space needed, if you also determine the width of a space.
Right alligning can be set in the options, can't it ? This seems to work for me at least:

Code: Select all

MyGui.Add("Button", "wp right", " OK.")       
User avatar
mikeyww
Posts: 27305
Joined: 09 Sep 2014, 18:38

Re: Adjust text inside a button control?

03 May 2023, 06:29

As far as I know, you have a choice of one type of text alignment, not multiple types within a control.
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Adjust text inside a button control?

03 May 2023, 06:36

Okay. I did not get that that was what he meant.

I wouldn't know either how to deal with that.
Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: Adjust text inside a button control?

03 May 2023, 07:26

Yes! I wanted like the first part of the sentence to be left-aligned and the last word to be right-aligned.

Lexikos, please create a built-in variable called "A_Fill". :)

Also, Mikeyww, I constantly challenge myself to improve my knowledge, but I know I will never be as skilled as you. It's simply a natural limitation. :D

Thank you, everyone. I will use the right/left alignment method along with space to achieve the desired formatting. :beer:
User avatar
boiler
Posts: 17328
Joined: 21 Dec 2014, 02:44

Re: Adjust text inside a button control?

03 May 2023, 12:23

Another option, although more involved, is to use images for buttons, which you can make look exactly how you would like them -- alignment, font, background color, icons/images, etc. There are libraries/classes that have been posted on the forum (at least for v1) that allow you to use multiple images per button so they can change on hover and while being clicked.
User avatar
FanaticGuru
Posts: 1908
Joined: 30 Sep 2013, 22:25

Re: Adjust text inside a button control?  Topic is solved

03 May 2023, 18:07

Krd wrote:
03 May 2023, 04:10

Code: Select all

#Requires AutoHotkey v2.0.2
#SingleInstance Force
MyGui := Gui()
MyGui.Add("Button", "w250", "More than one word button name.")  ;Looks bad for my use
MyGui.Add("Button", "wp", "OK.")                                ; looks ugly for my use
MyGui.Add("Button", "wp", "More than one word button name.                (COW)")                       ;Look fine for my use but really ugly use of spacing
MyGui.Add("Button", "wp", "OK.                                                              (WOW)")     ;Look fine for my use but really ugly use of spacing
MyGui.Show()
I've encountered an issue with button naming that has forced me to set the width of all buttons in my script according to the one with the longest name.
However, this has resulted in some buttons having different heights, which is not ideal. Ideally, I'd like all buttons to have the same width and height.

I'm wondering if there is a more user-friendly way to achieve this instead of using space, as shown in the example above?

Here is an example to at least calculate the number of spaces to use to make text take up the whole button width.

Code: Select all

MyGui := Gui()
MyButton1 := MyGui.Add("Button", "w250", "More|than|one|word|button|name.")  
MyButton2 := MyGui.Add("Button", "wp", "OK.|(WOW)")                                
MyButton3 := MyGui.Add("Button", "wp", "More than one word button name.|(COW)") 
JustifyCtrlText(MyButton4 := MyGui.Add("Button", "wp", "OK.|(WOW)|Now")) ; Another way of calling function
MyGui.Show()

JustifyCtrlText(MyButton1)
JustifyCtrlText(MyButton2)
JustifyCtrlText(MyButton3)

JustifyCtrlText(GuiCtrlObj)
{
	SpaceSize := (GuiCtrlTextSize(GuiCtrlObj, '          ').Width / 10)
	CtrlTextSegments := StrSplit(GuiCtrlObj.Text, '|')
	CtrlTextSegments.Size := GuiCtrlTextSize(GuiCtrlObj, RegExReplace(GuiCtrlObj.Text, '\|')).Width
	GuiCtrlObj.GetPos(, , &Width), Width -= 10 ; 5 margin on each side
	NumberSpaces := Floor((Width - CtrlTextSegments.Size) / SpaceSize - SpaceSize)
	Spaces := Array()
	For X in Range(CtrlTextSegments.Length - 1, 1, -1) 
		ThisSpaces := Ceil(NumberSpaces / X), NumberSpaces -= ThisSpaces, Spaces.Push(StrReplace(Format('{:' ThisSpaces '}', ''), ' ', ' '))
	For Index, CtrlTextSegment in CtrlTextSegments
	{
		Result .= CtrlTextSegment
		try Result .= Spaces[Index]
	}
	GuiCtrlObj.Text := Result
	
	GuiCtrlTextSize(GuiCtrlObj, Text)
	{
		Static WM_GETFONT := 0x0031, DT_CALCRECT := 0x400
		hDC := DllCall('GetDC', 'Ptr', GuiCtrlObj.Hwnd, 'Ptr')
		hPrevObj := DllCall('SelectObject', 'Ptr', hDC, 'Ptr', SendMessage(WM_GETFONT, , , GuiCtrlObj), 'Ptr')
		height := DllCall('DrawText', 'Ptr', hDC, 'Str', Text, 'Int', -1, 'Ptr', buf := Buffer(16), 'UInt', DT_CALCRECT)
		width := NumGet(buf, 8, 'Int') - NumGet(buf, 'Int')
		DllCall('SelectObject', 'Ptr', hDC, 'Ptr', hPrevObj, 'Ptr')
		DllCall('ReleaseDC', 'Ptr', GuiCtrlObj.Hwnd, 'Ptr', hDC)
		Return { Width: Round(width * 96 / A_ScreenDPI), Height: Round(height * 96 / A_ScreenDPI) }
	}
	
	Range(Start, Stop, Step := 1) => (&n) => (n := Start, Start += Step, Step > 0 ? n <= Stop : n >= Stop)
}

Basically | is replaced with spaces to make the width right. You can have multiple |. Might need to be some adjustments here and there, as I did not test it much. Although, I did try a few different fonts and sizes without a problem.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: Adjust text inside a button control?

04 May 2023, 05:20

Great! Thank you, Boiler, for that idea that I hadn't even considered.

FanaticGuru, what an awesome code you've written! :)

Thank you so much, everyone.
User avatar
FanaticGuru
Posts: 1908
Joined: 30 Sep 2013, 22:25

Re: Adjust text inside a button control?

04 May 2023, 11:57

boiler wrote:
03 May 2023, 12:23
Another option, although more involved, is to use images for buttons, which you can make look exactly how you would like them -- alignment, font, background color, icons/images, etc. There are libraries/classes that have been posted on the forum (at least for v1) that allow you to use multiple images per button so they can change on hover and while being clicked.

You can use a image as a button or you can also add an image to a button like this: GuiButtonIcon

The examples show mixing text and images but you could use just an image of text.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Bing [Bot] and 27 guests