SkinListCtrl.cpp
上传用户:sgmlaoniu
上传日期:2022-06-15
资源大小:28k
文件大小:7k
源码类别:

ListView/ListBox

开发平台:

Visual C++

  1. // SkinListCtrl.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "SkinListCtrl.h"
  5. #ifdef _DEBUG
  6. #define new DEBUG_NEW
  7. #undef THIS_FILE
  8. static char THIS_FILE[] = __FILE__;
  9. #endif
  10. /////////////////////////////////////////////////////////////////////////////
  11. // CSkinListCtrl
  12. CSkinListCtrl::CSkinListCtrl()
  13. {
  14. g_MyClrBgHi = RGB(115,123,165);
  15. g_MyClrFgHi = RGB(229,229,229);
  16. }
  17. CSkinListCtrl::~CSkinListCtrl()
  18. {
  19. }
  20. BEGIN_MESSAGE_MAP(CSkinListCtrl, CListCtrl)
  21. //{{AFX_MSG_MAP(CSkinListCtrl)
  22. ON_WM_NCCALCSIZE()
  23. ON_WM_MOUSEWHEEL()
  24. ON_WM_KEYDOWN()
  25. ON_WM_KEYUP()
  26. ON_WM_ERASEBKGND()
  27. ON_WM_PAINT()
  28. //}}AFX_MSG_MAP
  29. ON_NOTIFY_REFLECT ( NM_CUSTOMDRAW, OnCustomDrawList )
  30. END_MESSAGE_MAP()
  31. /////////////////////////////////////////////////////////////////////////////
  32. // CSkinListCtrl message handlers
  33. void CSkinListCtrl::PreSubclassWindow() 
  34. {
  35. //use our custom CHeaderCtrl as long as there
  36. //is a headerctrl object to subclass
  37. if(GetHeaderCtrl())
  38. m_SkinHeaderCtrl.SubclassWindow(GetHeaderCtrl()->m_hWnd);
  39. CListCtrl::PreSubclassWindow();
  40. }
  41. void CSkinListCtrl::OnCustomDrawList ( NMHDR* pNMHDR, LRESULT* pResult )
  42. {
  43. NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );
  44. static bool bHighlighted = false;
  45.     *pResult = CDRF_DODEFAULT;
  46.     if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
  47. {
  48.         *pResult = CDRF_NOTIFYITEMDRAW;
  49. }
  50.     else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
  51. {
  52.         int iRow = (int)pLVCD->nmcd.dwItemSpec;
  53. bHighlighted = IsRowHighlighted(m_hWnd, iRow);
  54. if (bHighlighted)
  55. {
  56. pLVCD->clrText   = g_MyClrFgHi; // Use my foreground hilite color
  57. pLVCD->clrTextBk = g_MyClrBgHi; // Use my background hilite color
  58. EnableHighlighting(m_hWnd, iRow, false);
  59. }
  60. *pResult = CDRF_DODEFAULT | CDRF_NOTIFYPOSTPAINT;
  61. }
  62. else if(CDDS_ITEMPOSTPAINT == pLVCD->nmcd.dwDrawStage)
  63. {
  64. if (bHighlighted)
  65.       {
  66.         int  iRow = (int)pLVCD->nmcd.dwItemSpec;
  67.         EnableHighlighting(m_hWnd, iRow, true);
  68.       }
  69.       *pResult = CDRF_DODEFAULT;
  70. }
  71. }
  72. void CSkinListCtrl::EnableHighlighting(HWND hWnd, int row, bool bHighlight)
  73. {
  74.   ListView_SetItemState(hWnd, row, bHighlight? 0xff: 0, LVIS_SELECTED);
  75. }
  76. bool CSkinListCtrl::IsRowSelected(HWND hWnd, int row)
  77. {
  78.   return ListView_GetItemState(hWnd, row, LVIS_SELECTED) != 0;
  79. }
  80. bool CSkinListCtrl::IsRowHighlighted(HWND hWnd, int row)
  81. {
  82.   return IsRowSelected(hWnd, row) /*&& (::GetFocus() == hWnd)*/;
  83. }
  84. void CSkinListCtrl::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS FAR* lpncsp) 
  85. {
  86. UpdateWindow();
  87. CListCtrl::OnNcCalcSize(bCalcValidRects, lpncsp);
  88. }
  89. BOOL CSkinListCtrl::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt) 
  90. {
  91. m_SkinVerticleScrollbar.UpdateThumbPosition();
  92. m_SkinHorizontalScrollbar.UpdateThumbPosition();
  93. return CListCtrl::OnMouseWheel(nFlags, zDelta, pt);
  94. }
  95. void CSkinListCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
  96. {
  97. m_SkinVerticleScrollbar.UpdateThumbPosition();
  98. m_SkinHorizontalScrollbar.UpdateThumbPosition();
  99. CListCtrl::OnKeyDown(nChar, nRepCnt, nFlags);
  100. }
  101. void CSkinListCtrl::Init()
  102. {
  103. //another way to hide scrollbars
  104. InitializeFlatSB(m_hWnd);
  105. FlatSB_EnableScrollBar(m_hWnd, SB_BOTH, ESB_DISABLE_BOTH);
  106. CWnd* pParent = GetParent();
  107. //Create scrollbars at runtime
  108. m_SkinVerticleScrollbar.Create(NULL, WS_CHILD|SS_LEFT|SS_NOTIFY|WS_VISIBLE|WS_GROUP,CRect(0,0,0,0), pParent);
  109. m_SkinHorizontalScrollbar.Create(NULL, WS_CHILD|SS_LEFT|SS_NOTIFY|WS_VISIBLE|WS_GROUP,CRect(0,0,0,0), pParent);
  110. m_SkinVerticleScrollbar.pList = this;
  111. m_SkinHorizontalScrollbar.pList = this;
  112. //call this to position the scrollbars properly
  113. PositionScrollBars();
  114. }
  115. void CSkinListCtrl::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) 
  116. {
  117. m_SkinVerticleScrollbar.UpdateThumbPosition();
  118. m_SkinHorizontalScrollbar.UpdateThumbPosition();
  119. CListCtrl::OnKeyUp(nChar, nRepCnt, nFlags);
  120. }
  121. BOOL CSkinListCtrl::OnEraseBkgnd(CDC* pDC) 
  122. {
  123. m_SkinVerticleScrollbar.UpdateThumbPosition();
  124. m_SkinHorizontalScrollbar.UpdateThumbPosition();
  125. return FALSE;
  126. //return CListCtrl::OnEraseBkgnd(pDC);
  127. }
  128. void CSkinListCtrl::OnPaint() 
  129. {
  130. CPaintDC dc(this);
  131. CRect rect;
  132. GetClientRect(&rect);
  133. CMemDC memDC(&dc, rect);
  134. //funky code to allow use to double buffer
  135. //the onpaint calls for flicker free drawing
  136. //of the list items
  137. CRect headerRect;
  138. GetDlgItem(0)->GetWindowRect(&headerRect);
  139. ScreenToClient(&headerRect);
  140. dc.ExcludeClipRect(&headerRect);
  141.    
  142.    
  143. CRect clip;
  144. memDC.GetClipBox(&clip);
  145. memDC.FillSolidRect(clip, RGB(76,85,118));
  146.    
  147. SetTextBkColor(RGB(76,85,118));
  148.    
  149. m_SkinVerticleScrollbar.UpdateThumbPosition();
  150. m_SkinHorizontalScrollbar.UpdateThumbPosition();
  151.    
  152.    
  153. DefWindowProc(WM_PAINT, (WPARAM)memDC->m_hDC, (LPARAM)0);
  154. }
  155. void CSkinListCtrl::PositionScrollBars()
  156. {
  157. //Thanks goes to mindows for this function
  158. //he posted on the message forums. He modified
  159. //it a bit based on the original init function,
  160. //and now I have modified his version a tiny bit ;)
  161. //The pParent->ScreenToClient that you did made it
  162. //possible for me to make the scrollbars position
  163. //properly based on any dialog size/borders/titlebar etc... :D
  164. CWnd* pParent = GetParent();
  165. CRect windowRect;
  166. GetWindowRect(&windowRect);
  167. int nTitleBarHeight = 0;
  168. if(pParent->GetStyle() & WS_CAPTION)
  169. nTitleBarHeight = GetSystemMetrics(SM_CYSIZE);
  170. int nDialogFrameHeight = 0;
  171. int nDialogFrameWidth = 0;
  172. if((pParent->GetStyle() & WS_BORDER))
  173. {
  174. nDialogFrameHeight = GetSystemMetrics(SM_CYDLGFRAME);
  175. nDialogFrameWidth = GetSystemMetrics(SM_CYDLGFRAME);
  176. }
  177. if(pParent->GetStyle() & WS_THICKFRAME)
  178. {
  179. nDialogFrameHeight+=1;
  180. nDialogFrameWidth+=1;
  181. }
  182. pParent->ScreenToClient(&windowRect);
  183. windowRect.top+=nTitleBarHeight+nDialogFrameHeight;
  184. windowRect.bottom+=nTitleBarHeight+nDialogFrameHeight;
  185. windowRect.left +=nDialogFrameWidth;
  186. windowRect.right+=nDialogFrameWidth;
  187. CRect vBar(windowRect.right-nDialogFrameWidth,windowRect.top-nTitleBarHeight-nDialogFrameHeight,windowRect.right+12-nDialogFrameWidth,windowRect.bottom+12-nTitleBarHeight-nDialogFrameHeight);
  188. CRect hBar(windowRect.left-nDialogFrameWidth,windowRect.bottom-nTitleBarHeight-nDialogFrameHeight,windowRect.right+1-nDialogFrameWidth,windowRect.bottom+12-nTitleBarHeight-nDialogFrameHeight);
  189. m_SkinVerticleScrollbar.SetWindowPos(NULL,vBar.left,vBar.top,vBar.Width(),vBar.Height(),SWP_NOZORDER);
  190. m_SkinHorizontalScrollbar.SetWindowPos(NULL,hBar.left,hBar.top,hBar.Width(),hBar.Height(),SWP_NOZORDER);
  191. m_SkinHorizontalScrollbar.UpdateThumbPosition();
  192. m_SkinVerticleScrollbar.UpdateThumbPosition();
  193. }