• Welcome to Jose's Read Only Forum 2023.
 

Cannot change Column width in a listview

Started by Chris Chancellor, November 03, 2018, 04:38:31 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Chris Chancellor

Hello all

i have a ListView program , which i tried changing the column widths but unsuccessful.

i have use
                lvc.mask = LVCF_FMT OR   LVCF_WIDTH   OR  LVCF_TEXT  OR LVCF_SUBITEM
                lvc.cx      = 25

where LVCF_WIDTH   style and lvc.cx  are use to modify the column widths

not sure why it couldn't change the width like the below code didn't work for the first column


      'Setup the  ListView Column Headers
      '        The  first column
       '        lvc.mask =    LVCF_WIDTH   
           '    lvc.cx      = 30
        '      txtStr="Column #" & str(1)
       '        lvc.pszText = txtStr   
         '      lvc.iorder = 0       
         '     ListView_InsertColumn(hListview, 0, &lvC)



And the entire code for the Listview program is as below


'====================================================================
' Listview example, nested modeless dialog.  modified Nov 2 2018
'====================================================================
$ filename "ListView_64M.exe"
use rtl64


uses dialogs


'Identifiers
#define IDD_DLG1 2000
#define IDC_LSV1  2001


'  The program logo icon  is obtained from the resource file
'  the 1000 must corespondence to the 1000 in the rc file
   #define IDI_LOGO     1000
   % ICON_BIG=1
   % WM_SETICON=0x80


macro ListView_InsertColumn(hwnd,iCol,pcol) (SendMessage(hwnd, LVM_INSERTCOLUMN, iCol, pcol))
macro ListView_SetColumnWidth(hwnd,iCol,cx) (SendMessage(hwnd, LVM_SETCOLUMNWIDTH, iCol, cx))
macro ListView_InsertItem(hwnd,pitem) (SendMessage(hwnd, LVM_INSERTITEM,0, pitem))
macro ListView_SetItem(hwnd,pitem) (SendMessage(hwnd, LVM_SETITEM,0, pitem))

% DS_CENTER=0x0800
% DS_MODALFRAME=0x80
% SS_CENTERIMAGE=0x200
% LVS_LIST  0x0003
% LVS_REPORT  0x0001
% LVS_EX_GRIDLINES 1
% LVS_EX_CHECKBOXES 4
% LVS_EX_FULLROWSELECT  0x0020
% LVSCW_AUTOSIZE  -1
% LVSCW_AUTOSIZE_USEHEADER  -2
% LVM_INSERTCOLUMN=4123
% LVM_SETCOLUMNWIDTH=4126
% LVM_INSERTITEM=4103
% LVM_SETITEM=4102
% LVCF_FMT 1
% LVCF_WIDTH 2
% LVCF_TEXT=4
% LVCF_SUBITEM 8
% LVIF_TEXT=1
% LVM_SETEXTENDEDLISTVIEWSTYLE 0x1036
% LVN_COLUMNCLICK = -108
% LVN_ITEMCHANGED = -101
% LR_LOADFROMFILE=0x0010
% IMAGE_ICON=1
% STM_SETIMAGE=0x172
% SWP_NOZORDER=4

type LVCOLUMN
  uint  mask
  int   fmt
  int   cx
  char* pszText
  int   cchTextMax
  int   iSubItem
  int   iImage
  int   iOrder
  int   cxMin
  int   cxDefault
  int   cxIdeal 
end type
typedef LVCOLUMN LV_COLUMN

type LVITEM 
  uint   mask
  int    iItem
  int    iSubItem
  uint   state
  uint   stateMask
  char*  pszText
  int    cchTextMax
  int    iImage       // index of the list view item's icon
  sys    lParam       // 32-bit value to associate with item
  int    iIndent
  int    iGroupId
  uint   cColumns
  uint   *puColumns
  int    *piColFmt
  int    iGroup
end type
typedef LVITEM LV_ITEM

' Number of rows in the ListView
   % LVROWS = 200
'  Number of columns in the ListView  meaning 3 +1 = 4 columns
   % LVCOLS = 3         


! GetDlgItem lib "user32.dll" (sys hDlg, int nIDDlgItem) as sys
! IsDialogMessage lib "user32.dll" alias"IsDialogMessageA" (sys hDlg, sys lpMsg) as bool
! IsWindow lib "user32.dll" (sys hWnd) as bool



   '  Handle for the Main Dialog
     sys hDlg

    init_common_controls()





