How to modify the position of the GuiControl after Gui.Add? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Giresharu
Posts: 17
Joined: 16 Apr 2024, 07:21
Contact:

How to modify the position of the GuiControl after Gui.Add?

Post by Giresharu » 22 May 2024, 04:15

First, let me explain my goal: I want to display a small window with text centered on it, regardless of the specified text size. The window's size should remain fixed, even if the text cannot fit entirely. Adding +Center option in Gui.Add doesn't achieve this because it doesn't adjust the text position based on the window size. So, I found that I can manually set the position of the text. However, to calculate its centered position based on the text's width and height, I need to use ControlGetPos(, , &w, &h, text.Hwnd), meaning I can't calculate the text position before Add. So, I tried the following code:

Code: Select all

    size := 120
    boxSize := 240
    myGui := Gui("-SysMenu +ToolWindow +AlwaysOnTop -Caption -DPIScale +E0x20")
    myGui.BackColor := 0xBB252525
    myGui.SetFont("c0xCC5050 s" size " bold q5", "微软雅黑")

    text := myGui.Add("Text", "+Center", "あ")
    ControlGetPos(, , &w, &h, text.Hwnd)

    offsetX := (boxSize - w) / 2
    offsetY := (boxSize - h) / 2
    MsgBox(offsetX " " offsetY)
    text.Opt("+X" offsetX " +Y" offsetX)
    ControlGetPos(&x, &y, , , text.Hwnd)
    MsgBox(x " " y)

    myGui.Show("NoActivate w240 h240")
However, I found that the Opt method doesn't seem to modify the position of the text. The values for offsetX/Y I obtained were 39.5/28.5 respectively, while ControlGetPos(&x, &y, , , text.Hwnd) gave me x/y as 150/90.

How can I modify the position of the text? Or in other words, how can I center the text within the window while restricting its size? (I prefer manually adjusting the coordinates because the baseline of the font I'm using is offset, visually making the entire character appear shifted downwards. However, I haven't found any AHK GUI API specifically for aligning with non-Western characters, so manual adjustment is necessary to achieve the desired effect.)
niCode
Posts: 319
Joined: 17 Oct 2022, 22:09

Re: How to modify the position of the GuiControl after Gui.Add?  Topic is solved

Post by niCode » 22 May 2024, 05:32

I'm tired so maybe I'm missing a key detail to what you're really after but this seems to center the text:

Code: Select all

size := 120
boxSize := 240
myGui := Gui("-SysMenu +ToolWindow +AlwaysOnTop -Caption -DPIScale +E0x20")
myGui.BackColor := 0xBB252525
myGui.SetFont("c0xCC5050 s" size " bold q5", "微软雅黑")

text := myGui.Add("Text", "+Center", "あ")
text.GetPos(,, &tW, &tH)        ; text width and height

myGui.Show("Hide w240 h240")    ; allows a hidden window to be moved or resized without showing it
myGui.GetPos(,, &gW, &gH)       ; gui width and height

text.Move((gW - tW)/2, (gH - tH)/2)
myGui.Show("NoActivate")
Giresharu
Posts: 17
Joined: 16 Apr 2024, 07:21
Contact:

Re: How to modify the position of the GuiControl after Gui.Add?

Post by Giresharu » 22 May 2024, 07:12

@niCode
Thank you! It works!
Post Reply

Return to “Ask for Help (v2)”