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%
}