'====================================================================
'   Main callback function
    Function DlgProc( hDlg,uint uMsg, sys wParam, lParam ) as sys callback

    int i , j
    string   txtStr

  '  Handle for the ListView
    sys hListview = GetDlgItem(hDlg, IDC_LSV1) 
   


     LV_COLUMN    lvc
     LV_ITEM        lvi


  select case uMsg

     case WM_INITDIALOG
      ' display the program icon
            sys  hInstance = GetModuleHandle(NULL)
            sys hIcon = LoadIcon(hInstance, IDI_Logo)
           'Set Icon to Main Window
            SendMessage(hDlg, WM_SETICON, ICON_BIG, hIcon)

       
        'Setup the  ListView Column Headers
      '        The  first column
       '        lvc.mask =    LVCF_WIDTH   
           '    lvc.cx      = 30
        '      txtStr="Column #" & str(1)
       '        lvc.pszText = txtStr   
         '      lvc.iorder = 0       
         '     ListView_InsertColumn(hListview, 0, &lvC)

     '   All the columns
        For i = 0 To LVCOLS -1
              lvc.mask = LVCF_FMT OR   LVCF_WIDTH   OR  LVCF_TEXT  OR LVCF_SUBITEM
               lvc.cx      = 25
              txtStr="Column #" & str(i+1)
               lvc.pszText = txtStr   
               lvc.iorder = i         
              ListView_InsertColumn(hListview, i, &lvC)
        Next i


        ' Setup the Listview  data Rows
        For i=1 To LVROWS
              'First column
              lvi.mask      =  LVIF_TEXT
              txtStr = "Row #" & str(200-i+1) ", Col # 1"
             lvi.pszText   = txtStr
             lvi.iSubItem  =  0
             ListView_InsertItem(hListview, &lvi)

           'Remaining columns
           for j=2 to LVCOLS
                 txtStr = "Row #" & str(200-i+1) ", Col # " & str(j)
                lvi.pszText   = txtStr
                lvi.iSubItem  =  j-1
                ListView_SetItem(hListview, &lvi)
           next j
        Next i

        for i = 0 to LVCOLS
              ListView_SetColumnWidth(hListview,i,LVSCW_AUTOSIZE_USEHEADER)
        next i
       
        SendMessage(hListview, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, 
                       LVS_EX_FULLROWSELECT| LVS_EX_CHECKBOXES or LVS_EX_GRIDLINES)

         


    case WM_COMMAND

           select case loword(wParam)
                   case IDCANCEL   
         '            exit   
                     DestroyWindow( hDlg )
              end select

    case WM_NOTIFY
              NMHDR pnm at lParam
       
                if pnm.hwndFrom = hListview then
                           'ListView         
                         select case pnm.code
                         case LVN_COLUMNCLICK
                                    mbox "LVN_COLUMNCLICK"   
                       
                            case LVN_ITEMCHANGED
                 end select
       end if
 

      case WM_SIZE     
               RECT rcClient
         // Calculate remaining height and size edit
              GetClientRect(hDlg, &rcClient)
              SetWindowPos(hListview, NULL, 0, rcClient.top, rcClient.right, rcClient.bottom, SWP_NOZORDER)

    case WM_CLOSE
              DestroyWindow( hDlg )

    case WM_DESTROY
              PostQuitMessage( null )

  end select

  return 0
end function



'====================================================================
'  Display the Main Dialog
Function  DispMainDialog

         sys lpdt
         MSG wMsg

         dyn::init(lpdt)
   
       Dialog( 1, 10,10,280,150, "Listview example 64bits ", lpdt,
                                   WS_OVERLAPPEDWINDOW or DS_CENTER or WS_VISIBLE,
                                    8,"MS Sans Serif" )

        CONTROL "",IDC_LSV1,"SysListView32", _
                             WS_CHILDWINDOW  |   WS_VISIBLE   |  WS_TABSTOP|WS_BORDER  |  LVS_REPORT, _
                             3,3,273,141,   WS_EX_CLIENTEDGE

        hDlg = CreateModelessDialog( 0, @DlgProc, 0, lpdt )

       while GetMessage( @wMsg, null, 0, 0 ) <> 0
              if IsDialogMessage( hDlg,  @wMsg ) = 0 then
                            TranslateMessage( @wMsg )
                            DispatchMessage( @wMsg )
               end if
          wend
End Function



'------------------------------------
'  Start of program
   DispMainDialog













Chris Chancellor

here is the complete program in a zip file

thanxx in advance for all help in solving this problem

what i need is to have a wider column for column 1  to accomodate the checkbox
and the rest of the columns have a smaller width


Chris Chancellor

This is the screen display

as display the column # 1  is too narrow and data is not fully display  bcos of the additional checkbox



José Roca

Why are you using lvc.iorder = i? You must use lvc.iSubItem = i.

Chris Chancellor

Thanxx Jose

