BmpComBox.cpp
上传用户:guangzhiyw
上传日期:2007-01-09
资源大小:495k
文件大小:4k
源码类别:

ICQ/即时通讯

开发平台:

Visual C++

  1. // BmpComBox.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "BmpComBox.h"
  5. #ifdef _DEBUG
  6. #define new DEBUG_NEW
  7. #undef THIS_FILE
  8. static char THIS_FILE[] = __FILE__;
  9. #endif
  10. /////////////////////////////////////////////////////////////////////////////
  11. // CBmpComBox
  12. CBmpComBox::CBmpComBox()
  13. {
  14. m_size=CSize(32,32);
  15. m_pimaList=NULL;
  16. }
  17. CBmpComBox::~CBmpComBox()
  18. {
  19. }
  20. BEGIN_MESSAGE_MAP(CBmpComBox, CComboBox)
  21. //{{AFX_MSG_MAP(CBmpComBox)
  22. // NOTE - the ClassWizard will add and remove mapping macros here.
  23. //}}AFX_MSG_MAP
  24. END_MESSAGE_MAP()
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CBmpComBox message handlers
  27. void CBmpComBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
  28. {
  29. CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
  30. if (!IsWindowEnabled())
  31. {
  32.         CBrush brDisabled(RGB(192,192,192)); // light gray
  33. CBrush* pOldBrush = pDC->SelectObject(&brDisabled);
  34. CPen penDisabled(PS_SOLID, 1, RGB(192,192,192));
  35.         CPen* pOldPen = pDC->SelectObject(&penDisabled);
  36.         OnOutputBmp(lpDrawItemStruct, FALSE);
  37. pDC->SelectObject(pOldBrush);
  38. pDC->SelectObject(pOldPen);
  39. return;
  40. }
  41. // Selected
  42. if ((lpDrawItemStruct->itemState & ODS_SELECTED) 
  43. && (lpDrawItemStruct->itemAction & (ODA_SELECT | ODA_DRAWENTIRE))) 
  44. {
  45. CBrush brHighlight(::GetSysColor(COLOR_HIGHLIGHT)); 
  46. CBrush* pOldBrush = pDC->SelectObject(&brHighlight);
  47. CPen penHighlight(PS_SOLID, 1, ::GetSysColor(COLOR_HIGHLIGHT));
  48. CPen* pOldPen = pDC->SelectObject(&penHighlight);
  49. pDC->Rectangle(&lpDrawItemStruct->rcItem);
  50. pDC->SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
  51.         pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
  52. OnOutputBmp(lpDrawItemStruct, TRUE);
  53. pDC->SelectObject(pOldBrush);
  54. pDC->SelectObject(pOldPen);
  55. }
  56. // De-Selected
  57. if (!(lpDrawItemStruct->itemState & ODS_SELECTED) 
  58. && (lpDrawItemStruct->itemAction & (ODA_SELECT | ODA_DRAWENTIRE))) 
  59. {
  60. CBrush brWindow(::GetSysColor(COLOR_WINDOW)); 
  61. CBrush* pOldBrush = pDC->SelectObject(&brWindow);
  62. CPen penHighlight(PS_SOLID, 1, ::GetSysColor(COLOR_WINDOW));
  63. CPen* pOldPen = pDC->SelectObject(&penHighlight);
  64. pDC->Rectangle(&lpDrawItemStruct->rcItem);
  65. pDC->SetBkColor(::GetSysColor(COLOR_WINDOW));
  66.         pDC->SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));
  67. OnOutputBmp(lpDrawItemStruct, FALSE);
  68. pDC->SelectObject(pOldBrush);
  69. pDC->SelectObject(pOldPen);
  70. }
  71.     // Focus
  72.     if (lpDrawItemStruct->itemAction & ODA_FOCUS) 
  73.         pDC->DrawFocusRect(&lpDrawItemStruct->rcItem);
  74.     }
  75. }
  76. void CBmpComBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct) 
  77. {
  78. lpMeasureItemStruct->itemHeight = (m_size.cy + 2); 
  79. }
  80. void CBmpComBox::OnOutputBmp(LPDRAWITEMSTRUCT lpDrawItemStruct, BOOL bSelected)
  81. {
  82. if (GetCurSel() == CB_ERR || GetCount() == 0) 
  83. return; // no item selected
  84.     
  85. CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
  86. int index=lpDrawItemStruct->itemData;
  87. int x = lpDrawItemStruct->rcItem.left + ((lpDrawItemStruct->rcItem.right - lpDrawItemStruct->rcItem.left) / 2) - (m_size.cx / 2);
  88. int y = lpDrawItemStruct->rcItem.top + ((lpDrawItemStruct->rcItem.bottom - lpDrawItemStruct->rcItem.top) / 2) - (m_size.cy / 2);
  89. m_pimaList->Draw(pDC,index,CPoint(x,y),ILD_NORMAL);
  90. }
  91. void CBmpComBox::SetImageList(CImageList *pList)
  92. {
  93. ASSERT(pList!=NULL);
  94. m_pimaList=pList;
  95. int i,index;
  96. for(i=0;i<pList->GetImageCount();i++)
  97. {
  98. index=AddString("");
  99. SetItemData(index,index);
  100. }
  101. IMAGEINFO iInfo;
  102. pList->GetImageInfo(0,&iInfo);
  103. m_size=CRect(iInfo.rcImage).Size();
  104. SetItemHeight(-1,m_size.cy);
  105. }
  106. void CBmpComBox::SetItemSize(CSize size)
  107. {
  108. m_size=size;
  109. SetItemHeight(-1,m_size.cy);
  110. }