Page 1 of 1

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

Posted: 22 Oct 2017, 21:04
by jeeswg
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

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

Posted: 22 Oct 2017, 21:35
by teadrinker
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

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

Posted: 22 Oct 2017, 21:44
by jeeswg
Interesting, thanks for sharing. Try with word wrap off. For the record, I'm using Windows 7.

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

Posted: 22 Oct 2017, 21:46
by teadrinker
I tested on both 7 and 10 and got the same behavior.

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

Posted: 22 Oct 2017, 21:53
by jeeswg
- 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.

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

Posted: 22 Oct 2017, 22:09
by teadrinker
- 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

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

Posted: 22 Oct 2017, 22:32
by jeeswg
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

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

Posted: 22 Oct 2017, 23:04
by teadrinker
If I switch word wrap off, Notepad looks like this:

Image

Image

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

Image

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

Posted: 22 Oct 2017, 23:22
by jeeswg
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?