List Control控件技巧总汇【三】

21. 在CListCtrl显示文件,并根据文件类型来显示图标
网上找到的代码,share
       BOOL CTest6Dlg::OnInitDialog()
       {
           CDialog::OnInitDialog();  
           HIMAGELIST himlSmall;
           HIMAGELIST himlLarge;
           SHFILEINFO sfi;
           char   cSysDir[MAX_PATH];
           CString   strBuf;
           memset(cSysDir, 0, MAX_PATH);
           GetWindowsDirectory(cSysDir, MAX_PATH);
           strBuf = cSysDir;
           sprintf(cSysDir, "%s", strBuf.Left(strBuf.Find("[url=file://%22)+1/]//")+1[/url]));
           himlSmall = (HIMAGELIST)SHGetFileInfo ((LPCSTR)cSysDir, 0, &sfi, sizeof(SHFILEINFO), SHGFI_SYSICONINDEX | SHGFI_SMALLICON );
           himlLarge = (HIMAGELIST)SHGetFileInfo((LPCSTR)cSysDir, 0, &sfi, s izeof(SHFILEINFO), SHGFI_SYSICONINDEX | SHGFI_LARGEICON);
           if (himlSmall && himlLarge)
           {
                 ::SendMessage(m_list.m_hWnd,LVM_SETIMAGELIST, (WPARAM)LVSIL_SMALL, (LPARAM)himlSmall);
                 ::SendMessage(m_list.m_hWnd, LVM_SETIMAGELIST,
                             (WPARAM)LVSIL_NORMAL, (LPARAM)himlLarge);
           }
           return TRUE;   // return TRUE   unless you set the focus to a control
       }

       void CTest6Dlg::AddFiles(LPCTSTR lpszFileName, BOOL bAddToDocument)
       {
           int nIcon = GetIconIndex(lpszFileName, FALSE, FALSE);
           CString strSize;
           CFileFind filefind;

           //   get file size
           if (filefind.FindFile(lpszFileName))
           {
                 filefind.FindNextFile();
                 strSize.Format("%d", filefind.GetLength());
           }
           else
                 strSize = "0";

           // split path and filename
           CString strFileName = lpszFileName;
           CString strPath;

           int nPos = strFileName.ReverseFind('//');
           if (nPos != -1)
           {
                 strPath = strFileName.Left(nPos);
                 strFileName = strFileName.Mid(nPos + 1);
           }

           // insert to list
           int nItem = m_list.GetItemCount();
           m_list.InsertItem(nItem, strFileName, nIcon);
           m_list.SetItemText(nItem, 1, strSize);
           m_list.SetItemText(nItem, 2, strFileName.Right(3));
           m_list.SetItemText(nItem, 3, strPath);
       }

       int CTest6Dlg::GetIconIndex(LPCTSTR lpszPath, BOOL bIsDir, BOOL bSelected)
       {
           SHFILEINFO sfi;
           memset(&sfi, 0, sizeof(sfi));

           if (bIsDir)
           {
             SHGetFileInfo(lpszPath,  
                         FILE_ATTRIBUTE_DIRECTORY,  
                         &sfi,  
                         sizeof(sfi),  
                         SHGFI_SMALLICON | SHGFI_SYSICONINDEX |
                         SHGFI_USEFILEATTRIBUTES |(bSelected ? SHGFI_OPENICON : 0));  
             return   sfi.iIcon;
           }
           else
           {
             SHGetFileInfo (lpszPath,  
                         FILE_ATTRIBUTE_NORMAL,  
                         &sfi,  
                         sizeof(sfi),  
                         SHGFI_SMALLICON | SHGFI_SYSICONINDEX |  
                         SHGFI_USEFILEATTRIBUTES | (bSelected ? SHGFI_OPENICON : 0));
             return   sfi.iIcon;
           }
           return   -1;
       }
--------------------------------------------------------------------------------
22. listctrl内容进行大数据量更新时,避免闪烁
       m_list.SetRedraw(FALSE);
       //更新内容
       m_list.SetRedraw(TRUE);
       m_list.Invalidate();
       m_list.UpdateWindow();
或者参考

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_mfc_cwnd.3a3a.setredraw.asp

--------------------------------------------------------------------------------

23. listctrl排序
Q250614How To Sort Items in a CListCtrl in Report View
http://support.microsoft.com/kb/250614/en-us

--------------------------------------------------------------------------------

24.listctrl中选中某个item时动态改变其iconbitmap
Q141834: How to change the icon or the bitmap of a CListCtrl item in Visual C++
http://support.microsoft.com/kb/141834/en-us

--------------------------------------------------------------------------------

25. 在添加item后,再InsertColumn()后导致整列数据移动的问题
Q151897: CListCtrl::InsertColumn() Causes Column Data to Shift
http://support.microsoft.com/kb/151897/en-us

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值