but i got it resolved by adding some blanks behind the Column #1 header string
thus making the column width wider


       'Setup the  ListView Column Headers
      '        The  first column must have a wider width to accomodate the checkbox
               lvc.mask =    LVCF_WIDTH  or  LVCF_ORDER
               lvc.cx      = 35
          '    Need to add some blanks behind the header string label
         '    inorder to get a wider column
               txtStr="Column #" & str(1) +  "                      "
              lvc.pszText = txtStr   
              lvc.iorder = 0     
          '      lvc.iSubItem = 0   
             ListView_InsertColumn(hListview, 0, &lvc)

     '   All the other columns to have a narrower width
        For i = 1  To  LVCOLS -1
              lvc.mask = LVCF_FMT OR   LVCF_WIDTH   OR  LVCF_TEXT  OR LVCF_SUBITEM
               lvc.cx      = 25
              txtStr="Column #" & str(i+1)
               lvc.pszText = txtStr   
               lvc.iorder = i 
                ListView_InsertColumn(hListview, i, &lvc)
        Next i






thus the final amended code is

'====================================================================
' Listview example, nested modeless dialog.  modified Nov 2 2018
'====================================================================
$ filename "ListView_64M.exe"
use rtl64


uses dialogs


'Identifiers
#define IDD_DLG1 2000
#define IDC_LSV1  2001


'  The program logo icon  is obtained from the resource file
'  the 1000 must corespondence to the 1000 in the rc file
   #define IDI_LOGO     1000
   % ICON_BIG=1
   % WM_SETICON=0x80


macro ListView_InsertColumn(hwnd,iCol,pcol) (SendMessage(hwnd, LVM_INSERTCOLUMN, iCol, pcol))
macro ListView_SetColumnWidth(hwnd,iCol,cx) (SendMessage(hwnd, LVM_SETCOLUMNWIDTH, iCol, cx))
macro ListView_InsertItem(hwnd,pitem) (SendMessage(hwnd, LVM_INSERTITEM,0, pitem))
macro ListView_SetItem(hwnd,pitem) (SendMessage(hwnd, LVM_SETITEM,0, pitem))

% DS_CENTER=0x0800
% DS_MODALFRAME=0x80
% SS_CENTERIMAGE=0x200
% LVS_LIST  0x0003
% LVS_REPORT  0x0001
% LVS_EX_GRIDLINES 1
% LVS_EX_CHECKBOXES 4
% LVS_EX_FULLROWSELECT  0x0020
% LVSCW_AUTOSIZE  -1
% LVSCW_AUTOSIZE_USEHEADER  -2
% LVM_INSERTCOLUMN=4123
% LVM_SETCOLUMNWIDTH=4126
% LVM_INSERTITEM=4103
% LVM_SETITEM=4102
% LVCF_FMT 1
% LVCF_WIDTH 2
% LVCF_TEXT=4
% LVCF_SUBITEM 8
% LVCF_ORDER = 20
% LVIF_TEXT=1
% LVM_SETEXTENDEDLISTVIEWSTYLE 0x1036
% LVN_COLUMNCLICK = -108
% LVN_ITEMCHANGED = -101
% LR_LOADFROMFILE=0x0010
% IMAGE_ICON=1
% STM_SETIMAGE=0x172
% SWP_NOZORDER=4

type LVCOLUMN
  uint  mask
  int   fmt
  int   cx
  char* pszText
  int   cchTextMax
  int   iSubItem
  int   iImage
  int   iOrder
  int   cxMin
  int   cxDefault
  int   cxIdeal 
end type
typedef LVCOLUMN LV_COLUMN

type LVITEM 
  uint   mask
  int    iItem
  int    iSubItem
  uint   state
  uint   stateMask
  char*  pszText
  int    cchTextMax
  int    iImage       // index of the list view item's icon
  sys    lParam       // 32-bit value to associate with item
  int    iIndent
  int    iGroupId
  uint   cColumns
  uint   *puColumns
  int    *piColFmt
  int    iGroup
end type
typedef LVITEM LV_ITEM

' Number of rows in the ListView
   % LVROWS = 200
'  Number of columns in the ListView  meaning 3 +1 = 4 columns
   % LVCOLS = 3         


! GetDlgItem lib "user32.dll" (sys hDlg, int nIDDlgItem) as sys
! IsDialogMessage lib "user32.dll" alias"IsDialogMessageA" (sys hDlg, sys lpMsg) as bool
! IsWindow lib "user32.dll" (sys hWnd) as bool



   '  Handle for the Main Dialog
     sys hDlg

    init_common_controls()





