Jump to content

Autosize ListView using $LVSCW_AUTOSIZE and $LVSCW_AUTOSIZE_USEHEADER


Recommended Posts

Hi!

I'm trying to autosize ListViews using pixel width...

When $LVSCW_AUTOSIZE < $LVSCW_AUTOSIZE_USEHEADER then the column is sized by column header width.

When $LVSCW_AUTOSIZE > $LVSCW_AUTOSIZE_USEHEADER then the column is sized by item text width.

The little function I wrote works pretty well, but it is rather slow (when using large ListViews)...

Is there a way to improve performance?

Func _GUICtrlListView_SetColumnWidth2($hWnd, $iVal = 0)
    Local $vTmp = _GUICtrlListView_GetColumnCount($hWnd)
    If $iVal > $vTmp Then $iVal = $vTmp
    ; <...>
    _GUICtrlListView_SetColumnWidth($hWnd, $iVal, $LVSCW_AUTOSIZE)
    Local $aTmp1 = _GUICtrlListView_GetColumn($hWnd, $iVal)
    _GUICtrlListView_SetColumnWidth($hWnd, $iVal, $LVSCW_AUTOSIZE_USEHEADER)
    Local $aTmp2 = _GUICtrlListView_GetColumn($hWnd, $iVal)
    ; <...>
    If $aTmp1[4] < $aTmp2[4] Then _GUICtrlListView_SetColumnWidth($hWnd, $iVal, $LVSCW_AUTOSIZE_USEHEADER)
    If $aTmp1[4] > $aTmp2[4] Then _GUICtrlListView_SetColumnWidth($hWnd, $iVal, $LVSCW_AUTOSIZE)
EndFunc

Greets,

-supersonic.

Edited by supersonic
Link to comment
Share on other sites

  • Moderators

supersonic,

This is how I would do it:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ListViewConstants.au3>

Global $aData[5] = ["1|1|1", "2|2222222222222|2", "33333333|3333|33333333333333", "4|444444444444444444444444444|4", "5|5|5"]
Global $aRet, $iMaxElement = 0, $iGUI_Width = 500

; Create GUI
Global $hGUI = GUICreate("Test", $iGUI_Width, 500)

; Create List View
Global $hListView = GUICtrlCreateListView(" | | ", 10, 10, 480, 480, $LVS_SHOWSELALWAYS, BitOR($LVS_EX_FULLROWSELECT, $WS_EX_CLIENTEDGE))
; Add data
For $i = 0 To 4
    GUICtrlCreateListViewItem($aData[$i], $hListView)
Next

; Determine ListView width
$iLV_Width = 0
For $i = 0 To 2
    GUICtrlSendMsg($hListView, $LVM_SETCOLUMNWIDTH, $i, $LVSCW_AUTOSIZE)
    $iLV_Width += GUICtrlSendMsg($hListView, $LVM_GETCOLUMNWIDTH, $i, 0)
Next

; resize ListView and GUI
ControlMove($hGUI, "", $hListView, 10, 10, $iLV_Width + 10, 480) ; Add 10 becasue of internal borders
WinMove($hGUI, "", Default, Default, $iLV_Width + 30) ; Add 30 for internal and external borders

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

It gets a bit more complex if there are too many lines to fit the GUI because the vertical scroll bar will appear. I will try and work on that this afternoon. :D

I hope we are on the right track. :huggles:

M23

Edit: Tpying!

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

supersonic,

This version will automatically increase the width of the ListView if a scroll bar is needed:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ListViewConstants.au3>
#Include <GuiListView.au3>

Global $iColumns = 3, $sHeader = " | | ", $iScroll_Allowance = 0
Global $aData[5] = ["1|1|1", "2|2222222222222|2", "33333333|3333|33333333333333", "4|444444444444444444444444444|4", "5|5|5"]
Global $iGUI_Width = 500, $iGUI_Height = 200

; Create GUI at initial size
Global $hGUI = GUICreate("Test", $iGUI_Width, $iGUI_Height)

; Create List View at initial size
Global $hListView = GUICtrlCreateListView($sHeader, 10, 10, $iGUI_Width - 20, $iGUI_Height - 20, $LVS_SHOWSELALWAYS, BitOR($LVS_EX_FULLROWSELECT, $WS_EX_CLIENTEDGE))
; Add data
For $i = 0 To UBound($aData) - 1
    GUICtrlCreateListViewItem($aData[$i], $hListView)
Next

