Click to See Complete Forum and Search --> : Listview / API


Sharathms
May 23rd, 2001, 07:04 AM
Hi,
Can some one please tell me the way to arrange the list view items when in report mode through code.
(IN the explorer, we can get this effet by pressing Ctrl and + [plus])
Thanks in advance,
Sharath

Isaacson
May 24th, 2001, 10:47 AM
You will need a module with the following:

option Explicit

Declare Function GetTextExtentPoint32 Lib "gdi32" Alias "GetTextExtentPoint32A" _
(byval hdc as Long, byval lpsz as string, byval cbString as Long, _
lpSize as TextSize_t) as Long
'computes the width and height of the specified text


' the struct type where some text's dims are returned
public Type TextSize_t
cx as Long
cy as Long
End Type





Then a form module with these items:


option Explicit
option Base 1 ' line up with listview
' make two command buttons: cmdColWidths and cmdGetSizes
' make a ListView called: ListView1
' feel free to email me: disaacson@novadigm.com

private Sub cmdColWidths_Click()
' get the current column widths in pixels
Dim CurrentMode as Long
Dim i as Integer
Dim ColWidths() as Long
Dim str as string

CurrentMode = me.ScaleMode

str = ""
ReDim ColWidths(ListView1.ColumnHeaders.Count)

for i = 1 to ListView1.ColumnHeaders.Count
ColWidths(i) = ListView1.ColumnHeaders(i).Width
str = str & i & ": " & ListView1.ColumnHeaders(i).Width & vbCrLf
next i

me.ScaleMode = CurrentMode

MsgBox str ' display the widths


End Sub


private Sub cmdExit_Click()
Unload me

End Sub


private Sub cmdGetSizes_Click()
Dim i as Long
Dim j as Long
Dim CurrentMode as Long ' scale mode of ListView
Dim TextSize as TextSize_t ' struct returned from API
Dim ColCount as Integer ' number of Columns
Dim ColSubItems as Integer ' number of SubItems
Dim ColSize() as Long ' sizes of ColumnHeaders

Dim HandleDeviceContext as Long
Dim sizeRet as Long


' setup
HandleDeviceContext = me.hdc

CurrentMode = me.ScaleMode
me.ScaleMode = 3 ' pixels
ColCount = ListView1.ColumnHeaders.Count
ColSubItems = ColCount - 1 ' = "size" column
ReDim ColSize(ColCount)

' col Header sizes
for i = 0 to ListView1.ColumnHeaders.Count
' you may wish to check the text of all the columnHeaders to see
' if they are the widest item...
next i

' ListItem sizes
for i = 1 to ListView1.ListItems.Count
' this is the only real secret here..
' This API call returns how big the text is,
' whether it is fully displayed or not
sizeRet = GetTextExtentPoint32(HandleDeviceContext, ListView1.ListItems(i), _
len(ListView1.ListItems(i)), TextSize)
ListView1.ListItems(i).SubItems(ColSubItems) = TextSize.cx & " , " & TextSize.cy

If TextSize.cx > ColSize(1) then
ColSize(1) = TextSize.cx ' save the largest value
End If

for j = 1 to ColSubItems
' look through each subitem for it's width
sizeRet = GetTextExtentPoint32(HandleDeviceContext, _
ListView1.ListItems(i).SubItems(j), _
len(ListView1.ListItems(i).SubItems(j)), _
TextSize)

If TextSize.cx > ColSize(j + 1) then
ColSize(j + 1) = TextSize.cx ' save the largest subitem
End If
next j


next i

' resize all cols as necessary
for i = 1 to ListView1.ColumnHeaders.Count
If ListView1.ColumnHeaders(i).Width < ColSize(i) then
' commented out next line for exact resize
' ListView1.ColumnHeaders(i).Width = ColSize(i) * 1.2
End If
next i
' OR
' resize all cols to fit exactly as needed
for i = 1 to ListView1.ColumnHeaders.Count
'1.2 seems to be the money scaling number for full visibility
ListView1.ColumnHeaders(i).Width = ColSize(i) * 1.2
next i

' for some reason, the last column always needs an extra increase in width
' I don't know why?
ListView1.ColumnHeaders(ListView1.ColumnHeaders.Count).Width = _
ListView1.ColumnHeaders(ListView1.ColumnHeaders.Count).Width * 1.15

' cleanup
me.ScaleMode = CurrentMode

End Sub


private Sub Form_Load()
Dim LstX as ListItem
Dim ColmX as ColumnHeader

' set up the ListView
ListView1.View = lvwReport ' 3

set ColmX = ListView1.ColumnHeaders.Add
ColmX.Text = "Name"
ColmX.Width = 1000

set ColmX = ListView1.ColumnHeaders.Add
ColmX.Text = "Address"
ColmX.Width = 1000

set ColmX = ListView1.ColumnHeaders.Add
ColmX.Text = "Phone"
ColmX.Width = 1000

set ColmX = ListView1.ColumnHeaders.Add
ColmX.Text = "size"
ColmX.Width = 1000


'populate the listview
set LstX = ListView1.ListItems.Add(, , "David Addison")
LstX.SubItems(1) = "125 Mercury Plaza"
LstX.SubItems(2) = "1234-5678"

set LstX = ListView1.ListItems.Add(, , "Allice Beasley")
LstX.SubItems(1) = "305 Boulevard"
LstX.SubItems(2) = "234-567-8900"

set LstX = ListView1.ListItems.Add(, , "Catherine Le Blank")
LstX.SubItems(1) = "801 Tejas Avenue"
LstX.SubItems(2) = "011-005-017-6067"

set LstX = ListView1.ListItems.Add(, , "Charles Grady")
LstX.SubItems(1) = "571 Hellena Court"
LstX.SubItems(2) = "909-090-9090"

set LstX = ListView1.ListItems.Add(, , "Harold Tice")
LstX.SubItems(1) = "21 Division Street"
LstX.SubItems(2) = "707-070-0077"

End Sub




This should give you a full demo project.
But, like it says in the code the real work is done by GetTextExtentPoint32