ResizableGrip.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // This file is part of ResizableLib
  4. // https://github.com/ppescher/resizablelib
  5. //
  6. // Copyright (C) 2000-2015 by Paolo Messina
  7. // mailto:ppescher@hotmail.com
  8. //
  9. // The contents of this file are subject to the Artistic License 2.0
  10. // http://opensource.org/licenses/Artistic-2.0
  11. //
  12. // If you find this code useful, credits would be nice!
  13. //
  14. /////////////////////////////////////////////////////////////////////////////
  15. /*!
  16. * @file
  17. * @brief Implementation of the CResizableGrip class.
  18. */
  19. #include "stdafx.h"
  20. #include "ResizableGrip.h"
  21. #ifdef _DEBUG
  22. #undef THIS_FILE
  23. static char THIS_FILE[]=__FILE__;
  24. #define new DEBUG_NEW
  25. #endif
  26. //////////////////////////////////////////////////////////////////////
  27. // Construction/Destruction
  28. //////////////////////////////////////////////////////////////////////
  29. CResizableGrip::CResizableGrip()
  30. {
  31. m_nShowCount = 0;
  32. }
  33. CResizableGrip::~CResizableGrip()
  34. {
  35. }
  36. void CResizableGrip::UpdateSizeGrip()
  37. {
  38. if (!::IsWindow(m_wndGrip.m_hWnd))
  39. return;
  40. // size-grip goes bottom right in the client area
  41. // (any right-to-left adjustment should go here)
  42. RECT rect;
  43. GetResizableWnd()->GetClientRect(&rect);
  44. rect.left = rect.right - m_wndGrip.m_size.cx;
  45. rect.top = rect.bottom - m_wndGrip.m_size.cy;
  46. // must stay below other children
  47. m_wndGrip.SetWindowPos(&CWnd::wndBottom, rect.left, rect.top, 0, 0,
  48. SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOREPOSITION
  49. | (IsSizeGripVisible() ? SWP_SHOWWINDOW : SWP_HIDEWINDOW));
  50. }
  51. // pbStatus points to a variable, maintained by the caller, that
  52. // holds its visibility status. Initialize the variable with 1
  53. // to allow to temporarily hide the grip, 0 to allow to
  54. // temporarily show the grip (with respect to the dwMask bit).
  55. // NB: visibility is effective only after an update
  56. void CResizableGrip::ShowSizeGrip(DWORD* pStatus, DWORD dwMask /*= 1*/)
  57. {
  58. ASSERT(pStatus != NULL);
  59. if (!(*pStatus & dwMask))
  60. {
  61. m_nShowCount++;
  62. (*pStatus) |= dwMask;
  63. }
  64. }
  65. void CResizableGrip::HideSizeGrip(DWORD* pStatus, DWORD dwMask /*= 1*/)
  66. {
  67. ASSERT(pStatus != NULL);
  68. if (*pStatus & dwMask)
  69. {
  70. m_nShowCount--;
  71. (*pStatus) &= ~dwMask;
  72. }
  73. }
  74. BOOL CResizableGrip::IsSizeGripVisible()
  75. {
  76. // NB: visibility is effective only after an update
  77. return (m_nShowCount > 0);
  78. }
  79. void CResizableGrip::SetSizeGripVisibility(BOOL bVisible)
  80. {
  81. if (bVisible)
  82. m_nShowCount = 1;
  83. else
  84. m_nShowCount = 0;
  85. }
  86. BOOL CResizableGrip::SetSizeGripBkMode(int nBkMode)
  87. {
  88. if (::IsWindow(m_wndGrip.m_hWnd))
  89. {
  90. if (nBkMode == OPAQUE)
  91. m_wndGrip.SetTransparency(FALSE);
  92. else if (nBkMode == TRANSPARENT)
  93. m_wndGrip.SetTransparency(TRUE);
  94. else
  95. return FALSE;
  96. return TRUE;
  97. }
  98. return FALSE;
  99. }
  100. void CResizableGrip::SetSizeGripShape(BOOL bTriangular)
  101. {
  102. if (::IsWindow(m_wndGrip.m_hWnd))
  103. m_wndGrip.SetTriangularShape(bTriangular);
  104. }
  105. BOOL CResizableGrip::CreateSizeGrip(BOOL bVisible /*= TRUE*/,
  106. BOOL bTriangular /*= TRUE*/, BOOL bTransparent /*= FALSE*/)
  107. {
  108. // create grip
  109. CRect rect(0 , 0, m_wndGrip.m_size.cx, m_wndGrip.m_size.cy);
  110. BOOL bRet = m_wndGrip.Create(WS_CHILD | WS_CLIPSIBLINGS
  111. | SBS_SIZEGRIP, rect, GetResizableWnd(), 0);
  112. if (bRet)
  113. {
  114. // set options
  115. m_wndGrip.SetTriangularShape(bTriangular);
  116. m_wndGrip.SetTransparency(bTransparent);
  117. SetSizeGripVisibility(bVisible);
  118. // update position
  119. UpdateSizeGrip();
  120. }
  121. return bRet;
  122. }
  123. /////////////////////////////////////////////////////////////////////////////
  124. // CSizeGrip implementation
  125. BOOL CResizableGrip::CSizeGrip::IsRTL()
  126. {
  127. return GetExStyle() & WS_EX_LAYOUTRTL;
  128. }
  129. BOOL CResizableGrip::CSizeGrip::PreCreateWindow(CREATESTRUCT& cs)
  130. {
  131. // set window size
  132. m_size.cx = GetSystemMetrics(SM_CXVSCROLL);
  133. m_size.cy = GetSystemMetrics(SM_CYHSCROLL);
  134. cs.cx = m_size.cx;
  135. cs.cy = m_size.cy;
  136. return CScrollBar::PreCreateWindow(cs);
  137. }
  138. LRESULT CResizableGrip::CSizeGrip::WindowProc(UINT message,
  139. WPARAM wParam, LPARAM lParam)
  140. {
  141. switch (message)
  142. {
  143. case WM_GETDLGCODE:
  144. // fix to prevent the control to gain focus, using arrow keys
  145. // (standard grip returns DLGC_WANTARROWS, like any standard scrollbar)
  146. return DLGC_STATIC;
  147. case WM_SETFOCUS:
  148. // fix to prevent the control to gain focus, if set directly
  149. // (for example when it's the only one control in a dialog)
  150. return 0;
  151. case WM_NCHITTEST:
  152. // choose proper cursor shape
  153. if (IsRTL())
  154. return HTBOTTOMLEFT;
  155. else
  156. return HTBOTTOMRIGHT;
  157. break;
  158. case WM_SETTINGCHANGE:
  159. {
  160. // update grip's size
  161. CSize sizeOld = m_size;
  162. m_size.cx = GetSystemMetrics(SM_CXVSCROLL);
  163. m_size.cy = GetSystemMetrics(SM_CYHSCROLL);
  164. // resize transparency bitmaps
  165. if (m_bTransparent)
  166. {
  167. CClientDC dc(this);
  168. // destroy bitmaps
  169. m_bmGrip.DeleteObject();
  170. m_bmMask.DeleteObject();
  171. // re-create bitmaps
  172. m_bmGrip.CreateCompatibleBitmap(&dc, m_size.cx, m_size.cy);
  173. m_bmMask.CreateBitmap(m_size.cx, m_size.cy, 1, 1, NULL);
  174. }
  175. // re-calc shape
  176. if (m_bTriangular)
  177. SetTriangularShape(m_bTriangular);
  178. // reposition the grip
  179. CRect rect;
  180. GetWindowRect(rect);
  181. rect.InflateRect(m_size.cx - sizeOld.cx, m_size.cy - sizeOld.cy, 0, 0);
  182. ::MapWindowPoints(NULL, GetParent()->GetSafeHwnd(), (LPPOINT)&rect, 2);
  183. MoveWindow(rect, TRUE);
  184. }
  185. break;
  186. case WM_DESTROY:
  187. // perform clean up
  188. if (m_bTransparent)
  189. SetTransparency(FALSE);
  190. break;
  191. case WM_PAINT:
  192. case WM_PRINTCLIENT:
  193. if (m_bTransparent)
  194. {
  195. PAINTSTRUCT ps;
  196. CDC* pDC = (message == WM_PAINT && wParam == 0) ?
  197. BeginPaint(&ps) : CDC::FromHandle((HDC)wParam);
  198. // select bitmaps
  199. CBitmap *pOldGrip, *pOldMask;
  200. pOldGrip = m_dcGrip.SelectObject(&m_bmGrip);
  201. pOldMask = m_dcMask.SelectObject(&m_bmMask);
  202. // obtain original grip bitmap, make the mask and prepare masked bitmap
  203. CScrollBar::WindowProc(message, (WPARAM)m_dcGrip.GetSafeHdc(), lParam);
  204. m_dcGrip.SetBkColor(m_dcGrip.GetPixel(0, 0));
  205. m_dcMask.BitBlt(0, 0, m_size.cx, m_size.cy, &m_dcGrip, 0, 0, SRCCOPY);
  206. m_dcGrip.BitBlt(0, 0, m_size.cx, m_size.cy, &m_dcMask, 0, 0, 0x00220326);
  207. // draw transparently
  208. pDC->BitBlt(0, 0, m_size.cx, m_size.cy, &m_dcMask, 0, 0, SRCAND);
  209. pDC->BitBlt(0, 0, m_size.cx, m_size.cy, &m_dcGrip, 0, 0, SRCPAINT);
  210. // unselect bitmaps
  211. m_dcGrip.SelectObject(pOldGrip);
  212. m_dcMask.SelectObject(pOldMask);
  213. if (message == WM_PAINT && wParam == 0)
  214. EndPaint(&ps);
  215. return 0;
  216. }
  217. break;
  218. }
  219. return CScrollBar::WindowProc(message, wParam, lParam);
  220. }
  221. void CResizableGrip::CSizeGrip::SetTransparency(BOOL bActivate)
  222. {
  223. // creates or deletes DCs and Bitmaps used for
  224. // implementing a transparent size grip
  225. if (bActivate && !m_bTransparent)
  226. {
  227. m_bTransparent = TRUE;
  228. CClientDC dc(this);
  229. // create memory DCs and bitmaps
  230. m_dcGrip.CreateCompatibleDC(&dc);
  231. m_bmGrip.CreateCompatibleBitmap(&dc, m_size.cx, m_size.cy);
  232. m_dcMask.CreateCompatibleDC(&dc);
  233. m_bmMask.CreateBitmap(m_size.cx, m_size.cy, 1, 1, NULL);
  234. }
  235. else if (!bActivate && m_bTransparent)
  236. {
  237. m_bTransparent = FALSE;
  238. // destroy memory DCs and bitmaps
  239. m_dcGrip.DeleteDC();
  240. m_bmGrip.DeleteObject();
  241. m_dcMask.DeleteDC();
  242. m_bmMask.DeleteObject();
  243. }
  244. }
  245. void CResizableGrip::CSizeGrip::SetTriangularShape(BOOL bEnable)
  246. {
  247. m_bTriangular = bEnable;
  248. if (bEnable)
  249. {
  250. // set a triangular window region
  251. CRect rect;
  252. GetWindowRect(rect);
  253. rect.OffsetRect(-rect.TopLeft());
  254. POINT arrPoints[] =
  255. {
  256. { rect.left, rect.bottom },
  257. { rect.right, rect.bottom },
  258. { rect.right, rect.top }
  259. };
  260. CRgn rgnGrip;
  261. rgnGrip.CreatePolygonRgn(arrPoints, 3, WINDING);
  262. SetWindowRgn((HRGN)rgnGrip.Detach(), IsWindowVisible());
  263. }
  264. else
  265. {
  266. SetWindowRgn((HRGN)NULL, IsWindowVisible());
  267. }
  268. }