发信人: jiangsheng() 
整理人: wenbobo(2002-12-27 15:57:42), 站内信件
 | 
 
 
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;
 			li.mask=LVIF_IMAGE|LVIF_INDENT;
 			IMAGEINFO	ImageInfo;
 			for(int iItem=iFrom;iItem<iTo;iItem++){
 				li.iItem=iItem;
 				GetItem(&li);
 				iIndent=max(li.iIndent,iIndent);
 				if(li.iImage!=-1){
 					CImageList*	pImageList=GetImageList(LVSIL_SMALL);
 					if(pImageList->GetSafeHandle()){
 						pImageList->GetImageInfo(li.iImage,&ImageInfo);
 						nImgWidth=max(nImgWidth,ImageInfo.rcImage.right-ImageInfo.rcImag 
 e.left);
 					}
 				}
 			}
 			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);
 }
 
 
 -- HE WHO CONTROLS THE PAST, COMMANDS THE FUTURE.
 HE WHO CONTROLS THE FUTURE CONQUERS THE PAST.
  ※ 修改:.jiangsheng 于 Sep  8 23:37:25 修改本文.[FROM: 202.96.44.196] ※ 来源:.月光软件站 http://www.moon-soft.com.[FROM: 202.96.44.196]
  | 
 
 
 |