How to set a minimum column width


Again we have to override the OnNotify() function. The code below restricts the columns to a minimum width of 80 pixels. The logic can be extended to restrict the width within a range or even specify different range for different columns. To extend the functionality, you would need to add member variables to the class to track the different settings.
BOOL CMyListCtrl::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) 
{
    HD_NOTIFY   *pHDN = (HD_NOTIFY*)lParam;

        if((pHDN->hdr.code == HDN_ITEMCHANGINGW || pHDN->hdr.code == HDN_ITEMCHANGINGA) 
                && pHDN->pitem->cxy < 80)
        {
                *pResult = TRUE;                // disable change
                return TRUE;                    // Processed message
        }
 
        return CListCtrl::OnNotify(wParam, lParam, pResult);
}
 



Comments

  • Fix when dragging fast

    Posted by Legacy on 11/30/1998 12:00am

    Originally posted by: Cees Mechielsen

    The solutions given up till this point all have the disadvantage that
    
    the end point is missed when you drag faster than the system samples
    the mouse position.
    This can easily be fixed. Just replace:

    *pResult = TRUE; // disable change
    return TRUE; // Processed message

    with:

    pHDN->pitem->cxy = 80;

    In the examples.

    The entire code now looks like this:

    if( ( pHDN->hdr.code == HDN_ITEMCHANGINGW ||
    pHDN->hdr.code == HDN_ITEMCHANGINGA ||
    pHDN->hdr.code == HDN_ENDTRACK)
    &&
    ( pHDN->pitem->mask & HDI_WIDTH &&
    pHDN->pitem->cxy < 80 &&
    pHDN->pitem->cxy >= 0 )
    )
    {
    pHDN->pitem->cxy = 80; // Set the column width
    }

    return CListView::OnNotify(wParam, lParam, pResult);

    Have Fun!
    Cees.

    Reply
  • Column resizing when "Show window contents while dragging" is not checked

    Posted by Legacy on 11/27/1998 12:00am

    Originally posted by: Grant Gussie

    In the control panel Display|Plus! is an item "Show window contents while dragging". If this item
    is not checked, the HDN_ITEMCHANGING messages are suppressed, and the code listed above will not work. What
    is sent, however, is a HDN_ENDTRACK message after the dragging is finished.
    
    

    I would recommend that the code be modified so that the HDN_ENDTRACK messages are trapped along with the HDN_ITEMCHANGING messages. For example, look at the code below, which also encorporates Michael Easter's suggestion of examining the mask.

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

    if(( pHDN->hdr.code == HDN_ITEMCHANGING || pHDN->hdr.code == HDN_ENDTRACK) && (pHDN->pitem->mask & HDI_WIDTH && pHDN->pitem->cxy < 80))
    {
    *pResult = TRUE;
    return TRUE;
    }

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


    Reply
  • Fix for column headers

    Posted by Legacy on 11/21/1998 12:00am

    Originally posted by: Michael Easter

    Zafir's article How to force a minimum column width describes an easy
    
    method to set and enforce a minimum column width. However, the method
    can prevent non-resizing changes to the column header to not take
    effect. For example, adding images or sort indicators to the column
    header.

    A small change to Zafir's code fixes the problem. Add a check to see
    if the change is related to column size.

    pHDN->pitem->mask & HDI_WIDTH

    The entire function with the change added is below. See Zafir's
    article for a complete description.


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

    if ((pHDN->hdr.code == HDN_ITEMCHANGINGW ||
    pHDN->hdr.code == HDN_ITEMCHANGINGA) &&
    pHDN->pitem->mask & HDI_WIDTH && pHDN->pitem->cxy < 80)
    {
    *pResult = TRUE;
    return TRUE;
    }

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

    Reply
  • Slight modification for owner-drawn column headers

    Posted by Legacy on 10/28/1998 12:00am

    Originally posted by: Chip Calvert

    This is an excellent article.  Thanks for writing it.  It was a big help to me.  However, when I implemented
    it, I ran into a problem.  The column headers in my CListCtrl were ownerdrawn.	The if statement below was
    catching the calls for redrawing in addition to those for resizing.
    
    

    if((pHDN->hdr.code == HDN_ITEMCHANGINGW ||
    pHDN->hdr.code == HDN_ITEMCHANGINGA)
    && pHDN->pitem->cxy < 80)

    I would recommend changing it to:

    if((pHDN->hdr.code == HDN_ITEMCHANGINGW ||
    pHDN->hdr.code == HDN_ITEMCHANGINGA) &&
    (pHDN->pitem->cxy < 80 && pHDN->pitem->cxy >= 0))

    From my testing, it appears that the value of pHDN->pitem->cxy is a negative number when the call is for redrawing and not resizing.

    Reply
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 …

  • Businesses are moving more and more of their customer transactions to the web. Security is understandably a top concern as online transactions increase, so it is important to make sure your electronic signature provider meets the highest security standards. That means more than simply passing a security audit or obtaining a certification. This white paper provides recommendations for taking a broader view of e-signature security, and answers key questions that help identify the security requirements against …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds