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

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: November 13, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT APIs can be a great source of competitive advantage. The practice of exposing backend services as APIs has become pervasive, however their use varies widely across companies and industries. Some companies leverage APIs to create internal, operational and development efficiencies, while others use them to drive ancillary revenue channels. Many companies successfully support both public and private programs from the same API by varying levels …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds