Auto resizing column widths in a CListCtrl

Isuru picture Isuru · Apr 9, 2010 · Viewed 12.7k times · Source

How can I make a CListCtrl to resize the width of its columns automatically? Usually, when an item in the list gets too long, the back end disappears from view and the user manually has to resize the width of the corresponding column. Is there any way to do this by code?

Answer

dwo picture dwo · Apr 9, 2010

Resizing the columns automatically is easy:

for(int i = 0;i < pListCtrl->GetHeaderCtrl()->GetItemCount();++i)
    pListCtrl->SetColumnWidth(i,LVSCW_AUTOSIZE_USEHEADER);

This will optimize the columns.