CListCtrl使用技巧彙總(二)

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(" \\")+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, 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();
 
或者參考
 
 
23. listctrl排序
Q250614:How To Sort Items in a CListCtrl in Report View
http://support.microsoft.com/kb/250614/en-us
 
 
24. 在listctrl中選中某個item時動態改變其icon或bitmap
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
 
 
26. 關於listctrl第一列始終居左的問題
解決辦法:把第一列當一個虛列,從第二列開始插入列及數據,最後刪除第一列。
     
具體解釋參閱   http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/listview/structures/lvcolumn.asp

 
27. 鎖定column header的拖動
http://msdn.microsoft.com/msdnmag/issues/03/06/CQA/
 
 
28. 如何隱藏clistctrl的列
    把需隱藏的列的寬度設爲0,而後檢測當該列爲隱藏列時,用上面第27點的鎖定column 的拖動來實現
 
 
 
30. 關於item只能顯示259個字符的問題
解決辦法:須要在item上放一個edit。
 
 
31. 響應在listctrl的column header上的鼠標右鍵單擊
Q125694: How To Find Out Which Listview Column Was Right-Clicked
http://support.microsoft.com/kb/125694/en-us
 
 
32. 相似於windows資源管理器的listview
Q234310: How to implement a ListView control that is similar to Windows Explorer by using DirLV.exe
http://support.microsoft.com/kb/234310/en-us

 
33. 在ListCtrl中OnTimer只響應兩次的問題
Q200054:
PRB: OnTimer() Is Not Called Repeatedly for a List Control
http://support.microsoft.com/kb/200054/en-us

34. 如下爲一些爲實現各類自定義功能的listctrl派生類
          (1)    拖放       
                   http://www.codeproject.com/listctrl/dragtest.asp
                   在CListCtrl和CTreeCtrl間拖放
                   http://support.microsoft.com/kb/148738/en-us
 
          (2)    多功能listctrl
                   支持subitem可編輯,圖標,radiobutton,checkbox,字符串改變顏色的類
                   http://www.codeproject.com/listctrl/quicklist.asp
 
                   支持排序,subitem可編輯,subitem圖標,subitem改變顏色的類
                   http://www.codeproject.com/listctrl/ReportControl.asp
          (3)    subitem中顯示超連接
                   http://www.codeproject.com/listctrl/CListCtrlLink.asp
          (4)    subitem的tooltip提示
                   http://www.codeproject.com/listctrl/ctooltiplistctrl.asp
          (5)    subitem中顯示進度條   
                   http://www.codeproject.com/listctrl/ProgressListControl.asp
                   http://www.codeproject.com/listctrl/napster.asp
                   http://www.codeguru.com/Cpp/controls/listview/article.php/c4187/
          (6)    動態改變subitem的顏色和背景色
                    http://www.codeproject.com/listctrl/highlightlistctrl.asp
                    http://www.codeguru.com/Cpp/controls/listbox/colorlistboxes/article.php/c4757/
 
          (7)    類vb屬性對話框
            &bsp;       http://www.codeproject.com/listctrl/propertylistctrl.asp
                    http://www.codeguru.com/Cpp/controls/listview/propertylists/article.php/c995/
                    http://www.codeguru.com/Cpp/controls/listview/propertylists/article.php/c1041/
 
          (8)    選中subitem(只高亮選中的item)
                    http://www.codeproject.com/listctrl/SubItemSel.asp
                    http://www.codeproject.com/listctrl/ListSubItSel.asp
 
          (9)    改變行高
                    http://www.codeproject.com/listctrl/changerowheight.asp
 
          (10)   改變行顏色
                    http://www.codeproject.com/listctrl/coloredlistctrl.asp
 
          (11)   可編輯subitem的listctrl
                    http://www.codeproject.com/listctrl/nirs2000.asp
                    http://www.codeproject.com/listctrl/editing_subitems_in_listcontrol.asp
 
          (12)   subitem可編輯,插入combobox,改變行顏色,subitem的tooltip提示
                    http://www.codeproject.com/listctrl/reusablelistcontrol.asp
 
          (13)   header 中容許多行字符串
                    http://www.codeproject.com/listctrl/headerctrlex.asp
 
          (14)   插入combobox
                    http://www.codeguru.com/Cpp/controls/listview/editingitemsandsubitem/article.php/c979/
 
          (15)   添加背景圖片
                    http://www.codeguru.com/Cpp/controls/listview/backgroundcolorandp_w_picpath/article.php/c4173/
                    http://www.codeguru.com/Cpp/controls/listview/backgroundcolorandp_w_picpath/article.php/c983/
                    http://www.vchelp.net/vchelp/archive.asp?type_id=9&class_id=1&cata_id=1&article_id=1088&search_term=
   
          (16)  自適應寬度的listctrl
                    http://www.codeproject.com/useritems/AutosizeListCtrl.asp

          (17)  改變ListCtrl高亮時的顏色(默認爲藍色)
                   處理 NM_CUSTOMDRAW
           http://www.codeproject.com/listctrl/lvcustomdraw.asp

//2009-05-06
35.  隱藏滾動條
void   CListView_ScrollBarView::OnSize(UINT   nType,   int   cx,   int   cy)    
  {  
  CListCtrl   &   ctrl   =   this->GetListCtrl   ();  
  if   (ctrl)  
  {  
                          //隱藏垂直滾動條  
          ::ShowScrollBar(   ctrl.m_hWnd,   SB_VERT,   FALSE);  
  }  
  CListView::OnSize(nType,   cx,   cy);  
  }
//2009-05-07
36. 處理鍵盤,循環滾動,不要在對話框中處理該消息,不然WListCtrlEx會產生兩次按鍵事件
BOOL WListCtrlEx::PreTranslateMessage(MSG* pMsg)
{
 // TODO: Add your specialized code here and/or call the base class
 if (pMsg->message == WM_KEYDOWN)
 {
  if (pMsg->wParam == VK_DOWN)
  {
   int nSel = GetNextItem(-1, LVNI_SELECTED);
   if (nSel == -1)
    return FALSE;
   if (nSel == GetItemCount()-1)
   {
    EnsureVisible(0, FALSE);   //滾動條自動滾動到第80行  
    SetItemState(0, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);
    return TRUE;
   }
  }
  if (pMsg->wParam == VK_UP)
  {  
   int nSel = GetNextItem(-1, LVNI_SELECTED);
   if (nSel == -1)
    return FALSE;
   if (nSel == 0)
   {
    EnsureVisible(GetItemCount()-1, FALSE);   //滾動條自動滾動到第80行 
    SetItemState(GetItemCount()-1, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);
    return TRUE;
   }
  }
 }
 
 return CListCtrl::PreTranslateMessage(pMsg);
}
 
本文來自CSDN博客,轉載請標明出處: http://blog.csdn.net/dengjiang1999/archive/2009/05/04/4149366.aspx
相關文章
相關標籤/搜索