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

ListView/ListBox

开发平台:

Visual C++

  1. // SkinHorizontalScrollbar.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "SkinList.h"
  5. #include "SkinHorizontalScrollbar.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CSkinHorizontalScrollbar
  13. CSkinHorizontalScrollbar::CSkinHorizontalScrollbar()
  14. {
  15. nThumbLeft = 25;
  16. dbThumbRemainder = 0.00f;
  17. bMouseDown = false;
  18. bMouseDownArrowLeft = false;
  19. bMouseDownArrowRight = false;
  20. bDragging = false;
  21. pList = NULL;
  22. }
  23. CSkinHorizontalScrollbar::~CSkinHorizontalScrollbar()
  24. {
  25. }
  26. BEGIN_MESSAGE_MAP(CSkinHorizontalScrollbar, CStatic)
  27. //{{AFX_MSG_MAP(CSkinHorizontalScrollbar)
  28. ON_WM_PAINT()
  29. ON_WM_LBUTTONDOWN()
  30. ON_WM_LBUTTONUP()
  31. ON_WM_MOUSEMOVE()
  32. ON_WM_TIMER()
  33. //}}AFX_MSG_MAP
  34. END_MESSAGE_MAP()
  35. /////////////////////////////////////////////////////////////////////////////
  36. // CSkinHorizontalScrollbar message handlers
  37. void CSkinHorizontalScrollbar::OnPaint() 
  38. {
  39. CPaintDC dc(this); // device context for painting
  40. Draw();
  41. }
  42. void CSkinHorizontalScrollbar::OnLButtonDown(UINT nFlags, CPoint point) 
  43. {
  44. SetCapture();
  45. CRect clientRect;
  46. GetClientRect(&clientRect);
  47. int nWidth = clientRect.Width()-26;
  48. CRect rectLeftArrow(0,0,26,20);
  49. CRect rectRightArrow(nWidth,0,nWidth+26,20);
  50. CRect rectThumb(nThumbLeft,0,nThumbLeft+26,20);
  51. if(rectThumb.PtInRect(point))
  52. {
  53. bMouseDown = true;
  54. }
  55. if(rectRightArrow.PtInRect(point))
  56. {
  57. bMouseDownArrowRight = true;
  58. SetTimer(2,250,NULL);
  59. }
  60. if(rectLeftArrow.PtInRect(point))
  61. {
  62. bMouseDownArrowLeft = true;
  63. SetTimer(2,250,NULL);
  64. }
  65. CStatic::OnLButtonDown(nFlags, point);
  66. }
  67. void CSkinHorizontalScrollbar::OnLButtonUp(UINT nFlags, CPoint point) 
  68. {
  69. UpdateThumbPosition();
  70. KillTimer(1);
  71. ReleaseCapture();
  72. bool bInChannel = true;
  73. CRect clientRect;
  74. GetClientRect(&clientRect);
  75. int nWidth = clientRect.Width()-26;
  76. CRect rectLeftArrow(0,0,26,20);
  77. CRect rectThumb(nThumbLeft,0,nThumbLeft+26,20);
  78. if(rectLeftArrow.PtInRect(point))
  79. {
  80. ScrollLeft();
  81. bInChannel = false;
  82. }
  83. CRect rectRightArrow(nWidth,0,nWidth+26,20);
  84. if(rectRightArrow.PtInRect(point))
  85. {
  86. ScrollRight();
  87. bInChannel = false;
  88. }
  89. if(rectThumb.PtInRect(point))
  90. {
  91. bInChannel = false;
  92. }
  93. if(bInChannel == true && !bMouseDown)
  94. {
  95. if(point.x > nThumbLeft)
  96. {
  97. PageRight();
  98. }
  99. else
  100. {
  101. PageLeft();
  102. }
  103. }
  104. //reset all variables
  105. bMouseDown = false;
  106. bDragging = false;
  107. bMouseDownArrowLeft = false;
  108. bMouseDownArrowRight = false;
  109. CStatic::OnLButtonUp(nFlags, point);
  110. }
  111. void CSkinHorizontalScrollbar::OnMouseMove(UINT nFlags, CPoint point) 
  112. {
  113. CRect clientRect;
  114. GetClientRect(&clientRect);
  115. if(bMouseDown)
  116. bDragging = true;
  117. if(bDragging)
  118. {
  119. nThumbLeft = point.x-13; //-13 so mouse is in middle of thumb
  120. double nMax = pList->GetScrollLimit(SB_HORZ);
  121. int nPos = pList->GetScrollPos(SB_HORZ);
  122. double nWidth = clientRect.Width()-75;
  123. double nVar = nMax;
  124. dbThumbInterval = nWidth/nVar;
  125. //figure out how many times to scroll total from top
  126. //then minus the current position from it
  127. int nScrollTimes = (int)((nThumbLeft-25)/dbThumbInterval)-nPos;
  128. CSize size;
  129. size.cx = nScrollTimes;
  130. size.cy = 0;
  131. pList->Scroll(size);
  132. LimitThumbPosition();
  133. Draw();
  134. }
  135. CStatic::OnMouseMove(nFlags, point);
  136. }
  137. void CSkinHorizontalScrollbar::OnTimer(UINT nIDEvent) 
  138. {
  139. if(nIDEvent == 1)
  140. {
  141. if(bMouseDownArrowRight)
  142. {
  143. ScrollRight();
  144. }
  145. if(bMouseDownArrowLeft)
  146. {
  147. ScrollLeft();
  148. }
  149. }
  150. else if(nIDEvent == 2)
  151. {
  152. if(bMouseDownArrowRight)
  153. {
  154. KillTimer(2);
  155. SetTimer(1, 50, NULL);
  156. }
  157. if(bMouseDownArrowLeft)
  158. {
  159. KillTimer(2);
  160. SetTimer(1, 50, NULL);
  161. }
  162. }
  163. CStatic::OnTimer(nIDEvent);
  164. }
  165. void CSkinHorizontalScrollbar::ScrollLeft()
  166. {
  167. pList->SendMessage(WM_HSCROLL, MAKELONG(SB_LINELEFT,0),NULL);
  168. UpdateThumbPosition();
  169. }
  170. void CSkinHorizontalScrollbar::ScrollRight()
  171. {
  172. pList->SendMessage(WM_HSCROLL, MAKELONG(SB_LINERIGHT,0),NULL);
  173. UpdateThumbPosition();
  174. }
  175. void CSkinHorizontalScrollbar::UpdateThumbPosition()
  176. {
  177. CRect clientRect;
  178. GetClientRect(&clientRect);
  179. double nPos = pList->GetScrollPos(SB_HORZ);
  180. double nMax = pList->GetScrollLimit(SB_HORZ);
  181. double nWidth = clientRect.Width()-75; 
  182. double nVar = nMax;
  183. dbThumbInterval = nWidth/nVar;
  184. double nNewdbValue = dbThumbInterval * (nPos);
  185. int nNewValue = (int)nNewdbValue;
  186. double nExtra = nNewdbValue - nNewValue;
  187. dbThumbRemainder = nExtra;
  188. nThumbLeft = 25+nNewValue;
  189. LimitThumbPosition();
  190. Draw();
  191. }
  192. void CSkinHorizontalScrollbar::PageRight()
  193. {
  194. pList->SendMessage(WM_HSCROLL, MAKELONG(SB_PAGEDOWN,0),NULL);
  195. UpdateThumbPosition();
  196. }
  197. void CSkinHorizontalScrollbar::PageLeft()
  198. {
  199. pList->SendMessage(WM_HSCROLL, MAKELONG(SB_PAGEUP,0),NULL);
  200. UpdateThumbPosition();
  201. }
  202. void CSkinHorizontalScrollbar::Draw()
  203. {
  204. CClientDC dc(this);
  205. CRect clientRect;
  206. GetClientRect(&clientRect);
  207. CMemDC memDC(&dc, &clientRect);
  208. memDC.FillSolidRect(&clientRect,  RGB(76,85,118));
  209. CDC bitmapDC;
  210. bitmapDC.CreateCompatibleDC(&dc);
  211. //draw left arrow of scrollbar
  212. CBitmap bitmap;
  213. bitmap.LoadBitmap(IDB_HORIZONTAL_SCROLLBAR_LEFTARROW);
  214. CBitmap* pOldBitmap = bitmapDC.SelectObject(&bitmap);
  215. memDC.BitBlt(clientRect.left,clientRect.top,26,12,&bitmapDC,0,0,SRCCOPY);
  216. bitmapDC.SelectObject(pOldBitmap);
  217. bitmap.DeleteObject();
  218. pOldBitmap = NULL;
  219. bitmap.LoadBitmap(IDB_HORIZONTAL_SCROLLBAR_SPAN);
  220. pOldBitmap = bitmapDC.SelectObject(&bitmap);
  221. int nWidth = clientRect.Width() - 26;
  222. memDC.StretchBlt(clientRect.left+26, clientRect.top, nWidth,12,&bitmapDC, 0,0, 1, 12, SRCCOPY);
  223. bitmapDC.SelectObject(pOldBitmap);
  224. bitmap.DeleteObject();
  225. pOldBitmap = NULL;
  226. bitmap.LoadBitmap(IDB_HORIZONTAL_SCROLLBAR_RIGHTARROW);
  227. pOldBitmap = bitmapDC.SelectObject(&bitmap);
  228. memDC.BitBlt(nWidth,clientRect.top,26,12,&bitmapDC,0,0,SRCCOPY);
  229. bitmapDC.SelectObject(pOldBitmap);
  230. bitmap.DeleteObject();
  231. pOldBitmap = NULL;
  232. //If there is nothing to scroll then don't
  233. //show the thumb control otherwise show it
  234. if(pList->GetScrollLimit(SB_HORZ) != 0)
  235. {
  236. bitmap.LoadBitmap(IDB_HORIZONTAL_SCROLLBAR_THUMB);
  237. pOldBitmap = bitmapDC.SelectObject(&bitmap);
  238. memDC.BitBlt(clientRect.left+nThumbLeft,clientRect.top,26,12,&bitmapDC,0,0,SRCCOPY);
  239. bitmapDC.SelectObject(pOldBitmap);
  240. bitmap.DeleteObject();
  241. pOldBitmap = NULL;
  242. }
  243. }
  244. void CSkinHorizontalScrollbar::LimitThumbPosition()
  245. {
  246. CRect clientRect;
  247. GetClientRect(&clientRect);
  248. if(nThumbLeft+26 > (clientRect.Width()-26))
  249. {
  250. nThumbLeft = clientRect.Width()-51;
  251. }
  252. if(nThumbLeft < (clientRect.left+25))
  253. {
  254. nThumbLeft = clientRect.left+25;
  255. }
  256. }