; Scroll down to ensure final item visible
_GUICtrlListView_EnsureVisible($hListView, UBound($aData) - 1)
; Check top index - if not 0 then we have a scroll bar so increase ListView width 
If _GUICtrlListView_GetTopIndex($hListView) > 0 Then
    $iScroll_Allowance = 17
EndIf
; Scroll to top again
_GUICtrlListView_EnsureVisible($hListView, 0)

; Determine ListView width
$iLV_Width = $iScroll_Allowance
For $i = 0 To $iColumns - 1
    GUICtrlSendMsg($hListView, $LVM_SETCOLUMNWIDTH, $i, $LVSCW_AUTOSIZE)
    $iLV_Width += GUICtrlSendMsg($hListView, $LVM_GETCOLUMNWIDTH, $i, 0)
Next

; Resize ListView and GUI to fit data
ControlMove($hGUI, "", $hListView, 10, 10, $iLV_Width + 10, $iGUI_Height - 20) ; Add 10 for internal ListView borders
WinMove($hGUI, "", Default, Default, $iLV_Width + 30) ; Add 30 for internal ListView and external GUI borders

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Are we getting there? :D

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Melba,

thank you, good point to start with...

But how can I autosize a column to make the whole header text visible (see attachment)?

In my ListViews the header text is often larger than the itmes themselves...

My function works well here, but it's rather slow when using rather large LVs ($array[300][50])...

post-34380-1264779697643_thumb.jpg

Edited by supersonic
Link to comment
Share on other sites

  • Moderators

supersonic,

Would Sir like fries with this? :huggles:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ListViewConstants.au3>
#Include <GuiListView.au3>

Global $iColumns = 3, $sHeader = "This header is longer than the items| | ", $iScroll_Allowance = 0
Global $aData[5] = ["1|1|1", "2|2222222222222|2", "33333333|3333|33333333333333", "4|444444444444444444444444444|4", "5|5|5"]
Global $iGUI_Width = 500, $iGUI_Height = 200

; Create GUI at initial size
Global $hGUI = GUICreate("Test", $iGUI_Width, $iGUI_Height)

; Create List View at initial size
Global $hListView = GUICtrlCreateListView($sHeader, 10, 10, $iGUI_Width - 20, $iGUI_Height - 20, $LVS_SHOWSELALWAYS, BitOR($LVS_EX_FULLROWSELECT, $WS_EX_CLIENTEDGE))
; Add data
For $i = 0 To UBound($aData) - 1
    GUICtrlCreateListViewItem($aData[$i], $hListView)
Next

; Scroll down to ensure final item visible
_GUICtrlListView_EnsureVisible($hListView, UBound($aData) - 1)
; Check top index - if not 0 then we have a scroll bar so increase ListView width
If _GUICtrlListView_GetTopIndex($hListView) > 0 Then
    $iScroll_Allowance = 17
EndIf
; Scroll to top again
_GUICtrlListView_EnsureVisible($hListView, 0)

; Determine ListView width
$iLV_Width = $iScroll_Allowance
For $i = 0 To $iColumns - 1

    ; Size column to fit header
    _GUICtrlListView_SetColumnWidth($hListView, $i, $LVSCW_AUTOSIZE_USEHEADER)
    $iHeader_Width = _GUICtrlListView_GetColumnWidth($hListView, $i)
    ; Now size column to fit data
    _GUICtrlListView_SetColumnWidth($hListView, $i, $LVSCW_AUTOSIZE)
    $iData_Width = _GUICtrlListView_GetColumnWidth($hListView, $i)
    ; If header is wider, reset width
    If $iHeader_Width > $iData_Width Then
        _GUICtrlListView_SetColumnWidth($hListView, $i, $iHeader_Width)
        $iLV_Width += $iHeader_Width
    Else
        $iLV_Width += $iData_Width
    EndIf

Next

; Resize ListView and GUI to fit data
ControlMove($hGUI, "", $hListView, 10, 10, $iLV_Width + 10, $iGUI_Height - 20) ; Add 10 for internal ListView borders
WinMove($hGUI, "", Default, Default, $iLV_Width + 30) ; Add 30 for internal ListView and external GUI borders

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

I have used _GUICtrlListView functions when we set the column width to make it easier to follow - all they do is wrap the GUICtrlSendMsg commands we used before. :D

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • 8 months later...
  • Moderators

hench,

Thank you for taking the time to say so. ;)

Merci pour avoir pris le temps de le dire*. :)

M23

*Je sais comment vous Québecois insistent que tout le monde parle en français! ;)

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...