Jump to content

Almost autosize all listview columns?


ycomp
 Share

Recommended Posts

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?

Link to comment
Share on other sites

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

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

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...