Jump to content

Recommended Posts

Posted

Hi!

A simple question is:

How to get "word" from edit's caret position??

Example:

- I have some text in notepad ("word1 word2 word3 word4...")

Now I set caret position somewhere inside "word2", and I want return from control only this "word2"

It's something like use ControlCommand function with "GetSelected", "" but I want return text without marking word

This function was used eg. AutoIt3 FuncPopUp

  • Moderators
Posted

piterek,

Here is a way to do it: :idea:

#Include <GuiEdit.au3>

; Start Notepad and enter some text, then start script

; Wait for Notepad to be active
WinWaitActive("[CLASS:Notepad]")
; Get handle for Edit control
$hNotePadEdit = ControlGetHandle("[CLASS:Notepad]", "", "[CLASS:Edit; INSTANCE:1]")

; Where is caret?
$aCaretPos = _GUICtrlEdit_GetSel($hNotePadEdit)
; Where is beginning of line?
$iLineStartPos = _GUICtrlEdit_LineIndex($hNotePadEdit, -1)

; Get text of the line
$sLine = _GUICtrlEdit_GetLine($hNotePadEdit, _GUICtrlEdit_LineFromChar($hNotePadEdit, -1))

; Find ends of current word
$iStartPos = StringInStr($sLine, " ", 0, -1, $aCaretPos[0] - $iLineStartPos)
$iEndPos = StringInStr($sLine, " ", 0, 1, $aCaretPos[0] - $iLineStartPos)
If $iEndPos = 0 Then $iEndPos = StringLen($sLine) + 1
; Extract word
$sWord = StringMid($sLine, $iStartPos + 1, $iEndPos - $iStartPos - 1)

; Display word
MsgBox(0, "Found", $sWord)

Ask if anything is unclear. :)

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

 

Posted

Another way without GuiEdit.au3

Global $hNotePad = WinGetHandle("[Class:Notepad]")

HotKeySet("{Esc}", "_Exit")
HotKeySet("^g", "_GetWordFromCaret")


While 1
 Sleep(10000)
WEnd

Func _GetWordFromCaret()
 Local $sRet
 Local $iLine = ControlCommand($hNotePad, "", "Edit1", "GetCurrentLine", "")
 Local $iCol = ControlCommand($hNotePad, "", "Edit1", "GetCurrentCol", "")
 Local $sLine = ControlCommand($hNotePad, "", "Edit1", "GetLine", $iLine)
 If StringMid($sLine, $iCol, 1) = " " Then $iCol -= 1
 Local $iFirst = StringInStr($sLine, " ", 0, -1, $iCol)
 $sLine = StringTrimLeft($sLine, $iFirst)
 Local $iLast = StringInStr($sLine, " ", 0, 1)
 If $iLast <> 0 Then
  $sLine = StringLeft($sLine, $iLast - 1)
 EndIf
 ConsoleWrite($sLine & @CR)
EndFunc

Func _Exit()
 Exit
EndFunc

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

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
×
×
  • Create New...