FileListBox.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // FileListBox.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "FileListBox.h"
  5. #include "FileObject.h"
  6. #include "DirObject.h"
  7. // CFileListBox
  8. IMPLEMENT_DYNAMIC(CFileListBox, CListBox)
  9. CFileListBox::CFileListBox()
  10. {
  11. }
  12. CFileListBox::~CFileListBox()
  13. {
  14. }
  15. BEGIN_MESSAGE_MAP(CFileListBox, CListBox)
  16. ON_WM_MEASUREITEM()
  17. END_MESSAGE_MAP()
  18. // CFileListBox message handlers
  19. void CFileListBox::PreSubclassWindow()
  20. {
  21. //ModifyStyle(0,LBS_OWNERDRAWFIXED);
  22. CListBox::PreSubclassWindow();
  23. }
  24. BOOL CFileListBox::PreCreateWindow(CREATESTRUCT& cs)
  25. {
  26. cs.style |= LBS_OWNERDRAWFIXED;
  27. return CListBox::PreCreateWindow(cs);
  28. }
  29. void CFileListBox::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMIS)
  30. {
  31. lpMIS->itemHeight = 24;
  32. CListBox::OnMeasureItem(nIDCtl, lpMIS);
  33. }
  34. void CFileListBox::DrawItem(LPDRAWITEMSTRUCT lpDIS)
  35. {
  36. if (lpDIS->itemID == -1)
  37. return;
  38. CString strFile;
  39. CObject* pObject = (CObject*)GetItemData(lpDIS->itemID);
  40. ASSERT(pObject != NULL);
  41. if (pObject == NULL)
  42. return;
  43. if (!pObject->IsKindOf(RUNTIME_CLASS(CBaseObject)))
  44. return;
  45. CBaseObject* pBaseObject = (CBaseObject*)pObject;
  46. strFile = pBaseObject->GetPath();
  47. SHFILEINFO shFileInfo = {0};
  48. ::SHGetFileInfo(strFile, 0, &shFileInfo, sizeof(SHFILEINFO),
  49. SHGFI_DISPLAYNAME|SHGFI_ICON|SHGFI_SMALLICON);
  50. CRect rect(lpDIS->rcItem);
  51. HBRUSH hBrush;
  52. COLORREF clrText;
  53. if (lpDIS->itemState & ODS_SELECTED)
  54. {
  55. hBrush = GetSysColorBrush(COLOR_HIGHLIGHT);
  56. clrText = GetSysColor(COLOR_HIGHLIGHTTEXT);
  57. }
  58. else
  59. {
  60. hBrush = GetSysColorBrush(COLOR_WINDOW);
  61. clrText = GetSysColor(COLOR_WINDOWTEXT);
  62. }
  63. ::FillRect(lpDIS->hDC, &lpDIS->rcItem, hBrush);
  64. if (lpDIS->itemState & ODS_FOCUS)
  65. ::DrawFocusRect(lpDIS->hDC, &rect);
  66. DrawIconEx(lpDIS->hDC, rect.left + 4, rect.top+4, shFileInfo.hIcon,
  67. 16, 16, 0, NULL, DI_NORMAL);
  68. COLORREF oldColor = ::SetTextColor(lpDIS->hDC, clrText);
  69. int nOldMode = ::SetBkMode(lpDIS->hDC, TRANSPARENT);
  70. rect.left += 24;
  71. DrawText(lpDIS->hDC, shFileInfo.szDisplayName,
  72. (int)_tcslen(shFileInfo.szDisplayName), &rect,
  73. DT_LEFT|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS);
  74. ::SetBkMode(lpDIS->hDC, nOldMode);
  75. ::SetTextColor(lpDIS->hDC, oldColor);
  76. }
  77. void CFileListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMIS)
  78. {
  79. lpMIS->itemHeight = 24;
  80. }