Prevent column resizing
Posted
by Zafir Anjum
on August 6th, 1998
The header control in the ListView control sends notification to the parent window (e.i. the ListView) before it begins resizing a column. We can override the OnNotify() function in the CListCtrl derived class to handle this notification. The code below prevents resizing of all columns. Note that the resize cursor still shows up.
BOOL CMyListCtrl::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
switch (((NMHDR*)lParam)->code)
{
case HDN_BEGINTRACKW:
case HDN_BEGINTRACKA:
*pResult = TRUE; // disable tracking
return TRUE; // Processed message
}
return CListCtrl::OnNotify(wParam, lParam, pResult);
}
If you want to prevent resizing of only one column, you should check for
the value in iItem field of the HD_NOTIFY structure. The code below stops
only the first column from being resized.
BOOL CMyListCtrl::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
HD_NOTIFY *pHDN = (HD_NOTIFY*)lParam;
if((pHDN->hdr.code == HDN_BEGINTRACKW || pHDN->hdr.code == HDN_BEGINTRACKA)
&& pHDN->iItem == 0) // Prevent only first (col# 0) from resizing
{
*pResult = TRUE; // disable tracking
return TRUE; // Processed message
}
return CListCtrl::OnNotify(wParam, lParam, pResult);
}

Comments
Help with Implementation
Posted by GeekyGirl on 08/20/2009 03:17pmI'm a newbie with MFC. Here's my function. I modified your code a little: //Here's your function: BOOL ListCtrlEx::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) { HD_NOTIFY *pHDN = (HD_NOTIFY*)lParam; if( (pHDN->hdr.code == HDN_BEGINTRACKW || pHDN->hdr.code == HDN_BEGINTRACKA) ) { if(GetColumnWidth(pHDN->iItem) == 0) // when the size of the column is 0, we want to hide { // this column and prevent the resizing of the column *pResult = TRUE; // disable tracking return TRUE; } else { return FALSE; } } return CListCtrl::OnNotify(wParam, lParam, pResult); } //And my function: void ListCtrlEx::SizeColumnsToText() { const int COL_SEPARATION = 8; // extra pixels to account for list control grid int MINCOLWIDTH = 10; int nCol = 0; // Column iterator int MaxColWidth = 0; //initializing at zero BOOL bSubImage = FALSE; SetRedraw(FALSE); for (nCol; nCol < m_nColumnCount; nCol++) { //int temp = GetColumnWidth( nCol ); if (int temp = GetColumnWidth( nCol ) == 0) ListCtrlEx::OnNotify( wParam, lParam, pResult ); //SetColumnWidth( nCol, 0 ); else { int ColWidth1 = GetColumnWidth( SetColumnWidth( nCol, LVSCW_AUTOSIZE_USEHEADER ) ); int ColWidth2 = GetColumnWidth( SetColumnWidth( nCol, LVSCW_AUTOSIZE ) ); if( ColWidth1 > MINCOLWIDTH || ColWidth2 > MINCOLWIDTH ) MaxColWidth = max( ColWidth1, ColWidth2 ); else MaxColWidth = MINCOLWIDTH; if (bSubImage = TRUE) SetColumnWidth( nCol, MaxColWidth + COL_SEPARATION + 15 ); else SetColumnWidth( nCol, MaxColWidth ); } } SetRedraw(TRUE); } I'm not sure how to implement this. Please help. Thanks!ReplyIt's work!!!!.
Posted by Legacy on 09/28/2003 12:00amOriginally posted by: Joe
Thank you, for your article. It have a good result in my application.
ReplyHere's how to set individual column resizing preferences
Posted by Legacy on 08/06/2003 12:00amOriginally posted by: Gregory J. Spiers
ReplyHow to do it in win32 applicatio?
Posted by Legacy on 08/04/2003 12:00amOriginally posted by: vidya
It worked fine. Really good. But how the same thing can be done in win32 application?
ReplyIt was too good
Posted by Legacy on 06/02/2003 12:00amOriginally posted by: Kapil Arya
It was too good, I was searching for it, and found on the right time, Thankx for Ur effort, Keep Writing these type of code
ReplyHow do i do this in win32 api ?
Posted by Legacy on 05/01/2003 12:00amOriginally posted by: Wiseguy
Im having a litle problem subclassing the ListView. The example above is for mfc but how do i do this in win32. Please can any ine help?
Reply
how to do this in ATL
Posted by Legacy on 03/05/2003 12:00amOriginally posted by: venkatesh
ReplyColud you tell me how to restrict resizing the column header if the list control is added on a ATL dialog.
HDN_BEGINTRACKW ?, HDN_ENDTRACKA ?
Posted by Legacy on 08/26/2002 12:00amOriginally posted by: cromianha
Replysame problem of shinoj
Posted by Legacy on 11/06/2001 12:00amOriginally posted by: aisha
I do have the same problem as of shinoj, saving the column widths, can anybody help??
ReplySide affect of this addition
Posted by Legacy on 07/05/2001 12:00amOriginally posted by: Girish
Notify message is sent always to the parent window when you add this piece of code in the Message notification and if you are trying to add some string to a edit control on the NM_CLICK event of your list control the string to the edit control will not be added
ReplyLoading, Please Wait ...