Get Listview Column

Post your working scripts, libraries and tools
SSpeed
Posts: 25
Joined: 04 Jul 2018, 09:48

Get Listview Column

17 Sep 2019, 21:19

edit: I don't use this function anymore; (kept for reference). Here's a better alternative?
_________________________

A way to get which column was doubled-clicked in a listview.
Seems to work. Hope it works for you. Just run the code and double click in the listview. F5 to reload.

I couldn't figure out how to determine the size of the border, so it may be off by a little..

example:

Code: Select all

#NoEnv
#SingleInstance Force
Create_Gui()
return
#Include LV_GetWhichColumn.ahk

;-----------------------------------------

Create_Gui() {
	global
	Random, coinflip, 0, 1
	if (coinflip)
		Gui +caption +resize
	else
		Gui -caption -resize +border
	Random randX, 0, 150
	Random randW, 300, 600
	Gui Add, ListView, x%randX% w%randW% Grid vMyListView gMyListView hwndMyListviewHwnd, Col 1|Col 2|Col 3|Col 4
	loop 4
	{
		Random, RandW, 50, 250
		LV_ModifyCol(A_Index, RandW)
	}
	; LV_ModifyCol(4, "AutoHdr")
	Gui Show, , test
}

;-----------------------------------------

F5:: Reload

;-----------------------------------------

MyListView:
	if (A_GuiEvent = "DoubleClick") {
		column := getwhichcolumn(MyListviewHwnd)
		tooltip % column
	}
	return

;-----------------------------------------

GuiEscape:
GuiClose:
	ExitApp
LV_GetWhichColumn:

Code: Select all

/*
example usage:
	if (A_GuiEvent = "DoubleClick")
		column := LV_GetWhichColumn(MyListviewHwnd)

	GuiContextMenu:
		column := LV_GetWhichColumn(MyListviewHwnd)
*/

;ꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤ

	LV_GetWhichColumn(hwnd) {
		CoordMode Mouse, Window
		MouseGetPos x, y  ; Get mouse position relative to the window
		GuiControlGet MyListView, Pos  ; Get the x coordinate of the listview relative to the window
		scrollpos := DllCall("GetScrollPos", "UInt", hwnd, "Int", 0)  ; Get the position of the horizontal scrollbar
		highcoord := (A_ScreenDPI/96)*(MyListViewX+5) - scrollpos  ; Get the relative position on the x axis
		; Get column widths
		columns := LV_GetCount("Column")
		Loop % columns
		{
			SendMessage 4125, A_Index - 1, 0, SysListView321  ; Gets the width of each cell
			columnWidth%A_Index% := ErrorLevel
		}
		if (ErrorLevel = "FAIL") {
			tooltip Failed to get column.
			sleep 800
			tooltip
			return 0
		}
		; Get the relative position of each cell, see if x falls in the range and return the column
		column := 0
		Loop % columns
		{
			highcoord := highcoord + columnWidth%A_Index%
			lowcoord := highcoord - columnWidth%A_Index%
			If x between %lowcoord% and %highcoord%
			{
				column := A_Index
				break
			}
		}
		return % column
	}

;ꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤꕤ
credit: original
alternative: link

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Cerberus, ioalex, mcl and 17 guests