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: August 20, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT When you look at natural user interfaces as a developer, it isn't just fun and games. There are some very serious, real-world usage models of how things can help make the world a better place – things like Intel® RealSense™ technology. Check out this upcoming eSeminar and join the panel of experts, both from inside and outside of Intel, as they discuss how natural user interfaces will likely be getting adopted in a wide variety …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds