Hi
jekko1976,
You can retrieve the coordinates of the selected item's center and then click it - for example:
Code: Select all
GUI, +hwndGUIID ; +hwndGUIID stores the window handle (HWND) of the GUI in 'GUIID'
GUI, Add, ListBox, x200 w200 h100 choose4 hwndhListBox, a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p
SendMessage, 0x1A1, 0, 0,, % "ahk_id " . hListBox ; 0x1A1 is LB_GETITEMHEIGHT - sets ErrorLevel to the height of items in the list box
lbItemHeight := ErrorLevel
ControlGetPos, listboxX, listboxY, listboxW,,, % "ahk_id " . hListBox
GUI, Show, AutoSize, test
return
!i::
; ---------------------------- retrieve the position of the selected item ----------------------------
SendMessage, 0x188, 0, 0,, % "ahk_id " . hListBox ; from the doc: https://www.autohotkey.com/docs/commands/ControlGet.htm
choicePos := ErrorLevel << 32 >> 32
; ---------------------------- retrieve the index of the first visible item ----------------------------
SendMessage, 0x018E, 0, 0,, % "ahk_id " . hListBox ; 0x018E is LB_GETTOPINDEX - sets ErrorLevel to the index of the first visible item in the list box
; ---------------------------- calculate offset from the top ----------------------------
offsetTop := (choicePos - ErrorLevel)
; ---------------------------- calculate X ----------------------------
xToMove := listboxX + listboxW // 2
; ---------------------------- calculate Y ----------------------------
yToMove := listboxY + (offsetTop + 0.5) * lbItemHeight
MouseMove % xToMove, % yToMove ; showcase result by moving the mouse to the coordinates retrieved by the subroutine
return
Surely there must be a more direct route to achieve this.
By the way, note that the
selected option is not supported by ListBoxes when using the
List sub-command.
Hope this helps.