mall 发表于 2013-11-10 18:56:07

CListCtrl使用技巧汇总【2】


21.在CListCtrl显示文件,并根据文件类型来显示图标   网上找到的代码,share
   BOOL CTest6Dlg::OnInitDialog()
   {
          CDialog::OnInitDialog();

          HIMAGELIST himlSmall;
          HIMAGELIST himlLarge;
          SHFILEINFO sfi;
          charcSysDir;
          CStringstrBuf;

          memset(cSysDir, 0, MAX_PATH);

          GetWindowsDirectory(cSysDir, MAX_PATH);
          strBuf = cSysDir;
          sprintf(cSysDir, "%s",strBuf.Left(strBuf.Find("\")+1));

          himlSmall = (HIMAGELIST)SHGetFileInfo((LPCSTR)cSysDir,
                     0,
                     &sfi,
                     sizeof(SHFILEINFO),
                     SHGFI_SYSICONINEX | SHGFI_SMALLICON );

          himlLarge =(HIMAGELIST)SHGetFileInfo((LPCSTR)cSysDir,
                     0,
                     &sfi,
                     sizeof(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, BOOLbAddToDocument)
   {
          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, BOOLbSelected)
   {
          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));
         returnsfi.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排序Q250614:How To Sort Items in a CListCtrl in ReportView
http://support.microsoft.com/kb/250614/en-us


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



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


26.关于listctrl第一列始终居左的问题解决办法:把第一列当一个虚列,从第二列开始插入列及数据,最后删除第一列。
   
具体解释参阅   http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/listview/structures/lvcolumn.asp


27. 锁定columnheader的拖动http://msdn.microsoft.com/msdnmag/issues/03/06/CQA/


28.如何隐藏clistctrl的列   把需隐藏的列的宽度设为0,然后检测当该列为隐藏列时,用上面第27点的锁定column 的拖动来实现


29.listctrl进行大数据量操作时,使用virtuallist http://www.codeguru.com/cpp/controls/listview/advanced/article.php/c4151/
http://www.codeproject.com/listctrl/virtuallist.asp


30.关于item只能显示259个字符的问题解决办法:需要在item上放一个edit。


31. 响应在listctrl的columnheader上的鼠标右键单击Q125694: How To Find Out Which Listview Column WasRight-Clicked
http://support.microsoft.com/kb/125694/en-us


32.类似于windows资源管理器的listviewQ234310: How to implement a ListView control that issimilar to Windows Explorer by using DirLV.exe
http://support.microsoft.com/kb/234310/en-us


本文摘自:http://blog.sina.com.cn/s/blog_4171e80d0100ynch.html

dwwja 发表于 2014-2-16 19:37:31

我也想了解,请大家都说说











static/image/common/sigline.gif
19V5A电源适配器
页: [1]
查看完整版本: CListCtrl使用技巧汇总【2】