locked
How to prevent resizing of column in CListCtrl

    Question

  • I am working on a CDHTMLDialog based application where I ahve added a CListCtrl as shown below.

    The list control has only one column.

    IDD_TEST_DIALOG DIALOGEX 0, 0, 419, 286
    STYLE DS_SETFONT | DS_FIXEDSYS | DS_CENTER | WS_MINIMIZEBOX | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME
    EXSTYLE WS_EX_APPWINDOW
    FONT 8, "MS Shell Dlg", 0, 0, 0x1
    BEGIN
        CONTROL         "",IDC_LIST_TEXT,"SysListView32",LVS_REPORT | LVS_OWNERDATA | LVS_NOSORTHEADER | NOT WS_VISIBLE | WS_BORDER | WS_TABSTOP,20,143,397,136
    END

    The problem is that I can drag the colum header to extreme left and the column goes hidden. To bring it back I will have to again drag the column header to the right.

    I want to prevent resizing of this column so that the user cannot drag this completely to the left and make it hidden.

    I did try the following code but it is not working.

    void CMyTestDlg::OnHDNEndtrackListLog(NMHDR *pNMHDR, LRESULT *pResult)
    {
    LPNMHEADER phdr = reinterpret_cast<LPNMHEADER>(pNMHDR);
    // TODO: Add your control notification handler code here
    if(m_ListCtrl.GetColumnWidth(0) < 100)
    {
    m_ListCtrl.SetColumnWidth(0, 500);
    }

    *pResult = 0;
    }

    Since I have very less time to implement this, I request you kindly give me some examples from MFC Dialog based applications.

    Wednesday, February 08, 2012 8:19 AM

Answers

  • The following modification worked. Thanks.

     LPNMHEADER phdr = reinterpret_cast<LPNMHEADER>(pNMHDR);

      // TODO: Add your control notification handler code here
      TRACE(_T("%d\n"),phdr->iItem);
      
      if (m_ListCtrl.GetColumnWidth(0) <= 200)
      {
        phdr->pitem->cxy=300;
      }
      
      *pResult = 0;


    • Edited by lucky shah Thursday, February 09, 2012 7:49 PM
    • Marked as answer by Rob Pan Monday, February 20, 2012 9:01 AM
    Thursday, February 09, 2012 7:49 PM

All replies

  • Preventing the resizing of the columns  should be able checking the HDN_ITEMCHANGING message.

    When you create a MFC event function for HDN_ITEMCHANGING and you use i.e. this code you would prevent all columns of changing the width of 100:

     LPNMHEADER phdr = reinterpret_cast<LPNMHEADER>(pNMHDR);
      // TODO: Add your control notification handler code here
      TRACE(_T("%d\n"),phdr->iItem);
      
      if ((phdr->pitem->mask & HDI_WIDTH) !=0)
      {
        phdr->pitem->cxy=100;
      }
      
      *pResult = 0;
    If you also check "phdr->iItem" you can handle this for each column in a different way.
    Wednesday, February 08, 2012 9:54 AM
  • IF you are using Window Vista and version 6.0 you can use LVCFMT_FIXED_WIDTH as extended style and it will work for you.

    According to MSDN  LVCFMT_FIXED_WIDTH Version 6.00 and Windows Vista. Can't resize the column; same as HDF_FIXEDWIDTH.

    Or

    The header control in the ListView control sends notification to the parent window (e.i. the ListView) before it begins resizing a column. We can override the OnNotify() function in the CListCtrl derived class to handle this notification.

    switch (((NMHDR*)lParam)->code)
            {
                    case HDN_BEGINTRACKW:
                    case HDN_BEGINTRACKA:
                            *pResult = TRUE;                                      

                           return TRUE;         
            }
     
            return CListCtrl::OnNotify(wParam, lParam, pResult);

    Thanks


    Rupesh Shukla

    Wednesday, February 08, 2012 3:53 PM
  • Hi Bordon, thanks for the code. Your code fixes the column width to 100. 

    The List Control can have variable length strings and hence when I click on the column divider the column should expand to the right so that the complete string is visible.

    I need some help on preventing user from dragging/resizing the column header to the left beyond some point. say the column name is "Transaction Details", which occupies around 100 pixels, the user should not be allowed to resize lesser than 100. 

    Wednesday, February 08, 2012 7:22 PM
  • The following modification worked. Thanks.

     LPNMHEADER phdr = reinterpret_cast<LPNMHEADER>(pNMHDR);

      // TODO: Add your control notification handler code here
      TRACE(_T("%d\n"),phdr->iItem);
      
      if (m_ListCtrl.GetColumnWidth(0) <= 200)
      {
        phdr->pitem->cxy=300;
      }
      
      *pResult = 0;


    • Edited by lucky shah Thursday, February 09, 2012 7:49 PM
    • Marked as answer by Rob Pan Monday, February 20, 2012 9:01 AM
    Thursday, February 09, 2012 7:49 PM