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

  • Today's competitive marketplace requires the organization to frequently release and deploy applications at the pace of user demands, with reduced cost, risk, and increased quality. This book defines the basics of application release and deployment, and provides best practices for implementation with resources for a deeper dive. Inside you will find: The business and technical drivers behind automated application release and deployment. Evaluation guides for application release and deployment solutions. …

  • Download the Information Governance Survey Benchmark Report to gain insights that can help you further establish business value in your Records and Information Management (RIM) program and across your entire organization. Discover how your peers in the industry are dealing with this evolving information lifecycle management environment and uncover key insights such as: 87% of organizations surveyed have a RIM program in place 8% measure compliance 64% cannot get employees to "let go" of information for …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds