Post by metro on Mar 30, 2018 17:56:42 GMT -5
This is the best way I've found, thanks to Brandon
'Insert code here
NoMainWin
Global False : False = 0
Global True : True = 1
Global currentRow : currentRow = 0
listViewHandle(0) = _NULL
Call initializeListViewConstants
WindowWidth = 530
WindowHeight = 335
Button #Example.btnAddItem, "Add Item", AddItem, UL, 5, (WindowHeight - 65)
Button #Example.btnEditItem, "Edit Item", EditItem, UL, 202.5, (WindowHeight - 65)
Button #Example.btnDeleteItem, "Delete Item", DeleteItem, UL, 405, (WindowHeight - 65)
Open "ListView Example" For Window As #Example
Call InitCommonControls
style = _WS_CHILD Or _WS_VISIBLE Or listViewConstants.LVS.NOSORTHEADER.struct _
Or listViewConstants.LVS.REPORT.struct Or listViewConstants.LVS.SHOWSELALWAYS.struct _
Or listViewConstants.LVS.SINGLESEL.struct
styleEx = _WS_EX_CLIENTEDGE Or _WS_EX_DLGMODALFRAME 'Or listViewConstants.LVS.EX.DOUBLEBUFFER.struct
listViewHandle(0) = CreateListView(style, styleEx, Hwnd(#Example), GetWindowLong(Hwnd(#Example), _GWL_HINSTANCE), 5, 23, 505, 244)
result = setListViewFullRowSelect(listViewHandle(0))
result = listViewShowGrid(listViewHandle(0))
result = createNewListColumn(listViewHandle(0), 0, 100, " Item 1")
result = createNewListColumn(listViewHandle(0), 1, 100, " Item 2")
result = createNewListColumn(listViewHandle(0), 2, 100, " Item 3")
#Example "TrapClose Quit"
While Hwnd(#Example)
Scan
Wend
Sub Quit handle$
result = DestroyWindow(listViewHandle(0))
Close #handle$
End
End Sub
Sub AddItem handle$
result = AddListViewData(listViewHandle(0), currentRow, 0, "Data1")
result = EditListViewData(listViewHandle(0), currentRow, 1, "Data2")
result = EditListViewData(listViewHandle(0), currentRow, 2, "Data3")
result = SelectListViewRow(listViewHandle(0), currentRow, True)
currentRow = (currentRow + 1)
End Sub
Sub EditItem handle$
numListViewEntries = GetListViewItemCount(listViewHandle(0))
If Not(numListViewEntries) Then Exit Sub
row = GetSelectedListViewRow(listViewHandle(0))
item1$ = GetListViewText$(listViewHandle(0), row, 0)
item2$ = GetListViewText$(listViewHandle(0), row, 1)
item3$ = GetListViewText$(listViewHandle(0), row, 2)
result = EditListViewData(listViewHandle(0), row, 0, GetListViewText$(listViewHandle(0), row, 0) + " Edited")
result = EditListViewData(listViewHandle(0), row, 1, GetListViewText$(listViewHandle(0), row, 1) + " Edited")
result = EditListViewData(listViewHandle(0), row, 2, GetListViewText$(listViewHandle(0), row, 2) + " Edited")
result = SelectListViewRow(listViewHandle(0), row, True)
End Sub
Sub DeleteItem handle$
numListViewEntries = GetListViewItemCount(listViewHandle(0))
If Not(numListViewEntries) Then Exit Sub
row = GetSelectedListViewRow(listViewHandle(0))
result = SendMessageLVITEMStruct(listViewHandle(0), listViewConstants.LVM.DELETEITEM.struct, row)
currentRow = (currentRow - 1)
End Sub
Sub InitCommonControls
CallDLL #comctl32, "InitCommonControls", ret As void
End Sub
Sub initializeListViewConstants
'Create the structs for ListViews
Struct LVITEM, mask As ulong, _
iItem As long, _
iSubItem As long, _
state As ulong, _
stateMask As ulong,_
pszText As ptr, _
cchTextMax As long, _
iImage As long, _
lParam As long, _
iIndent As long
Struct LVCOLUMN, mask As ulong, _
fmt As long, _
cx As long, _
pszText As ptr, _
cchTextMax As long, _
iSubItem As long, _
iImage As long, _
iOrder As long
Struct LVFINDINFO, flags As ulong, _
psz As ptr, _
lParam As ulong, _
pt As ulong, _ 'point structure
vkDirection As ulong
Struct listViewConstants, LVP.DBLCLK As long, _
LVS.REPORT As long, _
LVS.SINGLESEL As long, _
LVS.SHOWSELALWAYS As long, _
LVS.NOSORTHEADER As long, _
LVS.EXDOUBLEBUFFER As long, _
LVS.EX.FULLROWSELECT As long, _
LVS.EX.CHECKBOXES As long, _
LVIS.FOCUSED As long, _
LVIS.UNSELECTED As long, _
LVIS.SELECTED As long, _
LVIS.CHECKED As long, _
LVIS.UNCHECKED As long, _
LVIS.STATEIMAGEMASK As long, _
LVIF.TEXT As long, _
LVIF.STATE As long, _
LVIR.BOUNDS As long, _
LVFI.STRING As long, _
LVFI.PARTIAL As long, _
LVN.ITEMCHANGED As long, _
LVM.ENSUREVISIBLE As long, _
LVM.SETITEMSTATE As long, _
LVM.GETITEMSTATE As long, _
LVM.GETITEMTEXT As long, _
LVM.FINDITEMA As long, _
LVM.SETITEM As long, _
LVM.GETITEMCOUNT As long, _
LVM.GETITEMA As long, _
LVM.GETSUBITEMRECT As long, _
LVM.DELETEITEM As long, _
LVM.INSERTITEM As long, _
LVM.INSERTCOLUMN As long, _
LVM.SETEXTENDEDLISTVIEWSTYLE As long
'LVM_FIRST = &H1000 or 4096
listViewConstants.LVP.DBLCLK.struct = False
listViewConstants.LVS.REPORT.struct = 1
listViewConstants.LVS.SINGLESEL.struct = 4
listViewConstants.LVS.SHOWSELALWAYS.struct = 8
listViewConstants.LVS.NOSORTHEADER.struct = 32768
listViewConstants.LVS.EXDOUBLEBUFFER.struct = 10000
listViewConstants.LVS.EX.FULLROWSELECT.struct = 32
listViewConstants.LVS.EX.CHECKBOXES.struct = 4
listViewConstants.LVIS.FOCUSED.struct = 1
listViewConstants.LVIS.UNSELECTED.struct = 0
listViewConstants.LVIS.SELECTED.struct = 2
listViewConstants.LVIS.CHECKED.struct = 8192
listViewConstants.LVIS.UNCHECKED.struct = 4096
listViewConstants.LVIS.STATEIMAGEMASK.struct = 61440
listViewConstants.LVIF.TEXT.struct = 1
listViewConstants.LVIF.STATE.struct = 8
listViewConstants.LVIR.BOUNDS.struct = 0
listViewConstants.LVFI.STRING.struct = 2
listViewConstants.LVFI.PARTIAL.struct = 8
listViewConstants.LVN.ITEMCHANGED.struct = -101
listViewConstants.LVM.ENSUREVISIBLE.struct = 4115
listViewConstants.LVM.SETITEMSTATE.struct = 4139
listViewConstants.LVM.GETITEMSTATE.struct = 4140
listViewConstants.LVM.GETITEMTEXT.struct = 4141
listViewConstants.LVM.FINDITEMA.struct = 4109
listViewConstants.LVM.SETITEM.struct = 4102
listViewConstants.LVM.GETITEMCOUNT.struct = 4100
listViewConstants.LVM.GETITEMA.struct = 4101
listViewConstants.LVM.GETSUBITEMRECT.struct = 4152
listViewConstants.LVM.DELETEITEM.struct = 4104
listViewConstants.LVM.INSERTITEM.struct = 4103
listViewConstants.LVM.INSERTCOLUMN.struct = 4123
listViewConstants.LVM.SETEXTENDEDLISTVIEWSTYLE.struct = 4150
End Sub
Function CreateListView(ByRef style, ByRef styleEx, ByRef hParent, ByRef hInstance, ByRef xloc, ByRef yloc, ByRef width, ByRef height)
className$ = "SysListView32" + chr$(0)
CallDLL #user32, "CreateWindowExA", styleEx As ulong, _ ' extended style
className$ As ptr, _ ' class name
"" As ptr, _
style As ulong, _ ' style
xloc As long, _ ' left x
yloc As long, _ ' top y
width As long, _ ' width
height As long, _ ' height
hParent As ulong, _ ' parent hWnd
_NULL As ulong, _
hInstance As ulong, _ ' hInstance
_NULL As ulong, _
CreateListView As ulong 'Hwnd to ListView
'LVCF.WIDTH = 2 : LVCF.TEXT = 4
LVCOLUMN.mask.struct = LVCF.WIDTH OR LVCF.TEXT
End Function
Function GetWindowLong(hWnd, index)
CallDLL #user32, "GetWindowLongA", hWnd As ulong, _
index As long, _
GetWindowLong As long
End Function
Function GetListViewItemCount(hWndLV)
GetListViewItemCount = SendMessage(hWndLV, listViewConstants.LVM.GETITEMCOUNT.struct, 0, 0)
End Function
Function createNewListColumn(hListView, columnIndex, columnWidth, headerCaption$)
LVCOLUMN.cx.struct = columnWidth
LVCOLUMN.pszText.struct = headerCaption$
LVCF.WIDTH = 2 : LVCF.TEXT = 4
LVCOLUMN.mask.struct = LVCF.WIDTH OR LVCF.TEXT
createNewListColumn = SendMessageLVCOLUMNStruct(hListView, listViewConstants.LVM.INSERTCOLUMN.struct, columnIndex)
End Function
Function AddListViewData(hWndLV, row, column, myData$)
LVITEM.mask.struct = listViewConstants.LVIF.TEXT.struct
LVITEM.iItem.struct = row
LVITEM.iSubItem.struct = column
LVITEM.pszText.struct = myData$
result = SendMessageLVITEMStruct(hWndLV, listViewConstants.LVM.INSERTITEM.struct, 0)
AddListViewData = SendMessage(hWndLV, listViewConstants.LVM.GETITEMSTATE.struct, 0, 0)
End Function
Function EditListViewData(hWndLV, row, column, myData$)
LVITEM.mask.struct = listViewConstants.LVIF.TEXT.struct
LVITEM.iItem.struct = row
LVITEM.iSubItem.struct = column
LVITEM.pszText.struct = myData$
EditListViewData = SendMessageLVITEMStruct(hWndLV, listViewConstants.LVM.SETITEM.struct, 0)
End Function
Function SelectListViewRow(hWndLV, row, setFocus)
LVITEM.iItem.struct = row
LVITEM.iSubItem.struct = 0
LVITEM.mask.struct = listViewConstants.LVIF.STATE.struct
LVITEM.state.struct = listViewConstants.LVIS.SELECTED.struct
If setFocus = True Then
LVITEM.state.struct = (LVITEM.state.struct Or listViewConstants.LVIS.FOCUSED.struct)
End If
'SelectListViewRow = SetFocus(hWndLV)
SelectListViewRow = SendMessage(hWndLV, listViewConstants.LVM.ENSUREVISIBLE.struct, row, False)
SelectListViewRow = SendMessageLVITEMStruct(hWndLV, listViewConstants.LVM.SETITEM.struct, 0)
End Function
Function setListViewFullRowSelect(hListView)
setListViewFullRowSelect = SendMessage(hListView, 4150, 32, 32)'LVS_EX_FULLROWSELECT
End Function
Function listViewShowGrid(hListView)
listViewShowGrid = SendMessage(hListView, 4150, 1, 1)'LVS_EX_GRIDLINES
End Function
Function SendMessage(hWnd, Msg, wParam, lParam)
CallDLL #user32, "SendMessageA" , hWnd As ulong, _
Msg As long, _
wParam As long, _
lParam As long, _
SendMessage As long
End Function
Function SendMessageLVITEMStruct(hWnd, Msg, wParam)
CallDLL #user32, "SendMessageA" , hWnd As ulong, _
Msg As long, _
wParam As long, _
LVITEM As struct, _
SendMessageLVITEMStruct As long
End Function
Function SendMessageLVCOLUMNStruct(hWnd, Msg, wParam)
CallDLL #user32, "SendMessageA" , hWnd As ulong, _
Msg As long, _
wParam As long, _
LVCOLUMN As struct, _
SendMessageLVCOLUMNStruct As long
End Function
Function DestroyWindow(hWnd)
CallDLL #user32, "DestroyWindow", hWnd As ulong, _
DestroyWindow As long
End Function
Function GetSelectedListViewRow(hWndLV)
GetSelectedListViewRow = -1
numListViewEntries = GetListViewItemCount(hWndLV)
For row = 0 To (numListViewEntries - 1)
LVITEM.mask.struct = listViewConstants.LVIF.STATE.struct
LVITEM.iItem.struct = row
LVITEM.iSubItem.struct = 0 'first column
LVITEM.stateMask.struct = listViewConstants.LVIS.SELECTED.struct
result = SendMessageLVITEMStruct(hWndLV, listViewConstants.LVM.GETITEMA.struct, 0)
If LVITEM.state.struct And listViewConstants.LVIS.SELECTED.struct Then
GetSelectedListViewRow = row
Exit Function
Else
If row = (numListViewEntries - 1) Then
GetSelectedListViewRow = -1
Exit Function
End If
End If
Next row
End Function
Function GetListViewText$(hWndLV, row, column)
LVITEM.mask.struct = listViewConstants.LVIF.TEXT.struct
LVITEM.iSubItem.struct = column
LVITEM.pszText.struct = Space$(_MAX_PATH) + chr$(0)
LVITEM.cchTextMax.struct = Len(Winstring(LVITEM.pszText.struct))
EditListViewData = SendMessageLVITEMStruct(hWndLV, listViewConstants.LVM.GETITEMTEXT.struct, row)
GetListViewText$ = Trim$(Winstring(LVITEM.pszText.struct))
End Function