Jump to content

howto get cursor position in multiline edit


tito
 Share

Recommended Posts

Hi,

I'm trying to retrieve to cursor position in a multiline edit ctrl.

When edit ctrl is saved, the cursor position should also be saved.

If user reopens the edit ctrl, (using a fileread/replace content) , I try to put the cursor back where it was.

Now I autoscroll to the last line using:

$iLen = _GUICtrlEdit_GetLineCount($GUINotepad)

_GUICtrlEdit_LineScroll($GUINotepad, 1, $iLen)

but this isn't what I'm trying to do...

I need something like '_GUICtrlEdit_GetCursorPos()'

Any ideas?

Edited by tito
Link to comment
Share on other sites

  • Moderators

tito,

Any ideas?

Yes! :P

#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#Include <GuiEdit.au3>

Global $aLocation[2] = [0, 0]

$sText = "This is a long pice of text which should require wrapping in the edit control - " & _
"which is provided by the combination of $ES_WANTRETURN and $ES_MULTILINE styles"

$hGUI = GUICreate("Test", 500, 500)

$hEdit = GUICtrlCreateEdit($sText, 10, 10, 200, 200, BitOr($ES_WANTRETURN, $ES_MULTILINE))

$hButton_Locate = GUICtrlCreateButton("Locate", 10, 300, 80, 30)
$hButton_Reset = GUICtrlCreateButton("Reset", 10, 350, 80, 30)
$hButton_Relocate = GUICtrlCreateButton("Relocate", 10, 400, 80, 30)

$hLabel_Line = GUICtrlCreateLabel("Line: ", 100, 300, 100, 20)
$hLabel_Col = GUICtrlCreateLabel("Col: ", 200, 300, 100, 20)

GUISetState()

; Set cursor to start
_GUICtrlEdit_SetSel($hEdit, 0, 0)

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hButton_Locate
            ; Show and save current position
            GUICtrlSetData($hLabel_Line, "Line: " & ControlCommand($hGUI, "", $hEdit, "GetCurrentLine", ""))
            GUICtrlSetData($hLabel_Col, "Col: " & ControlCommand($hGUI, "", $hEdit, "GetCurrentCol", ""))
            ; Get current position in usable form
            $aLocation = _GUICtrlEdit_GetSel($hEdit)
            GUICtrlSetState($hEdit, $GUI_FOCUS)
        Case $hButton_Reset
            ; Reset cursor to start
            GUICtrlSetState($hEdit, $GUI_FOCUS)
            _GUICtrlEdit_SetSel($hEdit, 0, 0)
        Case $hButton_Relocate
            ; Relocate cursor to stored position
            GUICtrlSetState($hEdit, $GUI_FOCUS)
            _GUICtrlEdit_SetSel($hEdit, $aLocation[0], $aLocation[1])
    EndSwitch

WEnd

All clear? :x

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Addition.

#Include <GUIEdit.au3>
#Include <ScrollBarConstants.au3>
#Include <WindowsConstants.au3>

$sFile = RegRead('HKLM\SOFTWARE\AutoIt v3\AutoIt', 'InstallDir') & '\Include\Array.au3'

GUICreate('MyGUI', 400, 300)
$Edit = GUICtrlCreateEdit('', 10, 10, 380, 280, BitOR($ES_WANTRETURN, $WS_VSCROLL, $WS_HSCROLL))
GUISetState()

_GUICtrlEdit_SetText($Edit, FileRead($sFile))
_GUICtrlEdit_SetPos($Edit, 5, 18)

Do
Until GUIGetMsg() = -3

; #FUNCTION# ====================================================================================================================
; Name...........: _GUICtrlEdit_SetPos
; Description....: Sets the caret to the specified line and column.
; Syntax.........: _GUICtrlEdit_SetPos ( $hWnd, $iLine [, $iColumn] )
; Parameters.....: $hWnd    - Handle or identifier (controlID) to the control.
;                  $iLine   - The zero-based index of the line on which must set the caret. If this parameter is (-1),
;                             the caret will be set on the last line.
;                  $iColumn - The zero-based index of the column on which must set the caret. If this parameter is (-1),
;                             the caret will be set at the end of the specified line. Default is 0.
; Return values..: Success  - 1.
;                  Failure  - 0 and sets the @error flag to non-zero.
; Author.........: Yashied
; Modified.......:
; Remarks........: None
; Related........: _GUICtrlEdit_Scroll(), _GUICtrlEdit_SetSel()
; Link...........: None
; Example........: Yes
; ===============================================================================================================================

Func _GUICtrlEdit_SetPos($hWnd, $iLine, $iColumn = 0)

    If Not IsHWnd($hWnd) Then
        $hWnd = GUICtrlGetHandle($hWnd)
        If $hWnd = 0 Then
            Return SetError(1, 0, 0)
        EndIf
    EndIf

    Local $Lenght, $Num = 0, $Count = _GUICtrlEdit_GetLineCount($hWnd)

    If $iLine > $Count - 1 Then
        $Num = _GUICtrlEdit_GetTextLen($hWnd)
    Else
        If $iLine < 0 Then
            $iLine = $Count - 1
        EndIf
        For $i = 0 To $iLine - 1
            $Num += _GUICtrlEdit_LineLength($hWnd, $i) + 2 ; + @CR + @LF
        Next
        $Lenght = _GUICtrlEdit_LineLength($hWnd, $iLine)
        If ($iColumn < 0) Or ($iColumn > $Lenght) Then
            $iColumn = $Lenght
        EndIf
        $Num += $iColumn
    EndIf
    _GUICtrlEdit_SetSel($hWnd, $Num, $Num)
    _GUICtrlEdit_Scroll($hWnd, $SB_SCROLLCARET)
    Return 1
EndFunc   ;==>_GUICtrlEdit_SetPos
Link to comment
Share on other sites

  • Moderators

tito,

Would Sir like fries with that! :P

Look at _GUICtrlEdit_Scroll using the $SB_SCROLLCARET parameter in the Help file.:x

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Would Sir like fries with that! :P

No, thx though :x

Dim $notepad_scroll_position[2]
$notepad_scroll_position[0] = 0
$notepad_scroll_position[1] = 0
(used if no scroll position previously saved)


$notepad_scroll_position = _GUICtrlEdit_GetSel($GUINotepad)
=>
_GUICtrlEdit_SetSel($GUINotepad, $notepad_scroll_position[0], $notepad_scroll_position[1])
_GUICtrlEdit_Scroll($GUINotepad, $SB_SCROLLCARET)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...