Edit - Vertically Align Text

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
TheDewd
Posts: 1513
Joined: 19 Dec 2013, 11:16
Location: USA

Edit - Vertically Align Text

24 Sep 2014, 13:42

Is it possible to vertically align text in an edit control with a fixed height? I know that it's possible to horizontally align.

See attached image. The text is aligned to the top, with large padding at the bottom.
Edit_Example.png
Edit_Example.png (4.93 KiB) Viewed 4673 times

Code: Select all

Gui, Font, S20
Gui, Add, Edit, x10 y10 w200 h60 -Multi, Data
Gui, Show, w220 h80, Gui Test
User avatar
TheDewd
Posts: 1513
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Edit - Vertically Align Text

24 Sep 2014, 16:17

Let me explain further. I am attempting to create an attractive Gui by using a background image, and then placing controls on top of it.

I want the first visible edit control for length to fit the background image's edit field perfectly, which I could achieve with the vertical alignment of the text, but I do not believe that is possible.

How can I fit the edit control perfectly inside the image? I know that I could offset the y-coordinates, but I don't know if that's the best route to go... Any help please?
Form.png
Form.png (7.71 KiB) Viewed 4671 times

Code: Select all

; ===================================================================================
; NAME .........: NEW INTERFACE
; DESCRIPTION ..: INTERFACE USING A BACKGROUND IMAGE.
; AHK VERSION ..: AHK_L 1.1.16.04 (UNICODE 32-BIT) - SEPTEMBER 16, 2014
; PLATFORM .....: WINDOWS 7 PROFESSIONAL SP1 32-BIT
; LANGUAGE .....: ENGLISH (EN-US)
; ===================================================================================

; GLOBAL ============================================================================
#SingleInstance Force ; ALLOWS ONLY ONE RUNNING INSTANCE OF SCRIPT.
#Persistent ; KEEPS SCRIPT PERMANENTLY RUNNING UNTIL TERMINATED.
#NoEnv ; AVOIDS CHECKING EMPTY VARIABLES TO SEE IF THEY ARE ENVIRONMENT VARIABLES.
#Warn ; ENABLES WARNINGS TO ASSIST WITH DETECTING COMMON ERRORS.
SendMode Input ; RECOMMENDED FOR NEW SCRIPTS DUE TO ITS SUPERIOR SPEED AND RELIABILITY.
SetWorkingDir %A_ScriptDir% ; ENSURES A CONSISTENT STARTING DIRECTORY.
SetBatchLines, -1 ; RUNS SCRIPT AT MAXIMUM SPEED. NEVER SLEEPS.
;#NoTrayIcon ; DISABLES THE SHOWING OF A TRAY ICON.
;Menu, Tray, Icon, Shell32.dll, 174 ; REPLACES THE DEFAULT TRAY ICON.

Gui, Add, Picture, x0 y0 w600 h600, Form.png ; Background Image

Gui, Add, Edit, x0 y0 w0 h0 -E0x200 +ReadOnly, Focus ; Initial Focus

Gui, Font, S18 C666666 ; Set Font Options
Gui, Add, Edit, x32 y132 w119 h35 -Multi -E0x200 +Limit5 +BackgroundTrans, 20.00
Gui, Font ; Reset Font Options

Gui, Add, Edit, x32 y216 w536 h231 -E0x200 +BackgroundTrans, Data

Gui, Show, w600 h600, New Interface

Return ; END AUTOMATIC EXECUTION

; LABELS ============================================================================
MenuHandler:
Return

GuiClose:
	ExitApp
Return
just me
Posts: 9553
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Edit - Vertically Align Text

25 Sep 2014, 01:42

This might work for your special case of a multiline Edit containing a single line of limited width:

Code: Select all

#NoEnv
Gui, Font, S20
Gui, Add, Edit, x10 y10 w200 h60 Center +hwndHEDIT -VScroll +Limit5 -E0x200, Data
Edit_VCENTER(HEDIT)
Gui, Show, w220 h80, Gui Test
Return
GuiClose:
ExitApp

Edit_VCENTER(HEDIT) {
   ; 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, 0x00B2, 0, &RC, , ahk_id %HEDIT%
   RCHeight := NumGet(RC, 12, "Int") - NumGet(RC, 4, "Int")
   DY := (CLHeight - RCHeight) // 2
   NumPut(NumGet(RC, 4, "Int") + DY, RC, 4, "Int")
   NumPut(NumGet(RC, 12, "Int") + DY, RC, 12, "Int")
   SendMessage, 0x00B3, 0, &RC, , ahk_id %HEDIT%
}
User avatar
TheDewd
Posts: 1513
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Edit - Vertically Align Text

25 Sep 2014, 10:14

I'm having trouble getting the function values correct for the edit control with a height of 35px, and a S20 font size. Tried to moody CLHeight & RCHeight, but I really don't how to get it perfect.
just me
Posts: 9553
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Edit - Vertically Align Text

25 Sep 2014, 10:28

Font size s20 and a height of 35 pixels seem to create a single-line edit control with a line height of 31 pixels on Win 8.1 Pro. If you want to get the line moved down 2 pixels ((35 - 31) / 2) try to add the option +Multi.
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Edit - Vertically Align Text

27 Apr 2015, 23:04

Hello, just me, your example code doesn't work for me.

I found another code that works, but needs to specify some value manually:

Code: Select all

; http://www.autohotkey.com/board/topic/80698-margin-for-edit/?p=512948

tp := 6 ; the amount of top padding in pixels.

