Prevent column resizing


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. The code below prevents resizing of all columns. Note that the resize cursor still shows up.
BOOL CMyListCtrl::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) 
{
        switch (((NMHDR*)lParam)->code)
        {
                case HDN_BEGINTRACKW:
                case HDN_BEGINTRACKA:
                        *pResult = TRUE;                // disable tracking
                        return TRUE;                    // Processed message
        }
 
        return CListCtrl::OnNotify(wParam, lParam, pResult);
}
If you want to prevent resizing of only one column, you should check for the value in iItem field of the HD_NOTIFY structure. The code below stops only the first column from being resized.
 
BOOL CMyListCtrl::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) 
{
        HD_NOTIFY   *pHDN = (HD_NOTIFY*)lParam;

        if((pHDN->hdr.code == HDN_BEGINTRACKW || pHDN->hdr.code == HDN_BEGINTRACKA)
                && pHDN->iItem == 0)            // Prevent only first (col# 0) from resizing
        {
                *pResult = TRUE;                // disable tracking
                return TRUE;                    // Processed message
        }
 
        return CListCtrl::OnNotify(wParam, lParam, pResult);
}
 



Comments

  • Its is too helpful. we can do our task without knowing user.

    Posted by Yogesh Chaudhari on 07/31/2015 06:08am

    Its is too helpful. we can do our task without knowing user. Thank You.

    Reply
  • Help with Implementation

    Posted by GeekyGirl on 08/20/2009 03:17pm

    I'm a newbie with MFC.  Here's my function.  I modified your code a little:
    
    //Here's your function:
    BOOL ListCtrlEx::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
    {
    	HD_NOTIFY *pHDN = (HD_NOTIFY*)lParam;
    	if( (pHDN->hdr.code == HDN_BEGINTRACKW || pHDN->hdr.code == HDN_BEGINTRACKA)  )
    	{
    		if(GetColumnWidth(pHDN->iItem) == 0) // when the size of the column is 0, we want to hide
    		{          						     // this column and prevent the resizing of the column
    			*pResult = TRUE;  // disable tracking
    			return TRUE;
    		}
    		else
    		{
    			return FALSE;
    		}
    	}
    	return CListCtrl::OnNotify(wParam, lParam, pResult);
    }
    
    //And my function:
    void ListCtrlEx::SizeColumnsToText()
    {
    	const int COL_SEPARATION = 8;  // extra pixels to account for list control grid
    	int MINCOLWIDTH = 10;
    	int nCol = 0;	// Column iterator
    	int MaxColWidth = 0;  //initializing at zero
    
    	BOOL bSubImage = FALSE;
    	SetRedraw(FALSE);
    	for (nCol; nCol < m_nColumnCount; nCol++)
    	{
    		//int temp = GetColumnWidth( nCol );
    		if (int temp = GetColumnWidth( nCol ) == 0)
    			ListCtrlEx::OnNotify( wParam, lParam, pResult );
    			//SetColumnWidth( nCol, 0 );
    		else
    		{
    			int ColWidth1 = GetColumnWidth( SetColumnWidth( nCol, LVSCW_AUTOSIZE_USEHEADER ) );
    			int ColWidth2 = GetColumnWidth( SetColumnWidth( nCol, LVSCW_AUTOSIZE ) );
    			if( ColWidth1 > MINCOLWIDTH || ColWidth2 > MINCOLWIDTH )
    				MaxColWidth = max( ColWidth1, ColWidth2 );
    			else
    				MaxColWidth = MINCOLWIDTH;
    			if (bSubImage = TRUE)
    				SetColumnWidth( nCol, MaxColWidth + COL_SEPARATION + 15 );
    			else
    				SetColumnWidth( nCol, MaxColWidth );
    		}
    	}
    	SetRedraw(TRUE);
    }
    
    I'm not sure how to implement this.  Please help.
    
    Thanks!

    Reply
  • It's work!!!!.

    Posted by Legacy on 09/28/2003 12:00am

    Originally posted by: Joe

    Thank you, for your article. It have a good result in my application.

    Reply
  • Here's how to set individual column resizing preferences

    Posted by Legacy on 08/06/2003 12:00am

    Originally posted by: Gregory J. Spiers

    Sometimes it would be handy to specify any combination of
    
    columns that are not to be resized. To accomplish that
    with the solution described in the main topic, we only
    need to have a member variable as part of our derived
    class that will hold a bit for each column indicating
    whether or not it can be resized.

    NOTE THAT WE CAN ONLY APPLY INDIVIDUAL RESIZING TO COLUMNS
    THAT MATCH BITS IN OUR MEMBER VARIABLE MASK
    (a DWORD can represent 32 columns, a BYTE can represent 8).

    Turn bits on for columns not to be resized, or off for
    columns that will resize. The rightmost bit represents
    column 0 and the leftmost bit is the largest column
    number we can control.

    Handling the column resize calls works like this:

    BOOL CMyListCtrl::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
    {
    HD_NOTIFY *pHDN = (HD_NOTIFY*)lParam;

    switch (((NMHDR*)lParam)->code)
    {
    // Is this a resize request?
    case HDN_BEGINTRACKW: // drag column to resize
    case HDN_BEGINTRACKA:
    case (HDN_DIVIDERDBLCLICKA): // double click column to resize
    case (HDN_DIVIDERDBLCLICKW):

    // Set mask to identify coumn
    DWORD dwMaskedItem = 0L; // Set to 0 before we apply the mask

    // Ensure that the column specified is less than the bits of a DWORD
    if (pHDN->iItem < 32)
    {
    // and set the value of the bit corresponding to the specified column
    dwMaskedItem = (0x01 << pHDN->iItem);
    }

    // Do not resize any column without permission
    dwMaskedItem &= m_dwNoSizeCols; // AND with member variable holding the column mask
    if (dwMaskedItem != 0L)
    { // Must be a non-resizing column, so we..
    *pResult = TRUE; // Tell our program that we processed the message and..
    return TRUE; // return
    }

    break;
    }

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

    Reply
  • How to do it in win32 applicatio?

    Posted by Legacy on 08/04/2003 12:00am

    Originally posted by: vidya

    It worked fine. Really good. But how the same thing can be done in win32 application?

    Reply
  • It was too good

    Posted by Legacy on 06/02/2003 12:00am

    Originally posted by: Kapil Arya

    It was too good, I was searching for it, and found on the right time, Thankx for Ur effort, Keep Writing these type of code

    Reply
  • How do i do this in win32 api ?

    Posted by Legacy on 05/01/2003 12:00am

    Originally posted by: Wiseguy

    Im having a litle problem subclassing the ListView. The example above is for mfc but how do i do this in win32. Please can any ine help?

    Reply
  • how to do this in ATL

    Posted by Legacy on 03/05/2003 12:00am

    Originally posted by: venkatesh


    Colud you tell me how to restrict resizing the column header if the list control is added on a ATL dialog.

    Reply
  • HDN_BEGINTRACKW ?, HDN_ENDTRACKA ?

    Posted by Legacy on 08/26/2002 12:00am

    Originally posted by: cromianha

    What ? ->
    
    HDN_BEGINTRACKW ?, HDN_ENDTRACKA ?

    Reply
  • same problem of shinoj

    Posted by Legacy on 11/06/2001 12:00am

    Originally posted by: aisha

    I do have the same problem as of shinoj, saving the column widths, can anybody help??

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Today's enterprise datacenter can be one of the most complex business environments with dozens (for smaller business), hundreds (for larger and midsize business), even thousands (for hyperscale businesses) of servers that must be managed and monitored. At this level, just managing the cords can be challenging — let alone keeping up with the growing need for more agility and scalability within the datacenter. Simply put, companies are aggressively looking for less complexity and more agility from their …

  • Entire organizations suffer when their networks can't keep up and new opportunities are put on hold. Waiting on service providers isn't good business. In these examples, learn how to simplify network management so that your organization can better manage costs, adapt quickly to business demands, and seize market opportunities when they arise.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date