Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "win32api.inc"
%IDC_ListView = 500
Global hDlg As Dword 'main dialog handle
Global CD() As Single
Function PBMain() As Long
Local i As Long
Dialog New Pixels, 0, "ListView Test",300,300,300,95,%WS_OverlappedWindow, 0 To hDlg
Control Add ListView, hDlg, %IDC_ListView, "", 10,10,100,75
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local i,w,h As Long
Select Case Cb.Msg
Case %WM_InitDialog
ReDim CD(3)
CD(1) = 0.6 : CD(2) = 0.25 : CD(3) = 0.15
For i = 1 To UBound(CD) : ListView Insert Column hDlg, %IDC_ListView, i, "Col"+Str$(i), CD(i), 20 : Next i 'any size will does, since WM_Size changes col width
Case %WM_Size
ResizeAll
' AdjustColumnSizes_Last
AdjustColumnSizes_Last_WithMinimum
DisplayResults
End Select
End Function
Sub ResizeAll
Local i,w,h As Long
Dialog Get Client hDlg To w,h
Control Set Size hDlg, %IDC_ListView, w-20, h-20
For i = 1 To UBound(CD) : ListView Set Column hDlg, %IDC_ListView, i, CD(i) * (w-20) : Next i
End Sub
Sub DisplayResults
Local w,h As Long
Control Get Client hDlg, %IDC_ListView To w,h
Dialog Set Text hDlg, "LVclient" + Str$(w) + " TotalColW" + Str$(GetTotalColW)
End Sub
Function GetTotalColW() As Long
Local i,w,wTotal As Long
For i = 1 To UBound(CD)
ListView Get Column hDlg, %IDC_ListView, i To w
wTotal = wTotal + w
Next i
Function = wTotal
End Function
Sub AdjustColumnSizes_Last 'adjust width of last column so that SumOfColumns = ClientWidth
Local wClient, hClient, wColTotal, wLastCol As Long
Control Get Client hDlg, %IDC_ListView To wClient,hClient
ListView Get Column hDlg, %IDC_ListView, UBound(CD) To wLastCol 'right most column, to be adjusted
ListView Set Column hDlg, %IDC_ListView, UBound(CD), wLastCol + wClient - GetTotalColW
End Sub
Sub AdjustColumnSizes_Last_WithMinimum 'no column < cm width
Local i,wClient, hClient, wColTotal, wCol, cm As Long
Control Get Client hDlg, %IDC_ListView To wClient,hClient
cm = 20
For i = 1 To UBound(CD) 'set all column widths to a min of cm
ListView Get Column hDlg, %IDC_ListView, i to wCol
If wCol < cm Then ListView Set Column hDlg, %IDC_ListView, i, cm
Next i
For i = UBound(CD) To 1 Step - 1
ListView Get Column hDlg, %IDC_ListView, i To wCol
If (wCol + wClient - GetTotalColW) > cm Then
ListView Set Column hDlg, %IDC_ListView, i, wCol + wClient - GetTotalColW
Exit For
End If
Next i
End Sub
'gbs_00989
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm