Detecting scroll position of listview Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
colt
Posts: 291
Joined: 04 Aug 2014, 23:12
Location: Portland Oregon

Detecting scroll position of listview

Post by colt » 19 Jan 2021, 18:15

How can I detect the scrolled position of listview or the topmost visible element?
In my main program I have made a spreadsheet tool. It works great until I need to scroll the listview. When this happens the relative position of the cell being edited is no longer with respect to the upper left of the control.

Below is a quick example. I would like the mouse to hover over the 15th element even when the scroll position changes.

Code: Select all

gui,add,listview,vgLV w500 h500 x50 y50,ROWNUM|CONTENT
loop 50
{
	lv_Add("",A_Index,"TEST " . A_Index)
}
gui show ,w600h600
f8:: ;hover over 15th element
	ControlGetPos ,cx, cy,,, SysListView321, A	
	rowheight := 17
	offset := 0
	loop 15
	{
		offset += rowHeight
	}
	mouseMove,cx+40,cy+rowheight+offset
return

Rohwedder
Posts: 7655
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Detecting scroll position of listview  Topic is solved

Post by Rohwedder » 20 Jan 2021, 02:20

Hallo,
perhaps:

Code: Select all

gui,add,listview,vgLV w500 h500 x50 y50,ROWNUM|CONTENT
loop 50
{
	lv_Add("",A_Index,"TEST " . A_Index)
}
gui show ,w600h600
f8:: ;hover over 15th element
	ControlGetPos ,cx, cy,,, SysListView321, A
	ControlGetFocus, Control, A
	ControlGet, Hwnd, Hwnd,, %Control%, A
	ScrollPos := DllCall("GetScrollPos", "Ptr", Hwnd, "Int", 1)
	rowheight := 20
	element := 15
	IF (ElementPos:=1+element-ScrollPos) > 1
		mouseMove,cx+40,cy+rowheight*ElementPos
	Else
		SoundBeep ;invisible
return

garry
Posts: 3770
Joined: 22 Dec 2013, 12:50

Re: Detecting scroll position of listview

Post by garry » 20 Jan 2021, 05:12

@Rohwedder thank you works fine , tried with 4K monitor , dependig font must define rowheight
added example , with F7 show row 7

Code: Select all

;- Detecting scroll position of listview 
;- https://www.autohotkey.com/boards/viewtopic.php?f=76&t=85857
Gui,1:default
Gui,-dpiscale
Gui,1:Font,s12,Lucida Console
gui,add,listview,vgLV gLW2 w500 h800 x50 y10 +altsubmit,ROWNUM|CONTENT
loop, 50
	lv_Add("",A_Index,"TEST " . A_Index)
gui show ,w600 h900
return
;---------------
guiclose:
exitapp
;---------------
LW2:
gui,1:listview,glv
  RN:=LV_GetNext("C")
  RF:=LV_GetNext("F")
  GC:=LV_GetCount()
if A_GuiEvent = Normal
  {
  LV_GetText(C1,A_EventInfo,1)
  LV_GetText(C2,A_EventInfo,2)
  msgbox,%c1%  %c2%
  }
return
;---------------
F7::
LV_Modify(RF, "-Select -Focus")
LV_Modify(7, "+Select +Focus")
RF:=LV_GetNext("F")     ;- selected focused
LV_GetText(C1,RF,1)
LV_GetText(C2,RF,2)
LV_Modify(RF, "Vis")     ;- scroll to row 7
 ; msgbox,%c1%  %c2%
return
;---------------

f8:: ;-hover over 15th element
    LV_Modify(7, "-Select -Focus")
	ControlGetPos ,cx, cy,,, SysListView321, A
	ControlGetFocus, Control, A
	ControlGet, Hwnd, Hwnd,, %Control%, A
	ScrollPos := DllCall("GetScrollPos", "Ptr", Hwnd, "Int", 1)
	rowheight := 31
	element := 15
	IF (ElementPos:=1+element-ScrollPos) > 0
		mouseMove,cx+40,cy+rowheight*ElementPos
	Else
		SoundBeep,425, 800    ;- invisible
return
;===========================================================
Last edited by garry on 20 Jan 2021, 05:15, edited 1 time in total.

