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_ListBox = 500 : %IDC_GetWidth = 501 : %IDC_AutoSize = 502
Global hDlg,hFont As Dword
Function PBMain() As Long
Dim MyArray(3) As String
Array Assign MyArray() = "zero is good", "one is better", "two is almost there", "three is the very best there is"
Dialog New Pixels, 0, "ListBox Test",300,300,200,200, %WS_SysMenu, 0 To hDlg
Control Add Button, hDlg, %IDC_GetWidth, "Get Max Width", 10,10,100,20
Control Add Button, hDlg, %IDC_AutoSize, "AutoSize", 120,10,50,20
Font New "MS Sans Serif", 8, 0 To hFont 'font to use
Control Add ListBox, hDlg, %IDC_ListBox, MyArray(), 10,40,180,150, %WS_TabStop Or %WS_VScroll Or %LBS_NoIntegralHeight, %WS_Ex_ClientEdge
Control Set Font hDlg, %IDC_ListBox, hFont 'listbox uses the font
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local h,RowCount As Long
If Cb.Msg = %WM_Command And Cb.Ctl = %IDC_GetWidth And Cb.CtlMsg = %BN_Clicked Then
Control Send hDlg, %IDC_ListBox, %LB_GetItemheight, 0, 0 To h
? "Max Data Width: " + Str$(MaxWidth) + " Row Height: " + Str$(h)
ElseIf Cb.Msg = %WM_Command And Cb.Ctl = %IDC_AutoSize And Cb.CtlMsg = %BN_Clicked Then
ListBox Get Count hDlg, %IDC_ListBox To RowCount
Control Send hDlg, %IDC_ListBox, %LB_GetItemheight, 0, 0 To h
Control Set Size hDlg, %IDC_ListBox, MaxWidth+10,h*RowCount+6 '10/6 covers ListBox borders/margins
End If
End Function
Function MaxWidth() As Long
Local i,wMax,w,h,RowCount As Long, temp$, hBMP As Long
'create a bitmap using hFont so can measure size of text
Graphic Bitmap New 10,10 To hBMP : Graphic Attach hBMP,0 : Graphic Set Font hFont
'go through all rows, saving size of longest row
ListBox Get Count hDlg, %IDC_ListBox To RowCount
For i = 1 To RowCount
ListBox Get Text hDlg, %IDC_ListBox, i To temp$ 'text from each row
Graphic Text Size temp$ To w,h 'width of text
wMax = Max(wMax,w) 'keep largest width
Next i
Function = wMax
End Function
'gbs_00792
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm