Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
#Compiler PBWin 10
#Compile Exe
#Dim All
'%Unicode=1
#Include "Win32api.inc"
%IDC_ListView = 500
Global hDlg, hListView, hDC As Dword, R,R2 As Rect
Global DeltaY, OrigLVProc,StartSelRow,EndSelRow,LVHeight,RowHeight,MaxVisibleRows,TopVisibleRow,BottomRow As Long
Function PBMain() As Long
Dialog New Pixels, 0, "ListView Test",300,300,400,220, %WS_OverlappedWindow To hDlg
CreateListView
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local hdnptr As HD_NOTIFY Ptr, hdiptr As HD_ITEM Ptr
Select Case Cb.Msg
Case %WM_InitDialog
MaxVisibleRows = LVHeight / RowHeight
TopVisibleRow = 1 : BottomRow = 13
OrigLVProc = SetWindowLong(hListView, %GWL_WndProc, CodePtr(NewLVProc)) 'subclass
Case %WM_Destroy
SetWindowLong hListView, %GWL_WNDPROC, OrigLVProc
End Select
End Function
Function GetItemUnderCursor() As Long
Local pt As Point
GetCursorPos pt
Function = 5
End Function
Function NewLVProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Local RowUnderMouse As Long 'takes on a value from 1 to MaxVisibleRows unless off controls. Then grows in - or + direction.
Local pt As Point
Select Case Msg
Case %WM_LButtonDown
If GetFocus = hListView Then
R.nLeft = Lo(Word, LParam) : R.nTop = Hi(Word, LParam)
SetCapture hListView
RemoveSelections
TopVisibleRow = ListView_GetTopIndex(hListView)
StartSelRow = TopVisibleRow + (Hi(Word,LParam)-14) / RowHeight
EndSelRow = StartSelRow
SelectItems
Function = 0 : Exit Function
End If
Case %WM_MouseMove
If GetCapture = hListView Then
R.nRight = Lo(Word, LParam) : R.nBottom = Hi(Word, LParam)
If (WParam And %MK_LBUTTON) Then DrawRect 1
Dialog Set Text hDlg, Str$(Lo(Word,LParam)) + Str$(Hi(Word,LParam))
pt.y = Hi(Word,LParam)
If pt.y > 1000 Then pt.y = 0
RowUnderMouse = (pt.y-14) / RowHeight
If RowUnderMouse < 1 Then SendMessage hListView, %LVM_Scroll, 0, -LVHeight/2
If RowUnderMouse > MaxVisibleRows Then SendMessage hListView, %LVM_Scroll, 0, LVHeight/2
TopVisibleRow = ListView_GetTopIndex(hListView)
BottomRow = TopVisibleRow + MaxVisibleRows
EndSelRow = TopVisibleRow + RowUnderMouse
RemoveSelections
SelectItems
Function = 0 : Exit Function
End If
Case %WM_LButtonUp
DrawRect 0
ReleaseCapture
End Select
Function = CallWindowProc(OrigLVProc, hWnd, Msg, wParam, lParam)
End Function
Sub CreateListview
Local i As Long, rc As Rect, LVIX As LVitemIndex
LVHeight = 200
Control Add ListView, hDlg, %IDC_ListView,"", 10,10,380,LVHeight ', %LVS_NoColumnHeader
Control Handle hDlg, %IDC_ListView To hListView
ListView Set StyleXX hDlg, %IDC_ListView, %LVS_Ex_GridLines
ListView Insert Column hDlg, %IDC_ListView, 1, "Column1", 100, 0
ListView Insert Column hDlg, %IDC_ListView, 2, "Column2", 100, 0
ListView Insert Column hDlg, %IDC_ListView, 3, "Column3", 100, 0
For i = 1 To 100
ListView Insert Item hDlg, %IDC_ListView, i,0, "Row " + Str$(i)
ListView Set Text hDlg, %IDC_ListView, i, 2, "Col2 " + "Row " + Format$(i,"00")
ListView Set Text hDlg, %IDC_ListView, i, 3, "Col3 " + "Row " + Format$(i,"00")
Next i
ListView_GetItemRect hListView, 0, rc, %LVIR_Label
RowHeight = rc.nBottom - rc.nTop
End Sub
Sub RemoveSelections
Local i,iCount As Long
ListView Get Count hDlg, %IDC_ListView To iCount
For i = 1 To iCount
ListView Unselect hDlg, %IDC_ListView, i
Next i
End Sub
Sub SelectItems
Local i As Long
If StartSelRow > EndSelRow Then
For i = EndSelRow To StartSelRow
ListView Select hDlg, %IDC_ListView, i
Next i
Else
For i = StartSelRow To EndSelRow
ListView Select hDlg, %IDC_ListView, i
Next i
End If
End Sub
Sub DrawRect(Flag As Long) '1=moving 0=done
Local i As Long, tempR As Rect
hDC = GetDC(hDlg)
DrawFocusRect hDC, R2
tempR.nLeft = Min(R.nLeft,R.nRight)
tempR.nRight = Max(R.nLeft,R.nRight)
tempR.nTop = Min(R.nTop,R.nBottom)
tempR.nBottom = Max(R.nTop,R.nBottom)
If Flag Then DrawFocusRect hDC, tempR : R2 = tempR Else Reset R2
ReleaseDc hDlg, hDC
End Sub
'gbs_01257
'Date: 05-11-2013
http://www.garybeene.com/sw/gbsnippets.htm