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

  • When individual departments procure cloud service for their own use, they usually don't consider the hazardous organization-wide implications. Read this paper to learn best practices for setting up an internal, IT-based cloud brokerage function that service the entire organization. Find out how this approach enables you to retain top-down visibility and control of network security and manage the impact of cloud traffic on your WAN.

  • 2015 is shaping up to include some of the best innovation the mobile industry has ever experienced. There will be new discoveries, new technologies, and existing giants in the vendor space will test the limits to increase productivity while ensuring the confidentiality, integrity, and availability of data. While all of this innovation is taking place, enterprises must continue to remain vigilant as cybercriminals continue to look for weaknesses to steal company data, and intellectual property by infiltrating …

Most Popular Programming Stories

More for Developers

RSS Feeds

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