'====================================================================
'   Main callback function
    Function DlgProc( hDlg,uint uMsg, sys wParam, lParam ) as sys callback

    int i , j
    string   txtStr

  '  Handle for the ListView
    sys hListview = GetDlgItem(hDlg, IDC_LSV1) 
   


     LV_COLUMN    lvc
     LV_ITEM        lvi


  select case uMsg

     case WM_INITDIALOG
      ' display the program icon
            sys  hInstance = GetModuleHandle(NULL)
            sys hIcon = LoadIcon(hInstance, IDI_Logo)
           'Set Icon to Main Window
            SendMessage(hDlg, WM_SETICON, ICON_BIG, hIcon)

       
        'Setup the  ListView Column Headers
      '        The  first column must have a wider width to accomodate the checkbox
               lvc.mask =    LVCF_WIDTH  or  LVCF_ORDER
               lvc.cx      = 35
          '    Need to add some blanks behind the header string label
         '    inorder to get a wider column
               txtStr="Column #" & str(1) +  "                      "
              lvc.pszText = txtStr   
              lvc.iorder = 0     
          '      lvc.iSubItem = 0   
             ListView_InsertColumn(hListview, 0, &lvc)

     '   All the other columns to have a narrower width
        For i = 1  To  LVCOLS -1
              lvc.mask = LVCF_FMT OR   LVCF_WIDTH   OR  LVCF_TEXT  OR LVCF_SUBITEM
               lvc.cx      = 25
              txtStr="Column #" & str(i+1)
               lvc.pszText = txtStr   
               lvc.iorder = i 
                ListView_InsertColumn(hListview, i, &lvc)
        Next i


        ' Setup the Listview  data Rows
        For i=1 To LVROWS
              'First column
              lvi.mask      =  LVIF_TEXT
              txtStr = "Row #" & str(200-i+1) ", Col # 1"
             lvi.pszText   = txtStr
             lvi.iSubItem  =  0
             ListView_InsertItem(hListview, &lvi)

           'Remaining columns
           for j=2 to LVCOLS
                 txtStr = "Row #" & str(200-i+1) ", Col # " & str(j)
                lvi.pszText   = txtStr
                lvi.iSubItem  =  j-1
                ListView_SetItem(hListview, &lvi)
           next j
        Next i

        for i = 0 to LVCOLS
              ListView_SetColumnWidth(hListview,i,LVSCW_AUTOSIZE_USEHEADER)
        next i
       
        SendMessage(hListview, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, 
                       LVS_EX_FULLROWSELECT| LVS_EX_CHECKBOXES or LVS_EX_GRIDLINES)

         


    case WM_COMMAND

           select case loword(wParam)
                   case IDCANCEL   
         '            exit   
                     DestroyWindow( hDlg )
              end select

    case WM_NOTIFY
              NMHDR pnm at lParam
       
                if pnm.hwndFrom = hListview then
                           'ListView         
                         select case pnm.code
                         case LVN_COLUMNCLICK
                                    mbox "LVN_COLUMNCLICK"   
                       
                            case LVN_ITEMCHANGED
                 end select
       end if
 

      case WM_SIZE     
               RECT rcClient
         // Calculate remaining height and size edit
              GetClientRect(hDlg, &rcClient)
              SetWindowPos(hListview, NULL, 0, rcClient.top, rcClient.right, rcClient.bottom, SWP_NOZORDER)

    case WM_CLOSE
              DestroyWindow( hDlg )

    case WM_DESTROY
              PostQuitMessage( null )

  end select

  return 0
end function



'====================================================================
'  Display the Main Dialog
Function  DispMainDialog

         sys lpdt
         MSG wMsg

         dyn::init(lpdt)
   
       Dialog( 1, 10,10,280,150, "Listview example 64bits ", lpdt,
                                   WS_OVERLAPPEDWINDOW or DS_CENTER or WS_VISIBLE,
                                    8,"MS Sans Serif" )

        CONTROL "",IDC_LSV1,"SysListView32", _
                             WS_CHILDWINDOW  |   WS_VISIBLE   |  WS_TABSTOP|WS_BORDER  |  LVS_REPORT, _
                             3,3,273,141,   WS_EX_CLIENTEDGE

        hDlg = CreateModelessDialog( 0, @DlgProc, 0, lpdt )

       while GetMessage( @wMsg, null, 0, 0 ) <> 0
              if IsDialogMessage( hDlg,  @wMsg ) = 0 then
                            TranslateMessage( @wMsg )
                            DispatchMessage( @wMsg )
               end if
          wend
End Function



'------------------------------------
'  Start of program
   DispMainDialog








Chris Chancellor

and here is the new screen display of the listview

Hope that other users or forum lurkers can see how versatile is O2
and able to use it for its power  :)


José Roca

> but i got it resolved by adding some blanks behind the Column #1 header string thus making the column width wider

This is a bad practice.

Chris Chancellor

yeah,  but i'm only a beginner , doing this way is just a bandaid and i don't have the expertize to enhance it

maybe you can help by enhancing it

Chris Chancellor


maybe another way to change the column widths is to do this (after all listview data is setup)


  ListView_SetColumnWidth(hListview, 1, 35)
   for i=1 to 2
      ListView_SetColumnWidth(hListview, i, 25)
   next