Jump to content

[Resolved] How to derive active end of selection from _GUICtrlEdit_GetSel?


Recommended Posts

Resolution: Working from _GUICtrlRichEdit_GetSelAA() made a function to work for regular Edit controls. Wish I'd known that function was present before I posted.

Original post below:


Okay, so right out of the help file for _GUICtrlEdit_SetSel we have this: "The start value is the anchor point of the selection, and the end value is the active end. If the user uses the SHIFT key to adjust the size of the selection, the active end can move but the anchor point remains the same." But there seems to be no equivalent information for _GUICtrlEdit_GetSel.

I can manually set which end of the selection will be the active end with _GUICtrlEdit_SetSel, but is there any way I can find out which end of the selection is the active end if, for example, the user made a selection with the mouse? _GUICtrlEdit_GetSel only returns the start and end points in a left-to-right order.

#include <GUIConstants.au3>
#include <GUIEdit.au3>

$gui = GUICreate('', 200, 120)
$edit = GUICtrlCreateEdit('This is a string', 0, 0, 200, 80)
$button1 = GUICtrlCreateButton('Test 1', 0, 80, 100, 20)
$button2 = GUICtrlCreateButton('Test 2', 100, 80, 100, 20)
$label = GUICtrlCreateLabel('', 0, 100, 200, 20)
GUISetState()

While 1
    $gm = GUIGetMsg()
    Switch $gm
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $button1
            GUICtrlSetState($edit, $GUI_FOCUS)
            _GUICtrlEdit_SetSel($edit, 5, 9)
            $aSel = _GUICtrlEdit_GetSel($edit)
            GUICtrlSetData($label, 'Selection is: ' & $aSel[0] & ' to ' & $aSel[1])
        Case $button2
            GUICtrlSetState($edit, $GUI_FOCUS)
            _GUICtrlEdit_SetSel($edit, 9, 5)
            $aSel = _GUICtrlEdit_GetSel($edit)
            GUICtrlSetData($label, 'Selection is: ' & $aSel[0] & ' to ' & $aSel[1])
    EndSwitch
WEnd
Edited by therks
Link to comment
Share on other sites

is there any way I can find out which end of the selection is the active end if, for example, the user made a selection

#include <GUIConstants.au3>
#include <GUIEdit.au3>

$gui = GUICreate('', 200, 120)
$edit = GUICtrlCreateEdit('This is a string. This is a string', 0, 0, 200, 80)
;~ $button1 = GUICtrlCreateButton('Test 1', 0, 80, 100, 20)
;~ $button2 = GUICtrlCreateButton('Test 2', 100, 80, 100, 20)
$label = GUICtrlCreateLabel('', 0, 100, 200, 20)
GUISetState()

Opt("CaretCoordMode", 0)
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
;~      Case $button1
;~          GUICtrlSetState($edit, $GUI_FOCUS)
;~          _GUICtrlEdit_SetSel($edit, 5, 9)
;~          $aSel = _GUICtrlEdit_GetSel($edit)
;~          GUICtrlSetData($label, 'Selection is: ' & $aSel[0] & ' to ' & $aSel[1] & ' Active: ' & Active())
;~      Case $button2
;~          GUICtrlSetState($edit, $GUI_FOCUS)
;~          _GUICtrlEdit_SetSel($edit, 9, 5)
;~          $aSel = _GUICtrlEdit_GetSel($edit)
;~          GUICtrlSetData($label, 'Selection is: ' & $aSel[0] & ' to ' & $aSel[1] & ' Active: ' & Active())
    EndSwitch
    $aSel = _GUICtrlEdit_GetSel($edit)
    If $aSel[0] <> $aSel[1] Then
        GUICtrlSetData($label, 'Selection is: ' & $aSel[0] & ' to ' & $aSel[1] & ' Active: ' & Active())
    Else
        GUICtrlSetData($label, '')
    EndIf
    Sleep(50)
WEnd

Func Active()
    ;$iOpt = Opt("CaretCoordMode", 0)
    $aCaret = WinGetCaretPos()
    ;$iErrSav = @error
    ;Opt("CaretCoordMode", $iOpt)
    If @error Then Return SetError(1, 0, -1)
    $aPos = _GUICtrlEdit_CharFromPos($edit, $aCaret[0], $aCaret[1])
    Return $aPos[0]
EndFunc ;==>Active
Link to comment
Share on other sites

Unfortunately that returns very inconsistent results, especially if the control is not on the first line. I had tried something with WinGetCaretPos before, but had no luck.

#include <GUIConstants.au3>
#include <GUIEdit.au3>
#include <ScrollBarConstants.au3>

$gui = GUICreate('', 200, 120)
$edit = GUICtrlCreateEdit(@CRLF & @CRLF & @CRLF & @CRLF & 'This is a string. This is a string', 0, 0, 200, 80)
$button1 = GUICtrlCreateButton('Test', 0, 80, 100, 20)
$button2 = GUICtrlCreateButton('Test 2', 100, 80, 100, 20)
$label = GUICtrlCreateLabel('', 0, 100, 200, 20)
GUISetState()

Opt("CaretCoordMode", 0)
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
      Case $button1
          GUICtrlSetState($edit, $GUI_FOCUS)
          _GUICtrlEdit_SetSel($edit, 18, 24)
          _GUICtrlEdit_Scroll($edit, $SB_SCROLLCARET)
      Case $button2
          GUICtrlSetState($edit, $GUI_FOCUS)
          _GUICtrlEdit_SetSel($edit, 24, 18)
          _GUICtrlEdit_Scroll($edit, $SB_SCROLLCARET)
    EndSwitch
    $aSel = _GUICtrlEdit_GetSel($edit)
    If $aSel[0] <> $aSel[1] Then
        GUICtrlSetData($label, 'Selection is: ' & $aSel[0] & ' to ' & $aSel[1] & ' Active: ' & Active())
    Else
        GUICtrlSetData($label, '')
    EndIf
    Sleep(50)
WEnd

Func Active()
    ;$iOpt = Opt("CaretCoordMode", 0)
    $aCaret = WinGetCaretPos()
    ;$iErrSav = @error
    ;Opt("CaretCoordMode", $iOpt)
    If @error Then Return SetError(1, 0, -1)
    $aPos = _GUICtrlEdit_CharFromPos($edit, $aCaret[0], $aCaret[1])
    Return $aPos[0]
EndFunc ;==>Active

Thanks very much for trying something though!

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...