MultiLineListBox.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // MultiLineListBox.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "MultiLineListBox.h"
  5. #ifdef _DEBUG
  6. #define new DEBUG_NEW
  7. #undef THIS_FILE
  8. static char THIS_FILE[] = __FILE__;
  9. #endif
  10. /////////////////////////////////////////////////////////////////////////////
  11. // CMultiLineListBox
  12. BEGIN_MESSAGE_MAP(CMultiLineListBox, CListBox)
  13. //{{AFX_MSG_MAP(CMultiLineListBox)
  14. // NOTE - the ClassWizard will add and remove mapping macros here.
  15. //}}AFX_MSG_MAP
  16. END_MESSAGE_MAP()
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CMultiLineListBox message handlers
  19. void CMultiLineListBox::AddEntry(LPCTSTR lpszItem, COLORREF color, int nIndex /* 0 */)
  20. {
  21. int index = InsertString(nIndex, lpszItem);
  22. SetItemData(index,color);
  23. }
  24. void CMultiLineListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMIS)
  25. {
  26. // all items are of fixed size
  27. // must use LBS_OWNERDRAWVARIABLE for this to work
  28. int nItem = lpMIS->itemID;
  29. CPaintDC dc(this);
  30. CString sLabel;
  31. CRect rcLabel;
  32. GetText( nItem, sLabel );
  33. GetItemRect(nItem, rcLabel);
  34. // Using the flags below, calculate the required rectangle for
  35. // the text and set the item height for this specific item based
  36. // on the return value (new height).
  37. int itemHeight = dc.DrawText( sLabel, -1, rcLabel, DT_WORDBREAK | DT_CALCRECT );
  38. lpMIS->itemHeight = itemHeight;
  39. }
  40. void CMultiLineListBox::DrawItem(LPDRAWITEMSTRUCT lpDIS)
  41. {
  42. if ( lpDIS->itemID == -1 )
  43. return;
  44. CDC* pDC = CDC::FromHandle(lpDIS->hDC);
  45. COLORREF rColor = (COLORREF)lpDIS->itemData; // RGB in item data
  46. CString sLabel;
  47. GetText(lpDIS->itemID, sLabel);
  48. // item selected
  49. if ((lpDIS->itemState & ODS_SELECTED) && (lpDIS->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
  50. {
  51. // draw color box
  52. CBrush colorBrush(rColor);
  53. CRect colorRect = lpDIS->rcItem;
  54. // draw label background
  55. CBrush labelBrush(::GetSysColor(COLOR_HIGHLIGHT));
  56. CRect labelRect = lpDIS->rcItem;
  57. pDC->FillRect(&labelRect,&labelBrush);
  58. // draw label text
  59. COLORREF colorTextSave;
  60. COLORREF colorBkSave;
  61. colorTextSave = pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
  62. colorBkSave = pDC->SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
  63. pDC->DrawText( sLabel, -1, &lpDIS->rcItem, DT_WORDBREAK );
  64. pDC->SetTextColor(colorTextSave);
  65. pDC->SetBkColor(colorBkSave);
  66. return;
  67. }
  68. // item brought into box
  69. if (lpDIS->itemAction & ODA_DRAWENTIRE)
  70. {
  71. CBrush brush(rColor);
  72. CRect rect = lpDIS->rcItem;
  73. pDC->SetBkColor(rColor);
  74. pDC->FillRect(&rect,&brush);
  75. pDC->DrawText( sLabel, -1, &lpDIS->rcItem, DT_WORDBREAK );
  76. return;
  77. }
  78. // item deselected
  79. if (!(lpDIS->itemState & ODS_SELECTED) &&
  80. (lpDIS->itemAction & ODA_SELECT))
  81. {
  82. CRect rect = lpDIS->rcItem;
  83. CBrush brush(rColor);
  84. pDC->SetBkColor(rColor);
  85. pDC->FillRect(&rect,&brush);
  86. pDC->DrawText( sLabel, -1, &lpDIS->rcItem, DT_WORDBREAK );
  87. return;
  88. }
  89. }