#include once "window9.bi"

Sub DeleteListViewColumn(ByVal gadget As Long, ByVal columnIndex As Long) Export
    dim as hwnd hWin = GadgetID(gadget)
    dim as any ptr pStackIndexes = GetProp(hWin, "stackindexes")
    dim as HIMAGELIST imglist = Cast(HIMAGELIST,GetHimageCombo_(gadget))
    if imgList then
        if columnIndex = 0 then
            for i as Long = 0 to 1
                dim lvcol as LVCOLUMN
                lvcol.mask = LVCF_SUBITEM
                if SendMessage(hWin,LVM_GETCOLUMN , i , cast(lparam,@lvcol)) then
                else
                    if i = 1 then
                        ImageList_RemoveAll(imgList)
                        if pStackIndexes then freeStackFreeIndexes(pStackIndexes)
                        SendMessage(hWin,LVM_DELETECOLUMN,CInt(columnIndex),0)
                        exit sub
                    EndIf
                EndIf
            next
        endif
        if pStackIndexes then
            dim lvcol as LVCOLUMN
            lvcol.mask = LVCF_SUBITEM
            if SendMessage(hWin,LVM_GETCOLUMN , columnIndex , cast(lparam,@lvcol)) then
                for i as Long = 0 to SendMessage(hWin,LVM_GETITEMCOUNT,0,0) - 1
                    Dim LV_ITEM_ As LV_ITEM
                    LV_ITEM_.mask=LVIF_IMAGE
                    LV_ITEM_.iItem = i
                    LV_ITEM_.iSubItem = columnIndex
                    if SendMessage(hWin,LVM_GETITEM,0,Cast(lparam,@LV_ITEM_)) then
                        if LV_ITEM_.iImage <> -2 then
                            PushStackFreeIndexes(pStackIndexes , LV_ITEM_.iImage)
                        EndIf
                    endif
                next
            endif
        endif
    EndIf
    SendMessage(Cast(hwnd,GadgetID(gadget)),LVM_DELETECOLUMN,CInt(columnIndex),0)
End Sub
