天天看點

CListCtrl的基本用法

BOOL CPage2::OnInitDialog()

{

 CDialog::OnInitDialog();

 BOOL bImageOk = FALSE;

 //init function code

 DWORD dwStyle = m_lstCtl.GetExtendedStyle();

 dwStyle |= LVS_EX_FULLROWSELECT;

 m_lstCtl.SetExtendedStyle(dwStyle);

 //attach image list

 HIMAGELIST hList = ImageList_Create(16, 16, ILC_COLOR8 | ILC_MASK, 8, 1);

 if (hList)

 {

  bImageOk = TRUE;

  m_ilSmall.Attach(hList);

  CBitmap cBmp;

  cBmp.LoadBitmap(IDB_BITMAP5);

  m_ilSmall.Add(&cBmp, RGB(255,0, 255));

  cBmp.DeleteObject();

  m_lstCtl.SetImageList(&m_ilSmall, LVSIL_SMALL);

  //closehandle no need

  //CloseHandle(hList);

 }

 //init the column

 CRect rect;

 m_lstCtl.GetClientRect(&rect);

 DWORD nColInterval = rect.Width()/5;

 m_lstCtl.InsertColumn(0, "驅動器", LVCFMT_LEFT, nColInterval);

 m_lstCtl.InsertColumn(1, "裝置類型", LVCFMT_LEFT, nColInterval);

 m_lstCtl.InsertColumn(2, "檔案系統", LVCFMT_LEFT, nColInterval);

 m_lstCtl.InsertColumn(3, "總空間", LVCFMT_LEFT, nColInterval);

 m_lstCtl.InsertColumn(4, "可用空間", LVCFMT_LEFT, rect.Width() - 4 * nColInterval);

 //init the line

 // Delete the current contents

 m_lstCtl.DeleteAllItems();

 LVITEM lvi;

 for(int index = 0; index != m_vecDskInf.size(); ++ index)

 {

  lvi.mask =  LVIF_IMAGE | LVIF_TEXT;

  lvi.iItem = index;

  //set item 0

  lvi.iSubItem = 0;

  lvi.pszText = (LPTSTR)(LPCTSTR)(m_vecDskInf[index].m_strDiskName.c_str());

  lvi.iImage = 1;  // There are 8 images in the image list

  m_lstCtl.InsertItem(&lvi);

  //set item 1

  lvi.iSubItem =1;

  lvi.pszText = (LPTSTR)(LPCTSTR)(m_vecDskInf[index].m_strTypeName.c_str());

  m_lstCtl.SetItem(&lvi);

  //set item 2

  lvi.iSubItem =2;

  lvi.pszText = (LPTSTR)(LPCTSTR)(m_vecDskInf[index].m_strFileSystem.c_str());

  m_lstCtl.SetItem(&lvi);

  CString strTmp;

  //set item 3

  strTmp.Format("%ld MB", m_vecDskInf[index].m_dwTotalMBytes);

  lvi.iSubItem =3;

  lvi.pszText = (LPTSTR)(LPCTSTR)(strTmp);

  m_lstCtl.SetItem(&lvi);

  //set item 4

  strTmp.Format("%ld MB", m_vecDskInf[index].m_dwFreeMBytes);

  lvi.iSubItem =4;

  lvi.pszText = (LPTSTR)(LPCTSTR)(strTmp);

  m_lstCtl.SetItem(&lvi);

 }

 return TRUE;

繼續閱讀