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: September 10, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild". This loop of continuous delivery and continuous feedback is …

  • This ESG study by Mark Peters evaluated a common industry-standard disk VTl deduplication system (with 15:1 reduction ratio) versus a tape library with LTO-5, drives with full nightly backups, over a five-year period.  The scenarios included replicated systems and offsite tape vaults.  In all circumstances, the TCO for VTL with deduplication ranged from about 2 to 4 times more expensive than the LTO-5 tape library TCO. The paper shares recent ESG research and lots more. 

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds