2024年4月19日发(作者:)
自动调整ListCtrl的列的宽度
void CSortListCtrl::AutoSizeColumns(int col /*=-1*/,BOOL bFitWidth/*=F
ALSE*/)
{
// Call this after your list control is filled
if(!GetHeaderCtrl())return;
ShowWindow(SW_HIDE);//避免闪烁
SetRedraw(FALSE);
if(!bFitWidth)//如果不自动调整最后一列宽度,使得总列宽等于控件宽度,
InsertColumn(GetColumnCount(),"");
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 iFrom=GetTopIndex();
int nCountPerpage=GetCountPerPage();
if(nCountPerpage<=0)
nCountPerpage=GetItemCount();
int iTo=iFrom+nCountPerpage;
int nImgWidth=0;
int iIndent=0;
LVITEM li;
=LVIF_IMAGE|LVIF_INDENT;
IMAGEINFO ImageInfo;
for(int iItem=iFrom;iItem =iItem; GetItem(&li); iIndent=max(t,iIndent); if(!=-1){ CImageList* pImageList=GetImageList(LVSIL_SMALL); if(pImageList->GetSafeHandle()){ pImageList->GetImageInfo(,&ImageInfo); nImgWidth=max(nImgWidth, ); } } } int wc = max(20,max(wc1,wc2)); if(col==0) wc+=nImgWidth*(iIndent+1); else wc+=nImgWidth; SetColumnWidth(col,wc); } if(!bFitWidth) DeleteColumn(GetColumnCount()-1); SetRedraw(TRUE); ShowWindow(SW_SHOW); }


发布评论