Align text vertically in Edit controls Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
c7aesa7r
Posts: 209
Joined: 02 Jun 2016, 21:09

Align text vertically in Edit controls

Post by c7aesa7r » 19 Jun 2021, 21:44

I was searching at the forum for how to align a gui control text vertically in edit controls, found this topic:
https://www.autohotkey.com/boards/viewtopic.php?t=4673

Code: Select all

#NoEnv
Gui, Font, S10, Consolas
Gui, Add, Edit, -VScroll -E0x200 hwndHEdit1 w200 h30 Center, ABC
Gui, Add, Edit, -VScroll -E0x200 hwndHEdit2 w200 h30 Center, CDE
Gui, Show, , Gui
Edit_VCENTER(HEdit2)
Return

GuiClose:
ExitApp

Edit_VCENTER(HEDIT) { ; The Edit control must have the ES_MULTILINE style (0x0004 \ +Multi)!
   ; EM_GETRECT := 0x00B2 <- msdn.microsoft.com/en-us/library/bb761596(v=vs.85).aspx
   ; EM_SETRECT := 0x00B3 <- msdn.microsoft.com/en-us/library/bb761657(v=vs.85).aspx
   VarSetCapacity(RC, 16, 0)
   DllCall("User32.dll\GetClientRect", "Ptr", HEDIT, "Ptr", &RC)
   CLHeight := NumGet(RC, 12, "Int")
   SendMessage, 0x0031, 0, 0, , ahk_id %HEDIT% ; WM_GETFONT
   HFONT := ErrorLevel
   HDC := DllCall("GetDC", "Ptr", HEDIT, "UPtr")
   DllCall("SelectObject", "Ptr", HDC, "Ptr", HFONT)
   VarSetCapacity(RC, 16, 0)
   DTH := DllCall("DrawText", "Ptr", HDC, "Str", "W", "Int", 1, "Ptr", &RC, "UInt", 0x2400)
   DllCall("ReleaseDC", "Ptr", HEDIT, "Ptr", HDC)
   SendMessage, 0x00BA, 0, 0, , ahk_id %HEDIT% ; EM_GETLINECOUNT
   TXHeight := DTH * ErrorLevel
   If (TXHeight > CLHeight)
      Return False
   VarSetCapacity(RC, 16, 0)
   SendMessage, 0x00B2, 0, &RC, , ahk_id %HEDIT%
   DY := (CLHeight - TXHeight) // 2
   NumPut(DY, RC, 4, "Int")
   NumPut(TXHeight + DY, RC, 12, "Int")
   SendMessage, 0x00B3, 0, &RC, , ahk_id %HEDIT%
}

It don't align correctly in some fonts / size, does someone else know how i could achieve this kind of alignment?
image.png
image.png (1.2 KiB) Viewed 1892 times

just me
Posts: 9406
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Align text vertically in Edit controls

Post by just me » 20 Jun 2021, 05:07

Code: Select all

Edit_VCENTER(HEDIT) { ; The Edit control must have the ES_MULTILINE style (0x0004 \ +Multi)!
?

just me
Posts: 9406
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Align text vertically in Edit controls  Topic is solved

Post by just me » 20 Jun 2021, 06:47

Code: Select all

Gui, Add, Edit, -VScroll -E0x200 hwndHEdit1 w200 r3 Center, %String%
r3 -> 3 lines -> ES_MULTILINE

c7aesa7r
Posts: 209
Joined: 02 Jun 2016, 21:09

Re: Align text vertically in Edit controls

Post by c7aesa7r » 20 Jun 2021, 06:49

@just me if i apply this style after the button has already been created it does not work?

Code: Select all

#NoEnv
Gui, Font, S10, Consolas
Gui, Add, Edit, -VScroll -E0x200 0x004 hwndHEdit1 w200 h30 Center, ABC
Gui, Add, Edit, -VScroll -E0x200 hwndHEdit2 w200 h30 Center, CDE
Gui, Show, , Gui

Edit_VCENTER(HEdit1)

FileAppend, HEdit2: %HEdit2% `n,*

WinSet, Style, 0x0004, ahk_id %HEdit2%
WinSet, Style, 0x0004, %HEdit2%
WinSet, Style, +0x0004, ahk_id %HEdit2%
WinSet, Style, +0x0004, %HEdit2%
GuiControl, +Multi, %HEdit2%
Edit_VCENTER(HEdit2)
Return


Edit_VCENTER(HEDIT) { ; The Edit control must have the ES_MULTILINE style (0x0004 \ +Multi)!
   ; EM_GETRECT := 0x00B2 <- msdn.microsoft.com/en-us/library/bb761596(v=vs.85).aspx
   ; EM_SETRECT := 0x00B3 <- msdn.microsoft.com/en-us/library/bb761657(v=vs.85).aspx
   VarSetCapacity(RC, 16, 0)
   DllCall("User32.dll\GetClientRect", "Ptr", HEDIT, "Ptr", &RC)
   CLHeight := NumGet(RC, 12, "Int")
   SendMessage, 0x0031, 0, 0, , ahk_id %HEDIT% ; WM_GETFONT
   HFONT := ErrorLevel
   HDC := DllCall("GetDC", "Ptr", HEDIT, "UPtr")
   DllCall("SelectObject", "Ptr", HDC, "Ptr", HFONT)
   VarSetCapacity(RC, 16, 0)
   DTH := DllCall("DrawText", "Ptr", HDC, "Str", "W", "Int", 1, "Ptr", &RC, "UInt", 0x2400)
   DllCall("ReleaseDC", "Ptr", HEDIT, "Ptr", HDC)
   SendMessage, 0x00BA, 0, 0, , ahk_id %HEDIT% ; EM_GETLINECOUNT
   TXHeight := DTH * ErrorLevel
   If (TXHeight > CLHeight)
      Return False
   VarSetCapacity(RC, 16, 0)
   SendMessage, 0x00B2, 0, &RC, , ahk_id %HEDIT%
   DY := (CLHeight - TXHeight) // 2
   NumPut(DY, RC, 4, "Int")
   NumPut(TXHeight + DY, RC, 12, "Int")
   SendMessage, 0x00B3, 0, &RC, , ahk_id %HEDIT%
}


just me
Posts: 9406
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Align text vertically in Edit controls

Post by just me » 20 Jun 2021, 08:28

@c7aesa7r:
To create an edit control using the CreateWindow or CreateWindowEx function, specify the EDIT class, appropriate window style constants, and a combination of the following edit control styles. After the control has been created, these styles cannot be modified, except as noted.

Source: Edit Control Styles

Post Reply

Return to “Ask for Help (v1)”