CategoryCombobox.cpp
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:4k
源码类别:

P2P编程

开发平台:

Visual C++

  1. // CategoryCombobox.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "testbt.h"
  5. #include "CategoryCombobox.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CCategoryCombobox
  13. CCategoryCombobox::CCategoryCombobox()
  14. {
  15. }
  16. CCategoryCombobox::~CCategoryCombobox()
  17. {
  18. }
  19. BEGIN_MESSAGE_MAP(CCategoryCombobox, CComboBox)
  20. //{{AFX_MSG_MAP(CCategoryCombobox)
  21. ON_WM_DESTROY()
  22. //}}AFX_MSG_MAP
  23. END_MESSAGE_MAP()
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CCategoryCombobox message handlers
  26. void CCategoryCombobox::DrawItem( LPDRAWITEMSTRUCT pDIStruct )
  27. {
  28. assert(m_pimgList);
  29. static CString sFolder, tmpStr; // No Need To Reallocate Each Time
  30. CDC dcContext;
  31. CRect rItemRect(pDIStruct->rcItem);
  32. CRect rBlockRect(rItemRect);
  33. CRect rTextRect(rBlockRect);
  34. CBrush brFrameBrush;
  35. int iItem = pDIStruct -> itemID;
  36. int iState = pDIStruct -> itemState;
  37. int iImageIndex = 0;
  38. int iIndent;
  39. COLORREF crColor = GetSysColor(COLOR_WINDOW);
  40. COLORREF crNormal = GetSysColor(COLOR_WINDOW);
  41. COLORREF crSelected = GetSysColor(COLOR_HIGHLIGHT);
  42. COLORREF crText = GetSysColor(COLOR_WINDOWTEXT);
  43. // Attach the CDC object
  44. if(!dcContext.Attach(pDIStruct -> hDC ))
  45. return;
  46. brFrameBrush.CreateStockObject(BLACK_BRUSH);
  47. // Draw folder text and icon
  48. // If an item is selected...
  49. if( iItem != -1 )
  50. {
  51. // Get text for normal and disabled
  52. CString sFolder;
  53. GetLBText(iItem, sFolder);
  54. LPSFOLDER tmpFolder = (LPSFOLDER)GetItemDataPtr(iItem);
  55. iIndent = tmpFolder->m_iIndent;
  56. // if draw edit text, then no indent.
  57. if (iState & ODS_COMBOBOXEDIT)
  58. iIndent = 1;
  59. // Calculate icon display area and set the width
  60. rBlockRect.DeflateRect(CSize(2, 2));
  61. rBlockRect.right = ICONWIDTH * iIndent;
  62. // Calculate Text Area
  63. // Set start of text offset a bit
  64. rTextRect.left += (ICONWIDTH * iIndent) + 2;
  65. rTextRect.top += 2;
  66. CSize size = dcContext.GetTextExtent(sFolder);
  67. rTextRect.right = rTextRect.left + size.cx + 4;
  68. ////////////////////////////////////////////////
  69. // draw focus select.
  70. if(iState & ODS_SELECTED)
  71. {
  72. dcContext.SetTextColor((0x00FFFFFF & ~(crText)));
  73. dcContext.SetBkColor( crSelected );
  74. dcContext.FillSolidRect( &rTextRect, crSelected );
  75. }
  76. else
  77. {
  78. dcContext.SetTextColor( crText );
  79. dcContext.SetBkColor( crNormal );
  80. dcContext.FillSolidRect( &rTextRect, crNormal );
  81. }
  82. // If item has the focus...draw the focus rectangle. 
  83. // **** useless.
  84. if( iState & ODS_FOCUS )
  85. dcContext.DrawFocusRect( &rTextRect );
  86. ////////////////////////////////////////////////
  87. // draw text and image.
  88. iImageIndex = tmpFolder->m_iImageIndex;
  89. if( iState & ODS_DISABLED )
  90. {
  91. crColor = GetSysColor(COLOR_INACTIVECAPTIONTEXT);
  92. dcContext.SetTextColor(crColor);
  93. CPoint pt(pDIStruct->rcItem.left, pDIStruct->rcItem.top);
  94. pt.x = pDIStruct->rcItem.left + ((iIndent - 1) * ICONWIDTH);
  95. m_pimgList->Draw(&dcContext, iImageIndex, pt, ILD_TRANSPARENT);
  96. }
  97. else
  98. {
  99. CPoint pt(pDIStruct->rcItem.left, pDIStruct->rcItem.top);
  100. pt.x = pDIStruct->rcItem.left + ((iIndent - 1) * ICONWIDTH);
  101. m_pimgList->Draw(&dcContext, iImageIndex, pt, ILD_NORMAL);
  102. }
  103. dcContext.SetBkMode(TRANSPARENT);
  104. dcContext.TextOut(rTextRect.left, rTextRect.top, sFolder);
  105. }
  106. // Detach DC
  107. dcContext.Detach();
  108. }
  109. void CCategoryCombobox::OnDestroy() 
  110. {
  111. DeleteAllItems();
  112. CComboBox::OnDestroy();
  113. }
  114. void CCategoryCombobox::DeleteAllItems()
  115. {
  116. // Iterate through the items destroying all of the
  117. // associated data
  118. LPSFOLDER tmpFolder;
  119. int iListCount = GetCount();
  120. for (int x = 0; x < iListCount; x++)
  121. {
  122. tmpFolder = (LPSFOLDER)GetItemDataPtr(x);
  123. if (tmpFolder)
  124. delete tmpFolder;
  125. }
  126. ResetContent();
  127. }