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

  • 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
  • Side affect of this addition

    Posted by Legacy on 07/05/2001 12:00am

    Originally posted by: Girish

    Notify message is sent always to the parent window when you add this piece of code in the Message notification and if you are trying to add some string to a edit control on the NM_CLICK event of your list control the string to the edit control will not be added

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • The first phase of API management was about realizing the business value of APIs. This next wave of API management enables the hyper-connected enterprise to drive and scale their businesses as API models become more complex and sophisticated. Today, real world product launches begin with an API program and strategy in mind. This API-first approach to development will only continue to increase, driven by an increasingly interconnected web of devices, organizations, and people. To support this rapid growth, …

  • As more and more organizations migrate to the cloud, many are faced with hidden costs emerging from unexpected places. Two non-obvious and non-trivial factors can drive up costs. First are separate charges for everything from server memory to intrusion detection. Second are the high personnel costs for early-generation, manually operated clouds. These costs can rack up quickly, creating total cost of ownership (TCO) surprises. Keeping TCO low in the cloud is essentially a matter of management strategy. IT …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds