RoundButton.cpp
上传用户:hchw_8888
上传日期:2007-01-01
资源大小:23k
文件大小:15k
源码类别:

按钮控件

开发平台:

Visual C++

  1. // RoundButton.cpp : implementation file
  2. //
  3. // Round Buttons!
  4. //
  5. // Written by Chris Maunder (Chris.Maunder@cbr.clw.csiro.au)
  6. // Copyright (c) 1997,1998.
  7. // 
  8. // Modified: 2 Feb 1998 - Fix vis problem, CRgn resource leak,
  9. //                        button reposition code redone. CJM.
  10. //
  11. // This code may be used in compiled form in any way you desire. This
  12. // file may be redistributed unmodified by any means PROVIDING it is 
  13. // not sold for profit without the authors written consent, and 
  14. // providing that this notice and the authors name is included. If 
  15. // the source code in this file is used in any commercial application 
  16. // then a simple email would be nice.
  17. //
  18. // This file is provided "as is" with no expressed or implied warranty.
  19. // The author accepts no liability if it causes any damage to your
  20. // computer, causes your pet cat to fall ill, increases baldness or
  21. // makes you car start emitting strange noises when you start it up.
  22. //
  23. // Expect bugs.
  24. // 
  25. // Please use and enjoy. Please let me know of any bugs/mods/improvements 
  26. // that you have found/implemented and I will fix/incorporate them into this
  27. // file. 
  28. //
  29. /////////////////////////////////////////////////////////////////////////////
  30. #include "stdafx.h"
  31. #include "math.h"
  32. #include "RoundButton.h"
  33. #ifdef _DEBUG
  34. #define new DEBUG_NEW
  35. #undef THIS_FILE
  36. static char THIS_FILE[] = __FILE__;
  37. #endif
  38. // prototypes
  39. COLORREF GetColour(double dAngle, COLORREF crBright, COLORREF crDark);
  40. void DrawCircle(CDC* pDC, CPoint p, LONG lRadius, COLORREF crColour, BOOL bDashed = FALSE);
  41. void DrawCircleLeft(CDC* pDC, CPoint p, LONG lRadius, COLORREF crBright, COLORREF crDark);
  42. void DrawCircleRight(CDC* pDC, CPoint p, LONG lRadius, COLORREF crBright, COLORREF crDark);
  43. // Calculate colour for a point at the given angle by performing a linear
  44. // interpolation between the colours crBright and crDark based on the cosine
  45. // of the angle between the light source and the point.
  46. //
  47. // Angles are measured from the +ve x-axis (i.e. (1,0) = 0 degrees, (0,1) = 90 degrees )
  48. // But remember: +y points down!
  49. COLORREF GetColour(double dAngle, COLORREF crBright, COLORREF crDark)
  50. {
  51. #define Rad2Deg 180.0/3.1415 
  52. // For better light-continuity along the edge of a stretched button: 
  53. // LIGHT_SOURCE_ANGLE == -1.88
  54. //#define LIGHT_SOURCE_ANGLE -2.356 // -2.356 radians = -135 degrees, i.e. From top left
  55. #define LIGHT_SOURCE_ANGLE -1.88
  56. ASSERT(dAngle > -3.1416 && dAngle < 3.1416);
  57. double dAngleDifference = LIGHT_SOURCE_ANGLE - dAngle;
  58. if (dAngleDifference < -3.1415) dAngleDifference = 6.293 + dAngleDifference;
  59. else if (dAngleDifference > 3.1415) dAngleDifference = 6.293 - dAngleDifference;
  60. double Weight = 0.5*(cos(dAngleDifference)+1.0);
  61. BYTE Red   = (BYTE) (Weight*GetRValue(crBright) + (1.0-Weight)*GetRValue(crDark));
  62. BYTE Green = (BYTE) (Weight*GetGValue(crBright) + (1.0-Weight)*GetGValue(crDark));
  63. BYTE Blue  = (BYTE) (Weight*GetBValue(crBright) + (1.0-Weight)*GetBValue(crDark));
  64. //TRACE("LightAngle = %0.0f, Angle = %3.0f, Diff = %3.0f, Weight = %0.2f, RGB %3d,%3d,%3dn", 
  65. //   LIGHT_SOURCE_ANGLE*Rad2Deg, dAngle*Rad2Deg, dAngleDifference*Rad2Deg, Weight,Red,Green,Blue);
  66. return RGB(Red, Green, Blue);
  67. }
  68. void DrawCircle(CDC* pDC, CPoint p, LONG lRadius, COLORREF crColour, BOOL bDashed)
  69. {
  70. const int nDashLength = 1;
  71. LONG lError, lXoffset, lYoffset;
  72. int  nDash = 0;
  73. BOOL bDashOn = TRUE;
  74. //Check to see that the coordinates are valid
  75. ASSERT( (p.x + lRadius <= LONG_MAX) && (p.y + lRadius <= LONG_MAX) );
  76. ASSERT( (p.x - lRadius >= LONG_MIN) && (p.y - lRadius >= LONG_MIN) );
  77. //Set starting values
  78. lXoffset = lRadius;
  79. lYoffset = 0;
  80. lError   = -lRadius;
  81. do {
  82. if (bDashOn) {
  83. pDC->SetPixelV(p.x + lXoffset, p.y + lYoffset, crColour);
  84. pDC->SetPixelV(p.x + lXoffset, p.y - lYoffset, crColour);
  85. pDC->SetPixelV(p.x + lYoffset, p.y + lXoffset, crColour);
  86. pDC->SetPixelV(p.x + lYoffset, p.y - lXoffset, crColour);
  87. pDC->SetPixelV(p.x - lYoffset, p.y + lXoffset, crColour);
  88. pDC->SetPixelV(p.x - lYoffset, p.y - lXoffset, crColour);
  89. pDC->SetPixelV(p.x - lXoffset, p.y + lYoffset, crColour);
  90. pDC->SetPixelV(p.x - lXoffset, p.y - lYoffset, crColour);
  91. }
  92. //Advance the error term and the constant X axis step
  93. lError += lYoffset++;
  94. //Check to see if error term has overflowed
  95. if ((lError += lYoffset) >= 0)
  96. lError -= --lXoffset * 2;
  97. if (bDashed && (++nDash == nDashLength)) {
  98. nDash = 0;
  99. bDashOn = !bDashOn;
  100. }
  101. } while (lYoffset <= lXoffset); //Continue until halfway point
  102. // The original Drawcircle function is split up into DrawCircleRight and DrawCircleLeft
  103. // to make stretched buttons
  104. //
  105. void DrawCircleRight(CDC* pDC, CPoint p, LONG lRadius, COLORREF crBright, COLORREF crDark)
  106. {
  107. LONG lError, lXoffset, lYoffset;
  108. //Check to see that the coordinates are valid
  109. ASSERT( (p.x + lRadius <= LONG_MAX) && (p.y + lRadius <= LONG_MAX) );
  110. ASSERT( (p.x - lRadius >= LONG_MIN) && (p.y - lRadius >= LONG_MIN) );
  111. //Set starting values
  112. lXoffset = lRadius;
  113. lYoffset = 0;
  114. lError   = -lRadius;
  115. do {
  116. const double Pi = 3.141592654, 
  117.  Pi_on_2 = Pi * 0.5,
  118.  Three_Pi_on_2 = Pi * 1.5;
  119. COLORREF crColour;
  120. double   dAngle = atan2(lYoffset, lXoffset);
  121. //Draw the current pixel, reflected across all four arcs
  122. crColour = GetColour(dAngle, crBright, crDark);
  123. pDC->SetPixelV(p.x + lXoffset, p.y + lYoffset, crColour);
  124. crColour = GetColour(Pi_on_2 - dAngle, crBright, crDark);
  125. pDC->SetPixelV(p.x + lYoffset, p.y + lXoffset, crColour);
  126. crColour = GetColour(-Pi_on_2 + dAngle, crBright, crDark);
  127. pDC->SetPixelV(p.x + lYoffset, p.y - lXoffset, crColour);
  128. crColour = GetColour(-dAngle, crBright, crDark);
  129. pDC->SetPixelV(p.x + lXoffset, p.y - lYoffset, crColour);
  130. //Advance the error term and the constant X axis step
  131. lError += lYoffset++;
  132. //Check to see if error term has overflowed
  133. if ((lError += lYoffset) >= 0)
  134. lError -= --lXoffset * 2;
  135. } while (lYoffset <= lXoffset); //Continue until halfway point
  136. // The original Drawcircle function is split up into DrawCircleRight and DrawCircleLeft
  137. // to make stretched buttons
  138. //
  139. void DrawCircleLeft(CDC* pDC, CPoint p, LONG lRadius, COLORREF crBright, COLORREF crDark)
  140. {
  141. LONG lError, lXoffset, lYoffset;
  142. //Check to see that the coordinates are valid
  143. ASSERT( (p.x + lRadius <= LONG_MAX) && (p.y + lRadius <= LONG_MAX) );
  144. ASSERT( (p.x - lRadius >= LONG_MIN) && (p.y - lRadius >= LONG_MIN) );
  145. //Set starting values
  146. lXoffset = lRadius;
  147. lYoffset = 0;
  148. lError   = -lRadius;
  149. do {
  150. const double Pi = 3.141592654, 
  151.  Pi_on_2 = Pi * 0.5,
  152.  Three_Pi_on_2 = Pi * 1.5;
  153. COLORREF crColour;
  154. double   dAngle = atan2(lYoffset, lXoffset);
  155. //Draw the current pixel, reflected across all eight arcs
  156. crColour = GetColour(Pi_on_2 + dAngle, crBright, crDark);
  157. pDC->SetPixelV(p.x - lYoffset, p.y + lXoffset, crColour);
  158. crColour = GetColour(Pi - dAngle, crBright, crDark);
  159. pDC->SetPixelV(p.x - lXoffset, p.y + lYoffset, crColour);
  160. crColour = GetColour(-Pi + dAngle, crBright, crDark);
  161. pDC->SetPixelV(p.x - lXoffset, p.y - lYoffset, crColour);
  162. crColour = GetColour(-Pi_on_2 - dAngle, crBright, crDark);
  163. pDC->SetPixelV(p.x - lYoffset, p.y - lXoffset, crColour);
  164. //Advance the error term and the constant X axis step
  165. lError += lYoffset++;
  166. //Check to see if error term has overflowed
  167. if ((lError += lYoffset) >= 0)
  168. lError -= --lXoffset * 2;
  169. } while (lYoffset <= lXoffset); //Continue until halfway point
  170. /////////////////////////////////////////////////////////////////////////////
  171. // CRoundButton
  172. CRoundButton::CRoundButton()
  173. {
  174. m_bDrawDashedFocusCircle = TRUE;
  175. }
  176. CRoundButton::~CRoundButton()
  177. {
  178. m_rgn.DeleteObject();
  179. }
  180. BEGIN_MESSAGE_MAP(CRoundButton, CButton)
  181. //{{AFX_MSG_MAP(CRoundButton)
  182. //}}AFX_MSG_MAP
  183. END_MESSAGE_MAP()
  184. /////////////////////////////////////////////////////////////////////////////
  185. // CRoundButton message handlers
  186. void CRoundButton::PreSubclassWindow() 
  187. {
  188. CButton::PreSubclassWindow();
  189. ModifyStyle(0, BS_OWNERDRAW);
  190. CRect rect;
  191. GetClientRect(rect);
  192. // set m_bStretch if the button is not square and landscape 
  193. m_bStretch = rect.Width() > rect.Height() ? TRUE : FALSE;
  194. // Resize the window to make it square if it is not stretched
  195. if(!m_bStretch) rect.bottom = rect.right = min(rect.bottom,rect.right);
  196. // Get the vital statistics of the window
  197. // m_ptLeft/m_ptRight are the centerpoints of the left/right arcs of stretched buttons
  198. m_ptCentre = m_ptLeft = m_ptRight = rect.CenterPoint();
  199. m_nRadius  = rect.bottom/2-1;
  200. m_ptLeft.x = m_nRadius;
  201. m_ptRight.x = rect.right - m_nRadius - 1;
  202. // Set the window region so mouse clicks only activate the round section 
  203. // of the button
  204. m_rgn.DeleteObject(); 
  205. SetWindowRgn(NULL, FALSE);
  206. m_rgn.CreateEllipticRgnIndirect(rect);
  207. SetWindowRgn(m_rgn, TRUE);
  208. // Convert client coords to the parents client coords
  209. ClientToScreen(rect);
  210. CWnd* pParent = GetParent();
  211. if (pParent) pParent->ScreenToClient(rect);
  212. // Resize the window if it is not stretched
  213. if(!m_bStretch) MoveWindow(rect.left, rect.top, rect.Width(), rect.Height(), TRUE);
  214. }
  215. void CRoundButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
  216. {
  217. ASSERT(lpDrawItemStruct != NULL);
  218. CDC* pDC   = CDC::FromHandle(lpDrawItemStruct->hDC);
  219. CRect rect = lpDrawItemStruct->rcItem;
  220. UINT state = lpDrawItemStruct->itemState;
  221. UINT nStyle = GetStyle();
  222. int nRadius = m_nRadius;
  223. int nSavedDC = pDC->SaveDC();
  224. pDC->SelectStockObject(NULL_BRUSH);
  225. pDC->FillSolidRect(rect, ::GetSysColor(COLOR_BTNFACE));
  226. // Draw the focus circle around the button for non-stretched buttons
  227. if ((state & ODS_FOCUS) && m_bDrawDashedFocusCircle && !m_bStretch)
  228. DrawCircle(pDC, m_ptCentre, nRadius--, RGB(0,0,0));
  229. // Draw the raised/sunken edges of the button (unless flat)
  230. if (nStyle & BS_FLAT) {
  231. // for stretched buttons: draw left and right arcs and connect the with lines
  232. if(m_bStretch)
  233. {
  234. CPen* oldpen;
  235. CRect LeftBound(0,0,nRadius*2,nRadius*2);
  236. CRect RightBound(m_ptRight.x-nRadius,0,m_ptRight.x+nRadius,nRadius*2);
  237. oldpen = pDC->SelectObject(new CPen(PS_SOLID, 1, ::GetSysColor(COLOR_3DDKSHADOW)));
  238. pDC->Arc(LeftBound, CPoint(m_ptLeft.x,0), CPoint(m_ptLeft.x,nRadius*2));
  239. pDC->Arc(RightBound, CPoint(m_ptRight.x,nRadius*2), CPoint(m_ptRight.x,0));
  240. pDC->MoveTo(m_ptLeft.x,0); pDC->LineTo(m_ptRight.x,0);
  241. pDC->MoveTo(m_ptLeft.x,nRadius*2-1); pDC->LineTo(m_ptRight.x,nRadius*2-1);
  242. nRadius--;
  243. LeftBound.DeflateRect(1,1);
  244. RightBound.DeflateRect(1,1);
  245. delete pDC->SelectObject(new CPen(PS_SOLID, 1, ::GetSysColor(COLOR_3DHIGHLIGHT)));
  246. pDC->Arc(LeftBound, CPoint(m_ptLeft.x,1), CPoint(m_ptLeft.x,nRadius*2));
  247. pDC->Arc(RightBound, CPoint(m_ptRight.x,nRadius*2), CPoint(m_ptRight.x,0));
  248. pDC->MoveTo(m_ptLeft.x,1); pDC->LineTo(m_ptRight.x,1);
  249. pDC->MoveTo(m_ptLeft.x,nRadius*2); pDC->LineTo(m_ptRight.x,nRadius*2);
  250. delete pDC->SelectObject(oldpen);
  251. }
  252. // for non-stretched buttons: draw two circles
  253. else
  254. {
  255. DrawCircle(pDC, m_ptCentre, nRadius--, ::GetSysColor(COLOR_3DDKSHADOW));
  256. DrawCircle(pDC, m_ptCentre, nRadius--, ::GetSysColor(COLOR_3DHIGHLIGHT));
  257. }
  258. } else {
  259. if ((state & ODS_SELECTED)) {
  260. // draw the circular segments for stretched AND non-stretched buttons
  261. DrawCircleLeft(pDC, m_ptLeft, nRadius, 
  262.    ::GetSysColor(COLOR_3DDKSHADOW), ::GetSysColor(COLOR_3DHIGHLIGHT));
  263. DrawCircleRight(pDC, m_ptRight, nRadius, 
  264.    ::GetSysColor(COLOR_3DDKSHADOW), ::GetSysColor(COLOR_3DHIGHLIGHT));
  265. DrawCircleLeft(pDC, m_ptLeft, nRadius-1, 
  266.    ::GetSysColor(COLOR_3DSHADOW), ::GetSysColor(COLOR_3DLIGHT));
  267. DrawCircleRight(pDC, m_ptRight, nRadius-1, 
  268.    ::GetSysColor(COLOR_3DSHADOW), ::GetSysColor(COLOR_3DLIGHT));
  269. // draw connecting lines for stretched buttons only
  270. if (m_bStretch)
  271. {
  272. CPen* oldpen;
  273. CPen* mypen;
  274. oldpen = pDC->SelectObject(new CPen(PS_SOLID, 1, ::GetSysColor(COLOR_3DDKSHADOW)));
  275. pDC->MoveTo(m_ptLeft.x, 1); pDC->LineTo(m_ptRight.x, 1);
  276. delete pDC->SelectObject(new CPen(PS_SOLID, 1, ::GetSysColor(COLOR_3DSHADOW)));
  277. pDC->MoveTo(m_ptLeft.x, 2); pDC->LineTo(m_ptRight.x, 2);
  278. delete pDC->SelectObject(new CPen(PS_SOLID, 1, ::GetSysColor(COLOR_3DLIGHT)));
  279. pDC->MoveTo(m_ptLeft.x, m_ptLeft.y + nRadius-1); pDC->LineTo(m_ptRight.x, m_ptLeft.y + nRadius-1);
  280. delete pDC->SelectObject(new CPen(PS_SOLID, 1, ::GetSysColor(COLOR_3DHIGHLIGHT)));
  281. pDC->MoveTo(m_ptLeft.x, m_ptLeft.y + nRadius); pDC->LineTo(m_ptRight.x, m_ptLeft.y + nRadius);
  282. delete pDC->SelectObject(oldpen);
  283. }
  284.    
  285. } else {
  286. // draw the circular segments for stretched AND non-stretched buttons
  287. DrawCircleLeft(pDC, m_ptLeft, nRadius, 
  288.    ::GetSysColor(COLOR_3DHIGHLIGHT), ::GetSysColor(COLOR_3DDKSHADOW));
  289. DrawCircleRight(pDC, m_ptRight, nRadius, 
  290.    ::GetSysColor(COLOR_3DHIGHLIGHT), ::GetSysColor(COLOR_3DDKSHADOW));
  291. DrawCircleLeft(pDC, m_ptLeft, nRadius - 1, 
  292.    ::GetSysColor(COLOR_3DLIGHT), ::GetSysColor(COLOR_3DSHADOW));
  293. DrawCircleRight(pDC, m_ptRight, nRadius - 1, 
  294.    ::GetSysColor(COLOR_3DLIGHT), ::GetSysColor(COLOR_3DSHADOW));
  295. // draw connecting lines for stretched buttons only
  296. if (m_bStretch)
  297. {
  298. CPen* oldpen;
  299. oldpen = pDC->SelectObject(new CPen(PS_SOLID, 1, pDC->GetPixel(m_ptLeft.x, 1)));
  300. pDC->MoveTo(m_ptLeft.x, 1); pDC->LineTo(m_ptRight.x, 1);
  301. delete pDC->SelectObject(new CPen(PS_SOLID, 1, pDC->GetPixel(m_ptLeft.x, 2)));
  302. pDC->MoveTo(m_ptLeft.x, 2); pDC->LineTo(m_ptRight.x, 2);
  303. delete pDC->SelectObject(new CPen(PS_SOLID, 1, pDC->GetPixel(m_ptLeft.x, m_ptLeft.y + nRadius)));
  304. pDC->MoveTo(m_ptLeft.x, m_ptLeft.y + nRadius); pDC->LineTo(m_ptRight.x, m_ptLeft.y + nRadius);
  305. delete pDC->SelectObject(new CPen(PS_SOLID, 1, pDC->GetPixel(m_ptLeft.x, m_ptLeft.y + nRadius - 1)));
  306. pDC->MoveTo(m_ptLeft.x, m_ptLeft.y + nRadius - 1); pDC->LineTo(m_ptRight.x, m_ptLeft.y + nRadius - 1);
  307. delete pDC->SelectObject(oldpen);
  308. }
  309. }
  310. }
  311. // draw the text if there is any
  312. CString strText;
  313. GetWindowText(strText);
  314. if (!strText.IsEmpty())
  315. {
  316. CRgn rgn;
  317. if (m_bStretch){
  318. rgn.CreateRectRgn(m_ptLeft.x-nRadius/2, m_ptCentre.y-nRadius, 
  319. m_ptRight.x+nRadius/2, m_ptCentre.y+nRadius);}
  320. else{
  321. rgn.CreateEllipticRgn(m_ptCentre.x-nRadius, m_ptCentre.y-nRadius, 
  322. m_ptCentre.x+nRadius, m_ptCentre.y+nRadius);}
  323. pDC->SelectClipRgn(&rgn);
  324. CSize Extent = pDC->GetTextExtent(strText);
  325. CPoint pt = CPoint( m_ptCentre.x - Extent.cx/2, m_ptCentre.y - Extent.cy/2 );
  326. if (state & ODS_SELECTED) pt.Offset(1,1);
  327. pDC->SetBkMode(TRANSPARENT);
  328. if (state & ODS_DISABLED)
  329. pDC->DrawState(pt, Extent, strText, DSS_DISABLED, TRUE, 0, (HBRUSH)NULL);
  330. else
  331. {
  332. // changed this code to give the text a 3d-look
  333. COLORREF oldcol = pDC->SetTextColor(::GetSysColor(COLOR_3DHIGHLIGHT));
  334. pDC->TextOut(pt.x, pt.y, strText);
  335. pDC->SetTextColor(::GetSysColor(COLOR_3DDKSHADOW));
  336. pDC->TextOut(pt.x-1, pt.y-1, strText);
  337. pDC->SetTextColor(oldcol);
  338. }
  339. pDC->SelectClipRgn(NULL);
  340. rgn.DeleteObject();
  341. }
  342. // Draw the focus circle on the inside of the button if it is non-stretched
  343. if ((state & ODS_FOCUS) && m_bDrawDashedFocusCircle && !m_bStretch)
  344. DrawCircle(pDC, m_ptCentre, nRadius-2, RGB(0,0,0), TRUE);
  345. pDC->RestoreDC(nSavedDC);
  346. }