MyColorList.cpp
上传用户:jzscgs158
上传日期:2022-05-25
资源大小:8709k
文件大小:5k
源码类别:

百货/超市行业

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "MyColorList.h"
  3. #ifdef _DEBUG
  4. #define new DEBUG_NEW
  5. #undef THIS_FILE
  6. static char THIS_FILE[] = __FILE__;
  7. #endif
  8. CMyColorList::CMyColorList()
  9. {
  10. m_blnSelect=TRUE;
  11. }
  12. //***************************************************************
  13. CMyColorList::~CMyColorList()
  14. {
  15. m_ArrayCol.FreeExtra();
  16. m_ArrayHeaderAlign.FreeExtra();
  17. }
  18. //***************************************************************
  19. BEGIN_MESSAGE_MAP(CMyColorList, CListCtrl)
  20. //{{AFX_MSG_MAP(CMyColorList)
  21. ON_WM_PAINT()
  22. ON_WM_DESTROY()
  23. ON_WM_KILLFOCUS()
  24. ON_WM_SETFOCUS()
  25. ON_WM_SIZE()
  26. //}}AFX_MSG_MAP
  27. END_MESSAGE_MAP()
  28. void CMyColorList::PreSubclassWindow()
  29. {
  30. ModifyStyle(0,LVS_OWNERDRAWFIXED);
  31. }
  32. void CMyColorList::DeleteAllItems()
  33. {
  34. m_ArrayCol.RemoveAll();
  35. CListCtrl::DeleteAllItems();
  36. }
  37. void CMyColorList::DeleteItem(int nItem)
  38. {
  39. m_ArrayCol.RemoveAt(nItem);
  40. CListCtrl::DeleteItem(nItem);
  41. }
  42. void CMyColorList::OnDestroy() 
  43. {
  44. CListCtrl::OnDestroy();
  45. }
  46. void CMyColorList::InsertColumn(int nCol, LPCTSTR lpszColumnHeading, int nFormat , int nWidth , int nSubItem )
  47. {
  48. CString strText;
  49. strText.Format("%d",nFormat);
  50. m_ArrayHeaderAlign.InsertAt(nCol,strText);
  51. CListCtrl::InsertColumn(nCol,lpszColumnHeading,nFormat,nWidth,nSubItem);
  52. }
  53. //设置列头
  54. void CMyColorList::SetHeader()
  55. {
  56. VERIFY( m_ctlHeader.SubclassWindow( GetHeaderCtrl()->GetSafeHwnd() ) );
  57. m_ctlHeader.SetDefault();
  58. }
  59. int CMyColorList::AddItem(int image,COLORREF colText)
  60. {
  61. int m=GetItemCount();
  62. CMyColor *col=new CMyColor();
  63. col->colBack=GetBkColor();
  64. col->colText=colText;
  65. col->colSelect=RGB(52,148,39);
  66. m_ArrayCol.InsertAt(m,(CObject *)col);
  67. return CListCtrl::InsertItem(m,"",image);
  68. }
  69. void CMyColorList::SetItemColor(int nitem,COLORREF colText,COLORREF colBack,COLORREF colSelect)
  70. {
  71. CMyColor *col=(CMyColor *)m_ArrayCol.GetAt(nitem);
  72. col->colBack=colBack;
  73. col->colText=colText;
  74. col->colSelect=colSelect;
  75. }
  76. void CMyColorList::SetItemColor(int nitem,COLORREF colText)
  77. {
  78. CMyColor *col=(CMyColor *)m_ArrayCol.GetAt(nitem);
  79. col->colText=colText;
  80. }
  81. CMyColor CMyColorList::GetItemColor(int nitem)
  82. {
  83. CMyColor *col=(CMyColor *)m_ArrayCol.GetAt(nitem);
  84. return (*col);
  85. }
  86. int CMyColorList::AddItem(int image,COLORREF colText,COLORREF colBack,COLORREF colSelect)
  87. {
  88. int m=GetItemCount();
  89. CMyColor *col=new CMyColor();
  90. col->colBack=colBack;
  91. col->colText=colText;
  92. col->colSelect=colSelect;
  93. m_ArrayCol.InsertAt(m,(CObject *)col);
  94. return CListCtrl::InsertItem(m,"",image);
  95. }
  96. //***************************************************************
  97. void CMyColorList::OnPaint() 
  98. {
  99. CListCtrl::OnPaint();
  100. }
  101. //***************************************************************
  102. void CMyColorList::OnKillFocus(CWnd* pNewWnd) 
  103. {
  104.     CListCtrl::OnKillFocus(pNewWnd);
  105. }
  106. //***************************************************************
  107. void CMyColorList::OnSetFocus(CWnd* pOldWnd) 
  108. {
  109. CListCtrl::OnSetFocus(pOldWnd);
  110. }
  111. //***************************************************************
  112. void CMyColorList::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
  113. {
  114.     if (!lpDrawItemStruct) return;
  115. CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
  116. int m=GetHeaderCtrl()->GetItemCount();
  117. for(int i=0;i<m;i++)
  118. {
  119. CString str;
  120. CRect rcItem;
  121. GetSubItemRect(lpDrawItemStruct->itemID,i,LVIR_LABEL,rcItem);
  122. str=GetItemText(lpDrawItemStruct->itemID,i);
  123. CMyColor *col=(CMyColor *)m_ArrayCol.GetAt(lpDrawItemStruct->itemID);
  124. pDC->SetBkMode(TRANSPARENT);
  125. CBrush brush;
  126. brush.CreateSolidBrush(col->colBack);
  127. pDC->FillRect(rcItem,&brush);
  128. pDC->SetTextColor(col->colText);
  129. LV_ITEM lvi;
  130. lvi.mask = LVIF_IMAGE | LVIF_STATE;
  131. lvi.iItem = lpDrawItemStruct->itemID;
  132. lvi.iSubItem = 0;
  133. lvi.stateMask = 0xFFFF; // get all state flags
  134. GetItem(&lvi);
  135. if((lvi.state & LVIS_SELECTED)||(lvi.state & LVIS_FOCUSED))
  136. {
  137. if(m_blnSelect)
  138. {
  139. int r1=GetRValue(col->colSelect);
  140. int g1=GetGValue(col->colSelect);
  141. int b1=GetBValue(col->colSelect);
  142. for(int i=rcItem.Height()/2;i>0;i--)
  143. {
  144. r1=(r1+5)>255?255:(r1+5);
  145. g1=(g1+5)>255?255:(g1+5);
  146. b1=(b1+5)>255?255:(b1+5);
  147. CPen pen(PS_SOLID, 1, RGB(r1, g1, b1));
  148. CPen *old = pDC->SelectObject(&pen);
  149. pDC->MoveTo(rcItem.left,rcItem.top+i);
  150. pDC->LineTo(rcItem.right,rcItem.top+i);
  151. pDC->MoveTo(rcItem.left,rcItem.bottom-i);
  152. pDC->LineTo(rcItem.right,rcItem.bottom-i);
  153. pDC->SelectObject(old);
  154. }
  155. }
  156. }
  157. int nAlign=atoi(m_ArrayHeaderAlign.GetAt(i));
  158. if(i==0&&GetImageList(LVSIL_SMALL)!=NULL)
  159. {
  160. CPoint ptImage;
  161. ptImage.x=rcItem.left;
  162. ptImage.y=rcItem.top;
  163. GetImageList(LVSIL_SMALL)->Draw(pDC,lvi.iImage,ptImage,ILD_TRANSPARENT);
  164. pDC->SetBkMode(TRANSPARENT);
  165. rcItem.left=rcItem.left+16;
  166. if(nAlign==LVCFMT_LEFT)
  167. rcItem.left=rcItem.left+8;
  168. }
  169. switch(nAlign)
  170. {
  171. case LVCFMT_LEFT:
  172. pDC->DrawText(str,rcItem,DT_LEFT);
  173. break;
  174. case LVCFMT_CENTER:
  175. pDC->DrawText(str,rcItem,DT_CENTER);
  176. break;
  177. case LVCFMT_RIGHT:
  178. pDC->DrawText(str,rcItem,DT_RIGHT);
  179. break;
  180. default:
  181. pDC->DrawText(str,rcItem,DT_CENTER);
  182. break;
  183. }
  184. }
  185. }
  186. void CMyColorList::InsertItem(int nItem, LPCTSTR lpszItem, int nImage)
  187. {
  188. int m=GetItemCount();
  189. CMyColor *col=new CMyColor();
  190. col->colBack=RGB(255,255,255);
  191. col->colText=RGB(0,0,0);
  192. col->colSelect=RGB(52,148,39);
  193. m_ArrayCol.InsertAt(m,(CObject *)col);
  194. CListCtrl::InsertItem(nItem,lpszItem,nImage);
  195. }