OwnList.cpp
上传用户:mgf822
上传日期:2013-10-03
资源大小:133k
文件大小:2k
源码类别:

对话框与窗口

开发平台:

Visual C++

  1. // OwnList.cpp : implementation file
  2. // See OwnList.h for details on how to use this class
  3. //
  4. #include "stdafx.h"
  5. #include "OwnList.h"
  6. #include "MainFrm.h"
  7. #include "ChildFrm.h"
  8. #include "RTDemoDoc.h"
  9. #include "RTDemoView.h"
  10. #ifdef _DEBUG
  11. #undef THIS_FILE
  12. static char BASED_CODE THIS_FILE[] = __FILE__;
  13. #endif
  14. /////////////////////////////////////////////////////////////////////////////
  15. // COwnerDrawListBox
  16. COwnerDrawListBox::COwnerDrawListBox()
  17. {
  18. }
  19. COwnerDrawListBox::~COwnerDrawListBox()
  20. {
  21. }
  22. BEGIN_MESSAGE_MAP(COwnerDrawListBox, CListBox)
  23. //{{AFX_MSG_MAP(COwnerDrawListBox)
  24. // NOTE - the ClassWizard will add and remove mapping macros here.
  25. //}}AFX_MSG_MAP
  26. END_MESSAGE_MAP()
  27. /////////////////////////////////////////////////////////////////////////////
  28. // COwnerDrawListBox message handlers
  29. void COwnerDrawListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
  30. {
  31. CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
  32. int Index = (int)lpDrawItemStruct->itemData; // RGB in item data
  33. // White space
  34. CRect rcItem(lpDrawItemStruct->rcItem);
  35. rcItem.right /= 5;
  36. rcItem.InflateRect(-2, -2);
  37. CChildFrame* pChild = (CChildFrame*)(((CMainFrame*)(AfxGetApp()->m_pMainWnd))->MDIGetActive());
  38. CRTDemoView* pView = ((CRTDemoView*)pChild->m_wndSplitter.GetPane(0, 0));
  39. pView->GetColorAndName(Index, m_nColor, m_szName); 
  40. // Paint the color item in the color requested
  41. CBrush brush(m_nColor);
  42. pDC->FillRect(rcItem, &brush);
  43. pDC->Draw3dRect(rcItem, GetSysColor(COLOR_BTNHILIGHT), GetSysColor(COLOR_BTNSHADOW));
  44. int bm  = pDC->SetBkMode(TRANSPARENT);
  45. pDC->TextOut( rcItem.right + 10, rcItem.top + 2, m_szName);
  46. pDC->SetBkMode(bm);
  47. // Focus rect
  48. if (lpDrawItemStruct->itemAction & ODA_FOCUS)
  49. pDC->DrawFocusRect(&lpDrawItemStruct->rcItem);
  50. }
  51. int COwnerDrawListBox::CompareItem(LPCOMPAREITEMSTRUCT lpCompareItemStruct) 
  52. {
  53. int Index1 = (int)lpCompareItemStruct->itemData1;
  54. int Index2 = (int)lpCompareItemStruct->itemData2;
  55. if (Index1 > Index2) return 1;
  56. else if (Index1 == Index2) return 0;       // exact match
  57. else return -1;
  58. }
  59. #define COLOR_ITEM_HEIGHT   20
  60. void COwnerDrawListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
  61. {
  62. // all items are of fixed size
  63. // must use LBS_OWNERDRAWVARIABLE for this to work
  64. lpMeasureItemStruct->itemHeight = COLOR_ITEM_HEIGHT;
  65. }