Archive: SetCaretPos system call


SetCaretPos system call
I'm trying to set the caret position within the text field of a custom page. My call is within the SetFocus function.


System::Call "user32::CreateCaret(i r0, i, i R2, i R5)i"
System::Call "user32::SetCaretPos(i 10, i) i .r1"
System::Call "user32::ShowCaret(i r0)i"

This is the same order of calls that MSDN uses in their example. My return value ($1) is 1, which means that the call succeeded, according to the docs.

But, no matter what values I pass in, the caret remains at the beginning of the text field when it is given focus. Has anybody used this successfully? If so, how do you know what to pass in for the x/y coordinates? For example, how would you set the position to the end of the string in a text field?

Set caret position to the end of the text:

Test.nsi

Name "Test"
OutFile "Test.exe"

!include "WinMessages.nsh"

Var HWND
Var INI

Page Custom ShowCustom LeaveCustom
Page instfiles

Function ShowCustom
InstallOptions::initDialog /NOUNLOAD "$INI"
Pop $HWND

GetDlgItem $0 $HWND 1200
SendMessage $0 ${WM_SETTEXT} 1 "STR:12345$\r$\n67890$\r$\nabcdf$\r$\n"
System::Call "user32::SetFocus(i r0)"
System::Call "user32::SendMessage(i r0, i ${EM_GETLINECOUNT}, i 0, i 0)i .r1"
IntOp $1 $1 - 1
System::Call "user32::SendMessage(i r0, i ${EM_LINEINDEX}, i r1, i 0)i .r2"
System::Call "user32::SendMessage(i r0, i ${EM_LINELENGTH}, i r2, i 0)i .r3"
IntOp $4 $3 + $2
SendMessage $0 ${EM_SETSEL} $4 $4
SendMessage $0 ${EM_LINESCROLL} 0 $1

InstallOptions::show
Pop $0
FunctionEnd

Function LeaveCustom
ReadINIStr $0 $INI "Settings" "State"
StrCmp $0 0 Enter
goto main

main:
Abort

Enter:
FunctionEnd

Function .onInit
InitPluginsDir
GetTempFileName $INI $PLUGINSDIR
File /oname=$INI "Test.ini"
FunctionEnd

Section "Empty"
SectionEnd

Test.ini
[Settings]
NumFields=1
NextButtonText=&Enter

[Field 1]
Type=Text
Flags=MULTILINE|VSCROLL|HSCROLL
Left=5
Right=-48
Top=5
Bottom=60

Thanks Instructor! It works perfectly :)