MultiLineListBox.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. ASSERT(lpDIS->CtlType == ODT_LISTBOX);
  44. LPCTSTR lpszText = (LPCTSTR) lpDIS->itemData;
  45. if ( lpszText == NULL )
  46. {// µ±ÎÞÈκμǼʱ,Í˳ö;
  47. return;
  48. }
  49. COLORREF rColor = (COLORREF)lpDIS->itemData; // RGB in item data
  50. CString sLabel;
  51. GetText(lpDIS->itemID, sLabel);
  52. // item selected
  53. if ((lpDIS->itemState & ODS_SELECTED) &&
  54. (lpDIS->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
  55. {
  56. // draw color box
  57. CBrush colorBrush(rColor);
  58. CRect colorRect = lpDIS->rcItem;
  59. // draw label background
  60. CBrush labelBrush(::GetSysColor(COLOR_HIGHLIGHT));
  61. CRect labelRect = lpDIS->rcItem;
  62. pDC->FillRect(&labelRect,&labelBrush);
  63. // draw label text
  64. COLORREF colorTextSave;
  65. COLORREF colorBkSave;
  66. colorTextSave = pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
  67. colorBkSave = pDC->SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
  68. pDC->DrawText( sLabel, -1, &lpDIS->rcItem, DT_WORDBREAK );
  69. pDC->SetTextColor(colorTextSave);
  70. pDC->SetBkColor(colorBkSave);
  71. return;
  72. }
  73. // item brought into box
  74. if (lpDIS->itemAction & ODA_DRAWENTIRE)
  75. {
  76. CBrush brush(rColor);
  77. CRect rect = lpDIS->rcItem;
  78. pDC->SetBkColor(rColor);
  79. pDC->FillRect(&rect,&brush);
  80. pDC->DrawText( sLabel, -1, &lpDIS->rcItem, DT_WORDBREAK );
  81. return;
  82. }
  83. // item deselected
  84. if (!(lpDIS->itemState & ODS_SELECTED) &&
  85. (lpDIS->itemAction & ODA_SELECT))
  86. {
  87. CRect rect = lpDIS->rcItem;
  88. CBrush brush(rColor);
  89. pDC->SetBkColor(rColor);
  90. pDC->FillRect(&rect,&brush);
  91. pDC->DrawText( sLabel, -1, &lpDIS->rcItem, DT_WORDBREAK );
  92. return;
  93. }
  94. }