click a control inside of a listbox

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jekko1976
Posts: 97
Joined: 10 Oct 2014, 07:03

click a control inside of a listbox

19 Jun 2018, 01:31

Hello,

i saw with window spy that the control that i have to click is inside of a listbox,
so i found this procedure to cycle the listbox

Code: Select all

ControlGet, List, List, Selected, ListBox1, Definizione dispositivo di emissione
Loop, Parse, List, `n  ; Rows are delimited by linefeeds (`n).
	{
	RowNumber := A_Index
	Loop, Parse, A_LoopField, %A_Tab%  ; Fields (columns) in each row are delimited by tabs (A_Tab).
	MsgBox Row #%RowNumber% Col #%A_Index% is %A_LoopField%.
	}
This code actually works and returns me that my element is at row7, col1

Now my question is, how could i send a click to this element?
i figured out that i have to use "sendmessage" but i didn't find any sendemessage "left click" code

Thank you very much
User avatar
divanebaba
Posts: 805
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: click a control inside of a listbox

19 Jun 2018, 01:49

Hi.

To select and trigger an entry inside a listbox, you have to choose GuiControl like below:

Code: Select all

GuiControl, Choose, ListBox1, |3
Code above selects the third item and triggers the g-label.
If you want to choose a "foreign" Listbox, you have to choose Control.
For control command, there are two options you can choose.
Option 1: Control, choose, N, ...
Option 2: Control, choosestring, String, ...

No warranty :crazy:
Einfach nur ein toller Typ. :mrgreen:
jekko1976
Posts: 97
Joined: 10 Oct 2014, 07:03

Re: click a control inside of a listbox

19 Jun 2018, 07:09

unfortunately Control doesn't seem to work. I think that somehow the solution is SendMessage, but i can't figure out how to send a clic with Sendmessage.
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: click a control inside of a listbox

19 Jun 2018, 08:54

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.
my scripts
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: click a control inside of a listbox

19 Jun 2018, 11:33

Can you not set the selected item and then use ControlSend/SendInput with {Enter}?
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, Lamron750, mikeyww and 226 guests