Edit control: right-aligned control but left-aligned text

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Edit control: right-aligned control but left-aligned text

22 Oct 2017, 21:04

In this example, the text of Notepad's Edit control is set, and the control is set to right-aligned text. Short lines appear right-aligned as expected, but any lines longer than the visible part of the control appear as though they are left-aligned. Does anyone know of any workaround for this? Cheers.

Code: Select all

q:: ;Notepad - Edit control align right
vText := "
(Join`r`n
abcdefghijklm nopqrstuvwxyz
abcdefghijklm nopqrstuvwxyz abcdefghijklm nopqrstuvwxyz
abcdefghijklm nopqrstuvwxyz abcdefghijklm nopqrstuvwxyz abcdefghijklm nopqrstuvwxyz
abcdefghijklm nopqrstuvwxyz abcdefghijklm nopqrstuvwxyz abcdefghijklm nopqrstuvwxyz abcdefghijklm nopqrstuvwxyz
abcdefghijklm nopqrstuvwxyz abcdefghijklm nopqrstuvwxyz abcdefghijklm nopqrstuvwxyz abcdefghijklm nopqrstuvwxyz abcdefghijklm nopqrstuvwxyz
)"
ControlGetText, vText2, Edit1, ahk_class Notepad
if (vText2 = "")
{
	;ES_RIGHT := 0x2
	Control, Style, +0x2, Edit1, ahk_class Notepad
	ControlSetText, Edit1, % vText, ahk_class Notepad
}
return
I was checking through various lines of code, and I wanted to inspect the ends of the lines only. A workaround is to pad each line with spaces.

Code: Select all

w:: ;pad text with leading spaces (assumes an equal-width font e.g. Courier New)
vText := "
(Join`r`n
abcdefghijklm nopqrstuvwxyz
abcdefghijklm nopqrstuvwxyz abcdefghijklm nopqrstuvwxyz
abcdefghijklm nopqrstuvwxyz abcdefghijklm nopqrstuvwxyz abcdefghijklm nopqrstuvwxyz
abcdefghijklm nopqrstuvwxyz abcdefghijklm nopqrstuvwxyz abcdefghijklm nopqrstuvwxyz abcdefghijklm nopqrstuvwxyz
abcdefghijklm nopqrstuvwxyz abcdefghijklm nopqrstuvwxyz abcdefghijklm nopqrstuvwxyz abcdefghijklm nopqrstuvwxyz abcdefghijklm nopqrstuvwxyz
)"
vLen := 0
Loop, Parse, vText, `n, `r
	if (StrLen(A_LoopField) > vLen)
		vLen := StrLen(A_LoopField)
vWhitespace := ""
Loop, % vLen
	vWhitespace .= " "
vOutput := ""
Loop, Parse, vText, `n, `r
	vOutput .= (A_Index=1?"":"`r`n") SubStr(vWhitespace, 1, vLen-StrLen(A_LoopField)) A_LoopField
ControlGetText, vText2, Edit1, ahk_class Notepad
if (vText2 = "")
	ControlSetText, Edit1, % vOutput, ahk_class Notepad
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: Edit control: right-aligned control but left-aligned text

22 Oct 2017, 21:35

but any lines longer than the visible part of the control appear as though they are left-aligned.
For me it's not true:

Image
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Edit control: right-aligned control but left-aligned text

22 Oct 2017, 21:44

Interesting, thanks for sharing. Try with word wrap off. For the record, I'm using Windows 7.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: Edit control: right-aligned control but left-aligned text

22 Oct 2017, 21:46

I tested on both 7 and 10 and got the same behavior.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Edit control: right-aligned control but left-aligned text

22 Oct 2017, 21:53

- If you set word wrap on/off, then I believe you need to set the style to ES_RIGHT again.
- I don't know whether fonts/certain characters can be an issue.
- Also, try moving the caret up/down, and to the start/end of lines.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: Edit control: right-aligned control but left-aligned text

22 Oct 2017, 22:09

- If you set word wrap on/off, then I believe you need to set the style to ES_RIGHT again.
Yes, for me it's true also.
- I don't know whether fonts/certain characters can be an issue.
I use "calibri", but tried changing, got the same result.
- Also, try moving the caret up/down, and to the start/end of lines.
The caret moves as I expect:

Image
By pressing {Right}:

Image
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Edit control: right-aligned control but left-aligned text

22 Oct 2017, 22:32

I have aero mode off, and Edit control style/ex. style: 0x50300106 0x00000200.

I do not have the problem when word wrap is on. I only have the problem when word wrap is off.

Code: Select all

q:: ;control get style/ExStyle
vCtlClassNN := "Edit1"
ControlGet, vCtlStyle, Style,, % vCtlClassNN, A
ControlGet, vCtlExStyle, ExStyle,, % vCtlClassNN, A
vOutput := Format("0x{:08X} 0x{:08X}", vCtlStyle, vCtlExStyle)
Clipboard := vOutput
MsgBox, % vOutput
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: Edit control: right-aligned control but left-aligned text

22 Oct 2017, 23:04

If I switch word wrap off, Notepad looks like this:

Image

Image

I don't know whether it is proper or not. :)
Styles:

Image
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Edit control: right-aligned control but left-aligned text

22 Oct 2017, 23:22

Yeah, that looks like what I've got, right-aligned if the line can fit in the visible control width, otherwise left-aligned.

Unless I'm missing something, how can someone who uses a right-aligned language, and occasionally/frequently uses long lines, use Edit controls?
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 292 guests