MultiLineListBox.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. CDC* pDC = CDC::FromHandle(lpDIS->hDC);
  43. COLORREF rColor = (COLORREF)lpDIS->itemData; // RGB in item data
  44. CString sLabel;
  45. GetText(lpDIS->itemID, sLabel);
  46. // item selected
  47. if ((lpDIS->itemState & ODS_SELECTED) &&
  48. (lpDIS->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
  49. {
  50. // draw color box
  51. CBrush colorBrush(rColor);
  52. CRect colorRect = lpDIS->rcItem;
  53. // draw label background
  54. CBrush labelBrush(::GetSysColor(COLOR_HIGHLIGHT));
  55. CRect labelRect = lpDIS->rcItem;
  56. pDC->FillRect(&labelRect,&labelBrush);
  57. // draw label text
  58. COLORREF colorTextSave;
  59. COLORREF colorBkSave;
  60. colorTextSave = pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
  61. colorBkSave = pDC->SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
  62. pDC->DrawText( sLabel, -1, &lpDIS->rcItem, DT_WORDBREAK );
  63. pDC->SetTextColor(colorTextSave);
  64. pDC->SetBkColor(colorBkSave);
  65. return;
  66. }
  67. // item brought into box
  68. if (lpDIS->itemAction & ODA_DRAWENTIRE)
  69. {
  70. CBrush brush(rColor);
  71. CRect rect = lpDIS->rcItem;
  72. pDC->SetBkColor(rColor);
  73. pDC->FillRect(&rect,&brush);
  74. pDC->DrawText( sLabel, -1, &lpDIS->rcItem, DT_WORDBREAK );
  75. return;
  76. }
  77. // item deselected
  78. if (!(lpDIS->itemState & ODS_SELECTED) &&
  79. (lpDIS->itemAction & ODA_SELECT))
  80. {
  81. CRect rect = lpDIS->rcItem;
  82. CBrush brush(rColor);
  83. pDC->SetBkColor(rColor);
  84. pDC->FillRect(&rect,&brush);
  85. pDC->DrawText( sLabel, -1, &lpDIS->rcItem, DT_WORDBREAK );
  86. return;
  87. }
  88. }