ListView Extending class - defining selection
Posted: 14 May 2022, 07:16
I tried to add some easy to use properties to the listview.
For example LV.table returns the content, but also can be used to set a specific content.
LV.Selection_table also works, but I would prever that I could use LV.Selection.table to retrieve the selected content, but this seems to be failing.
Does anybody can give me tips how to fix this/ or a better shorter way to define this?
For example LV.table returns the content, but also can be used to set a specific content.
LV.Selection_table also works, but I would prever that I could use LV.Selection.table to retrieve the selected content, but this seems to be failing.
Does anybody can give me tips how to fix this/ or a better shorter way to define this?
Code: Select all
#SingleInstance force
class ListView_Ext extends Gui.ListView { ; Technically no need to extend classes unless
Static __New() { ; you are attaching new base on control creation.
For prop in this.Prototype.OwnProps()
super.Prototype.%prop% := this.Prototype.%prop%
super.Prototype.DefineProp("hwndHeader", { get: (this) => (hwndHeader := SendMessage(LVM_GETHEADER := 0x101F, 0, 0, , "ahk_id " . This.hwnd), (hwndHeader = "FAIL" ? False : hwndHeader)) })
super.Prototype.DefineProp("columns", { get: (this) => (this.GetCount("Col"))})
super.Prototype.DefineProp("rows", { get: (this) => (this.GetCount(""))})
super.Prototype.DefineProp("table", { get: (this) => (this.GetTable(this)), Set: (this, value) => (this.SetTable(value))})
super.Prototype.DefineProp("headerArray", { get: (this) => (this.GetHeaderArray()), Set: (this, value) => (this.SetHeaderArray(value))})
super.Prototype.DefineProp("selection_table", { get: (this) => (this.GetTable("selected"))})
super.Prototype.DefineProp("selection_rows", { get: (this) => (this.GetCount("S"))})
super.Prototype.DefineProp("selection_row", { get: (this) => (this.GetNext()) })
; super.Prototype.Selection := Object()
; super.Prototype.Selection.parent := this
; super.Prototype.Selection.DefineProp("table", { get: (this) => (this.parent.GetTable("selected"))})
; super.Prototype.Selection.DefineProp("rows", { get: (this) => (this.GetCount("S"))})
}
Checked(row) => (SendMessage(4140, row - 1, 0xF000, , "ahk_id " this.hwnd) >> 12) - 1 ; VM_GETITEMSTATE = 4140 / LVIS_STATEIMAGEMASK = 0xF000
IconIndex(row, col := 1) { ; from "just me" LV_EX ; Link: https://www.autohotkey.com/boards/viewtopic.php?f=76&t=69262&p=298308#p299057
LVITEM := Buffer((A_PtrSize = 8) ? 56 : 40, 0) ; create variable/structure
NumPut("UInt", 0x2, "Int", row - 1, "Int", col - 1, LVITEM.ptr, 0) ; LVIF_IMAGE := 0x2 / iItem (row) / column num
NumPut("Int", 0, LVITEM.ptr, (A_PtrSize = 8) ? 36 : 28) ; iImage
SendMessage(StrLen(Chr(0xFFFF)) ? 0x104B : 0x1005, 0, LVITEM.ptr, , "ahk_id " this.hwnd) ; LVM_GETITEMA/W := 0x1005 / 0x104B
return NumGet(LVITEM.ptr, (A_PtrSize = 8) ? 36 : 28, "Int") + 1 ;iImage
}
GetColWidth(n) => (SendMessage(0x101D, n - 1, 0, this.hwnd))
GetTable(Options := "") {
Result := ""
Loop this.GetCount("Column") {
Result .= (A_Index = 1) ? "" : "`t"
Result .= this.GetText(0, A_Index)
}
Result .= "`n" ListViewGetContent(Options, this)
return Result
}
SetTable(Table) {
this.Opt("-Redraw")
; Clear all columns
this.Delete()
Loop this.GetCount("Column") {
this.DeleteCol(1)
}
loop parse Table, "`n", "`r" {
aRow := StrSplit(A_LoopField, "`t")
if (A_index = 1) {
for Index, Header in aRow {
this.InsertCol(Index, , Header)
}
} else {
this.Add("", aRow*)
}
}
this.ModifyCol
this.Opt("+Redraw")
return
}
GetHeaderArray() {
Result := ""
Loop This.GetCount("Column") {
Result .= (A_Index = 1) ? "" : "`t"
Result .= This.GetText(0, A_Index)
}
return Result
}
SetHeaderArray(aHeaders) {
this.Opt("-Redraw")
for index, HeaderText in aHeaders {
this.ModifyCol(Index,,HeaderText)
}
this.Opt("+Redraw")
}
}
; Create the window:
MyGui := Gui()
; Create the ListView with two columns, Name and Size:
LV := MyGui.Add("ListView", "r20 w700", ["Name","Size"])
; Gather a list of file names from a folder and put them into the ListView:
Loop Files, A_MyDocuments "\*.*"
LV.Add("select", A_LoopFileName, A_LoopFileSizeKB)
LV.ModifyCol ; Auto-size each column to fit its contents.
; Display the window:
MyGui.Show
; sleep 5000
lv.headerArray := ["test","test2"]
MsgBox(lv.selection.table)