string = This is some text ; <-- write to the text box here
Gui, Add, Edit, -0x200000 hwndEdit vEdit, % string "`n`n"
GuiControl,, Edit, % RegExReplace( string, "`n" )
Gui, Show, x10 y10, gui

;; Undocumented Constants:
; EM_GETRECT = 0xB2
; EM_SETRECT = 0xB3

VarSetCapacity( R, 16, 0 ) ; varset for Rect Struct
SendMessage, 0xB2,, % &R,, ahk_id %Edit%
x1:=NumGet( &R+0 ), y1:=NumGet( &R+4 )
x2:=NumGet( &R+8 ), y2:=NumGet( &R+12 )
NumPut( (x1-1), &R, 0 ), NumPut( (y1+tp), &R, 4 )
NumPut( (x2+1), &R, 8 ), NumPut( (y2+y1+1), &R, 12 ) ; <-- compensation for upper padding
SendMessage, 0xB3, 0x0, % &R,, ahk_id %Edit%
Return

GuiClose:
ExitApp
Could you have a look, and fix it? Thanks in advance.

Some references:
just me
Posts: 9553
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Edit - Vertically Align Text

28 Apr 2015, 00:46

Hello tmplinshi,

both functions, mine as well as the one you linked, are designed to work for singe-line controls, whereas you are initially creating a control with three lines. Do you want to center the remaining line in your Edit vertically or to adjust the Edit's height?
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Edit - Vertically Align Text

28 Apr 2015, 01:36

Single line is ok. But your example code display the text in top-center:
top-center.png
top-center.png (4.09 KiB) Viewed 4469 times
I want to display the text in the middle, like this:
middle.png
middle.png (4.2 KiB) Viewed 4469 times

Code: Select all

Gui, Font, S20
tp := 15 ; the amount of top padding in pixels.

Gui, Add, Edit, -VScroll -E0x200 hwndEdit w200 h60 Center, Data
Gui, Show, x10 y10, gui

VarSetCapacity( R, 16, 0 ) ; varset for Rect Struct
SendMessage, 0xB2,, % &R,, ahk_id %Edit%
x1:=NumGet( &R+0 ), y1:=NumGet( &R+4 )
x2:=NumGet( &R+8 ), y2:=NumGet( &R+12 )
NumPut( (x1-1), &R, 0 ), NumPut( (y1+tp), &R, 4 )
NumPut( (x2+1), &R, 8 ), NumPut( (y2+y1+1), &R, 12 ) ; <-- compensation for upper padding
SendMessage, 0xB3, 0x0, % &R,, ahk_id %Edit%
Return

GuiClose:
ExitApp
just me
Posts: 9553
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Edit - Vertically Align Text

28 Apr 2015, 10:15

Code: Select all

#NoEnv
Gui, Font, S20
Gui, Add, Edit, -VScroll -E0x200 hwndEdit w200 h60 Center, Data
Gui, Show, , gui
Edit_VCENTER(Edit)
Return
GuiClose:
ExitApp
Edit_VCENTER(HEDIT) {
   ; 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, 0x00B2, 0, &RC, , ahk_id %HEDIT%
   RCHeight := NumGet(RC, 12, "Int") - NumGet(RC, 4, "Int")
   DY := (CLHeight - RCHeight) // 2
   NumPut(NumGet(RC, 4, "Int") + DY, RC, 4, "Int")
   NumPut(NumGet(RC, 12, "Int") + DY, RC, 12, "Int")
   SendMessage, 0x00B3, 0, &RC, , ahk_id %HEDIT%
}
Test.PNG
Test.PNG (2.47 KiB) Viewed 4456 times
:?:
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Edit - Vertically Align Text

28 Apr 2015, 12:14

just me wrote:

Code: Select all

#NoEnv
Gui, Font, S20
Gui, Add, Edit, -VScroll -E0x200 hwndEdit w200 h60 Center, Data
Gui, Show, , gui
Edit_VCENTER(Edit)
Return
GuiClose:
ExitApp
Edit_VCENTER(HEDIT) {
   ; 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, 0x00B2, 0, &RC, , ahk_id %HEDIT%
   RCHeight := NumGet(RC, 12, "Int") - NumGet(RC, 4, "Int")
   DY := (CLHeight - RCHeight) // 2
   NumPut(NumGet(RC, 4, "Int") + DY, RC, 4, "Int")
   NumPut(NumGet(RC, 12, "Int") + DY, RC, 12, "Int")
   SendMessage, 0x00B3, 0, &RC, , ahk_id %HEDIT%
}
Test.PNG
:?:
On my computer, the text displayed at top-center :| ...Then I tried to change the font name Gui, Font, S20, Consolas, then it works!! My OS is Chinese language, and the default font is simsun, a Chinese font. I also tried other Chinese fonts, none of them works.
just me
Posts: 9553
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Edit - Vertically Align Text

29 Apr 2015, 10:58

Does this work better?

Code: Select all

#NoEnv
; String := "Ütag"
; String := "Ütag`nÖtag"
String := "Ütag`nÖtag`nÄtag"
Gui, Font, S20
Gui, Add, Edit, -VScroll -E0x200 hwndHEdit1 w200 r3 Center, %String%
Gui, Add, Edit, -VScroll -E0x200 hwndHEdit2 w200 r3 Center, %String%
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%
}
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Edit - Vertically Align Text

29 Apr 2015, 11:35

Yes, it works now! :D Thank you very much, I guess you've spent many hours on it. Thanks a lot!!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Chunjee, exjnbtech49, Google [Bot], j4byers and 208 guests