FIX: CListCtrl::AutoSizeColumns

There's a small problem with the AutoSizeColumns routine (http://www.dev-archive.com/listview/autosize_col.shtml) if you use it when the style is LVS_LIST instead of LVS_REPORT, in that all the item text labels disappear and only the icons are displayed. In the routine, always change the style to LVS_REPORT before autosizing the columns, and then change it back. Here's my fix...

void CListCtrlEx::AutoSizeColumns( int col )

{

// Call this after your list control is filled

SetRedraw(false);

DWORD dwOldStyle = SetViewStyle ( LVS_REPORT );

int mincol = col < 0 ? 0 : col;

int maxcol = col < 0 ? GetColumnCount()-1 : col;

for (col = mincol; col <= maxcol; col++) {

SetColumnWidth(col,LVSCW_AUTOSIZE);

int wc1 = GetColumnWidth(col);

SetColumnWidth(col,LVSCW_AUTOSIZE_USEHEADER);

int wc2 = GetColumnWidth(col);

int wc = max(MINCOLWIDTH,max(wc1,wc2));

SetColumnWidth(col,wc);

}

SetViewStyle ( dwOldStyle );

SetRedraw(true);

Invalidate();

}
[1217 byte] By [Glenn Carr] at [2007-11-15 22:06:41]
# 1 Re: FIX: CListCtrl::AutoSizeColumns
[Ooops, meant to format it correctly the first time]

There's a small problem with the AutoSizeColumns routine (http://www.dev-archive.com/listview/autosize_col.shtml) if you use it when the style is LVS_LIST instead of LVS_REPORT, in that all the item text labels disappear and only the icons are displayed. In the routine, always change the style to LVS_REPORT before autosizing the columns, and then change it back. Here's my fix...

void CListCtrlEx::AutoSizeColumns( int col )

{

// Call this after your list control is filled

SetRedraw(false);

DWORD dwOldStyle = SetViewStyle ( LVS_REPORT );

int mincol = col < 0 ? 0 : col;

int maxcol = col < 0 ? GetColumnCount()-1 : col;

for (col = mincol; col <= maxcol; col++) {

SetColumnWidth(col,LVSCW_AUTOSIZE);

int wc1 = GetColumnWidth(col);

SetColumnWidth(col,LVSCW_AUTOSIZE_USEHEADER);

int wc2 = GetColumnWidth(col);

int wc = max(MINCOLWIDTH,max(wc1,wc2));

SetColumnWidth(col,wc);

}

SetViewStyle ( dwOldStyle );

SetRedraw(true);

Invalidate();

}
Glenn Carr at 2007-11-10 3:08:00 >