just me
Posts: 9464
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Detecting scroll position of listview

Post by just me » 20 Jan 2021, 05:15

... or?

Code: Select all

#NoEnv
Gui, Add, ListView, hwndHLV vgLV w500 h500 x50 y50, ROWNUM|CONTENT
Loop, 50
   LV_Add("", A_Index, "TEST " . A_Index)
Gui, Show, w600 h600, ListView
Return

GuiClose:
ExitApp

F8:: ; hover over 15th element
   ControlGetPos, LVX, LVY, , , , ahk_id %HLV%
   Row := LV_EX_GetTopIndex(HLV) + 14
   RC := LV_EX_GetItemRect(HLV, Row, 2) ; LVIR_LABEL = 2
   MX := LVX + RC.X + (RC.W // 2)
   MY := LVY + RC.Y + (RC.H // 2)
   MouseMove, MX, MY
Return

; ======================================================================================================================
; LV_EX_GetItemRect - Retrieves the bounding rectangle for all or part of an item in the current view.
; ======================================================================================================================
LV_EX_GetItemRect(HLV, Row := 1, LVIR := 0, Byref RECT := "") {
   ; LVM_GETITEMRECT = 0x100E -> http://msdn.microsoft.com/en-us/library/bb761049(v=vs.85).aspx
   VarSetCapacity(RECT, 16, 0)
   NumPut(LVIR, RECT, 0, "Int")
   SendMessage, 0x100E, % (Row - 1), % &RECT, , % "ahk_id " . HLV
   If (ErrorLevel = 0)
      Return False
   Result := {}
   Result.X := NumGet(RECT,  0, "Int")
   Result.Y := NumGet(RECT,  4, "Int")
   Result.R := NumGet(RECT,  8, "Int")
   Result.B := NumGet(RECT, 12, "Int")
   Result.W := Result.R - Result.X
   Result.H := Result.B - Result.Y
   Return Result
}
; ======================================================================================================================
; LV_EX_GetTopIndex - Retrieves the index of the topmost visible item when in list or report view.
; ======================================================================================================================
LV_EX_GetTopIndex(HLV) {
   ; LVM_GETTOPINDEX = 0x1027 -> http://msdn.microsoft.com/en-us/library/bb761087(v=vs.85).aspx
   SendMessage, 0x1027, 0, 0, , % "ahk_id " . HLV
   Return (ErrorLevel + 1)
}

LV_EX - Additional functions for GUI ListView controls

garry
Posts: 3770
Joined: 22 Dec 2013, 12:50

Re: Detecting scroll position of listview

Post by garry » 20 Jan 2021, 05:23

@just me works fine , goto row 15 ( not depending fontsize ) , when row 21 is at top , goto row 35 ( 15-th row )
a question : when I hower a row how show the row content ( tooltip ) ?

just me
Posts: 9464
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Detecting scroll position of listview

Post by just me » 20 Jan 2021, 06:11

@garry, do you want to show the row's contents in a tooltip after the mouse was moved to that row?

garry
Posts: 3770
Joined: 22 Dec 2013, 12:50

Re: Detecting scroll position of listview

Post by garry » 20 Jan 2021, 07:55

@just me thank you, show content from desired column (tooltip) , sometimes I have columns with small width , so show as example only column-3 when I go with mouse to row-x / column-3

just me
Posts: 9464
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Detecting scroll position of listview

Post by just me » 20 Jan 2021, 11:25

@garry,

LVS_EX_LABELTIP (LV0x4000) might do what you want.

garry
Posts: 3770
Joined: 22 Dec 2013, 12:50

Re: Detecting scroll position of listview

Post by garry » 20 Jan 2021, 12:06

@just me thank you I'll try ...
I use your function , click on column and can run only this column ...
https://autohotkey.com/board/topic/80265-solved-which-column-is-clicked-in-listview/
LV_SubitemHitTest(HLV)

colt
Posts: 291
Joined: 04 Aug 2014, 23:12
Location: Portland Oregon

Re: Detecting scroll position of listview

Post by colt » 20 Jan 2021, 13:24

Thank you all! Some good points in all these examples

Post Reply

Return to “Ask for Help (v1)”