Jump to content

Recommended Posts

Posted

Hi,

I would like to have this kind of behaviour... but I'm not sure how to code it. Any suggestions appreciated.

(1) ListView autosizes all columns (that is what I am doing now)

works great except if there is not enough room for all the text in the items.

(2) Then it shrinks the first columns and leaves the last one wide

new logic desired:

- If (and only if) (2) occurs then I would like to autosize all columns except the last one. The last one then will either be shrunk to fit the remaining space... or it will be autosized wide enough for its text, but not to the detriment of the previous columns. That is, it would overflow to the right past the width of the window, so I would have to scroll to see all of the text.

anyone have some ideas?

Posted

I use this:

#include <GuiConstants.au3>

$Main_GUI = GUICreate("ListView Set Equel Column Width", 340, 260, -1, -1, $WS_OVERLAPPEDWINDOW)

$ListView = GUICtrlCreateListView("col1|col2|col3", 20, 30, 300, 150, _
    $LVS_SHOWSELALWAYS+$LVS_NOSORTHEADER, $LVS_EX_FULLROWSELECT+$LVS_EX_GRIDLINES)

GUICtrlCreateListViewItem("line1|data1|more1", $ListView)
GUICtrlCreateListViewItem("line2|data2|more2", $ListView)
GUICtrlCreateListViewItem("line3|data3|more3", $ListView)
GUICtrlCreateListViewItem("line4|data4|more4", $ListView)
GUICtrlCreateListViewItem("line5|data5|more5", $ListView)

$Set_Equel_Button = GUICtrlCreateButton("Set Equel Width", 20, 190, 100, 20)
$Set_Full_Button = GUICtrlCreateButton("Set Full Width", 20, 220, 100, 20)

GUISetState()

GUIRegisterMsg($WM_SIZE, "WM_SIZE")

While 1
    $iMsg = GUIGetMsg()
    Switch $iMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Set_Full_Button, $GUI_EVENT_MAXIMIZE, $GUI_EVENT_RESTORE
            _GUICtrlListView_SetEquelColWidth($Main_GUI, $ListView, 1)
        Case $Set_Equel_Button
            _GUICtrlListView_SetEquelColWidth($Main_GUI, $ListView, 0)
    EndSwitch
WEnd

Func _GUICtrlListView_SetEquelColWidth($hWnd, $hLV, $iFullMode=1)
    If Not IsHWnd($hLV) Then $hLV = ControlGetHandle($hWnd, "", $hLV)
    If @error Then Return SetError(1, 0, -1)
    
    Local $hUser32DllOpen = DllOpen("user32.dll")
    If $hUser32DllOpen = -1 Then Return SetError(2, 0, -1)
    
    Local $LV_Header = DllCall($hUser32DllOpen, "long", "SendMessage", "hwnd", $hLV, "int", $LVM_GETHEADER, "int", 0, "int", 0)
    If @error Then Return SetError(3, 0, -1)
    
    ;Local Const $HDM_GETITEMCOUNT = 0x1200
    Local $sItmsCnt = DllCall($hUser32DllOpen, "long", "SendMessage", "hwnd", $LV_Header[0], "int", 0x1200, "int", 0, "int", 0)
    If @error Then Return SetError(4, 0, -1)
    
    Local $Columns_Count = $sItmsCnt[0]
    Local $iLV_Width = 0
    
    Switch $iFullMode
        Case 0
            Local $Columns_Width = 0
            Local $GetColumns_Width
            
            For $i = 0 To $Columns_Count
                $GetColumns_Width = DllCall($hUser32DllOpen, "long", "SendMessage", _
                    "hwnd", $hLV, "int", $LVM_GETCOLUMNWIDTH, "int", $i, "int", 0)
                $Columns_Width += $GetColumns_Width[0]
            Next
            
            $iLV_Width = $Columns_Width / $Columns_Count
        Case 1
            Local $sLV_Width = ControlGetPos($hWnd, "", $hLV)
            If @error Then Return SetError(5, 0, 0)
            
            $iLV_Width = ($sLV_Width[2]-10) / $Columns_Count
        Case Else
            Return SetError(6, 0, -1)
    EndSwitch
    
    For $i = 0 To $Columns_Count
        DllCall($hUser32DllOpen, "long", "SendMessage", "hwnd", $hLV, "int", $LVM_SETCOLUMNWIDTH, "int", $i, "int", $iLV_Width)
    Next
    
    DllClose($hUser32DllOpen)
EndFunc

Func WM_SIZE($hWndGUI, $MsgID, $WParam, $LParam)
    ;По каким то причинам функция не срабатывает корректно при разворачивании/восстановлении,
    ;поэтому пусть стандартный обработчик справляется с этими событиями ($GUI_EVENT_MAXIMIZE, $GUI_EVENT_RESTORE)..
    _GUICtrlListView_SetEquelColWidth($hWndGUI, $ListView, 1)
EndFunc

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

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
  • Recently Browsing   0 members

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