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