colorbtn.cpp
Upload User: sz27991729
Upload Date: 2007-01-01
Package Size: 13k
Code Size: 5k
Category:

Button control

Development Platform:

Visual C++

  1. /////////////////////////////////////////////////////////////////////////////
  2. // colorBtn.cpp : implementation file for the CColorButton class
  3. //
  4. // Written by Bob Ryan (ryan@cyberzone.net)
  5. // Copyright (c) 1998.
  6. //
  7. // This code may be freely distributable in any application.  If
  8. // you make any changes to the source, please let me know so that
  9. // I might make a better version of the class.
  10. //
  11. // This file is provided "as is" with no expressed or implied warranty.
  12. // The author accepts no liability for any damage/loss of business that
  13. // this product may cause.
  14. //
  15. /////////////////////////////////////////////////////////////////////////////
  16. #include "stdafx.h"
  17. #include "colorbtn.h"
  18.   
  19. #ifdef _DEBUG
  20. #undef THIS_FILE
  21. static char BASED_CODE THIS_FILE[] = __FILE__;
  22. #endif
  23. // no automatic class substitution for this file!
  24. #ifdef CColorButton
  25. #undef CColorButton      CColorButton
  26. #endif
  27. // CColorButton
  28. IMPLEMENT_DYNAMIC(CColorButton, CButton)
  29. CColorButton::CColorButton() 
  30. {  
  31. #if (_MFC_VER < 0x0250)
  32.   hwndOwner = NULL;  // initialize hwndOwner for GetOwner() and SetOwner() support in MFC < 2.5
  33. #endif 
  34. CColorButton::~CColorButton()
  35. {
  36. BOOL CColorButton::Attach(const UINT nID, CWnd* pParent, const COLORREF BGColor, const COLORREF FGColor, const COLORREF DisabledColor, const UINT nBevel)
  37. {
  38. if (!SubclassDlgItem(nID, pParent))
  39. return FALSE;
  40. m_fg = FGColor;
  41. m_bg = BGColor; 
  42. m_disabled = DisabledColor;
  43. m_bevel = nBevel;
  44. return TRUE;
  45. void CColorButton::DrawItem(LPDRAWITEMSTRUCT lpDIS)
  46. {
  47. CDC* pDC = CDC::FromHandle(lpDIS->hDC);
  48. UINT state = lpDIS->itemState; 
  49. CRect focusRect, btnRect;
  50. focusRect.CopyRect(&lpDIS->rcItem); 
  51. btnRect.CopyRect(&lpDIS->rcItem); 
  52. //
  53. // Set the focus rectangle to just past the border decoration
  54. //
  55. focusRect.left += 4;
  56.     focusRect.right -= 4;
  57.     focusRect.top += 4;
  58.     focusRect.bottom -= 4;
  59.       
  60. //
  61. // Retrieve the button's caption
  62. //
  63.     const int bufSize = 512;
  64.     TCHAR buffer[bufSize];
  65.     GetWindowText(buffer, bufSize);
  66. //
  67. // Draw and label the button using draw methods 
  68. //
  69.     DrawFilledRect(pDC, btnRect, GetBGColor()); 
  70.     DrawFrame(pDC, btnRect, GetBevel());
  71.    DrawButtonText(pDC, btnRect, buffer, GetFGColor());
  72. //
  73. // Now, depending upon the state, redraw the button (down image) if it is selected,
  74. // place a focus rectangle on it, or redisplay the caption if it is disabled
  75. //
  76. if (state & ODS_FOCUS) {
  77. DrawFocusRect(lpDIS->hDC, (LPRECT)&focusRect);
  78. if (state & ODS_SELECTED){ 
  79.      DrawFilledRect(pDC, btnRect, GetBGColor()); 
  80.      DrawFrame(pDC, btnRect, -1);
  81.    DrawButtonText(pDC, btnRect, buffer, GetFGColor());
  82. DrawFocusRect(lpDIS->hDC, (LPRECT)&focusRect);
  83. }
  84. }
  85. else if (state & ODS_DISABLED) {
  86.      //COLORREF disabledColor = bg ^ 0xFFFFFF; // contrasting color
  87.    DrawButtonText(pDC, btnRect, buffer, GetDisabledColor());
  88.     }
  89. void CColorButton::DrawFrame(CDC *DC, CRect R, int Inset)
  90. COLORREF dark, light, tlColor, brColor;
  91. int i, m, width;
  92. width = (Inset < 0)? -Inset : Inset;
  93. for (i = 0; i < width; i += 1) {
  94. m = 255 / (i + 2);
  95. dark = PALETTERGB(m, m, m);
  96. m = 192 + (63 / (i + 1));
  97. light = PALETTERGB(m, m, m);
  98.   
  99.    if ( width == 1 ) {
  100. light = RGB(255, 255, 255);
  101. dark = RGB(128, 128, 128);
  102. }
  103. if ( Inset < 0 ) {
  104. tlColor = dark;
  105. brColor = light;
  106. }
  107. else {
  108. tlColor = light;
  109. brColor = dark;
  110. }
  111. DrawLine(DC, R.left, R.top, R.right, R.top, tlColor); // Across top
  112. DrawLine(DC, R.left, R.top, R.left, R.bottom, tlColor); // Down left
  113.   
  114. if ( (Inset < 0) && (i == width - 1) && (width > 1) ) {
  115. DrawLine(DC, R.left + 1, R.bottom - 1, R.right, R.bottom - 1, RGB(1, 1, 1));// Across bottom
  116. DrawLine(DC, R.right - 1, R.top + 1, R.right - 1, R.bottom, RGB(1, 1, 1)); // Down right
  117. }
  118.    else {
  119. DrawLine(DC, R.left + 1, R.bottom - 1, R.right, R.bottom - 1, brColor); // Across bottom
  120. DrawLine(DC, R.right - 1, R.top + 1, R.right - 1, R.bottom, brColor); // Down right
  121. }
  122.    InflateRect(R, -1, -1);
  123. }
  124. }
  125. void CColorButton::DrawFilledRect(CDC *DC, CRect R, COLORREF color)
  126. CBrush B;
  127. B.CreateSolidBrush(color);
  128. DC->FillRect(R, &B);
  129. }
  130.  
  131. void CColorButton::DrawLine(CDC *DC, CRect EndPoints, COLORREF color)
  132. CPen newPen;
  133. newPen.CreatePen(PS_SOLID, 1, color);
  134. CPen *oldPen = DC->SelectObject(&newPen);
  135. DC->MoveTo(EndPoints.left, EndPoints.top);
  136. DC->LineTo(EndPoints.right, EndPoints.bottom);
  137. DC->SelectObject(oldPen);
  138.     newPen.DeleteObject();
  139. }
  140. void CColorButton::DrawLine(CDC *DC, long left, long top, long right, long bottom, COLORREF color)
  141. CPen newPen;
  142. newPen.CreatePen(PS_SOLID, 1, color);
  143. CPen *oldPen = DC->SelectObject(&newPen);
  144. DC->MoveTo(left, top);
  145. DC->LineTo(right, bottom);
  146. DC->SelectObject(oldPen);
  147.     newPen.DeleteObject();
  148. }
  149. void CColorButton::DrawButtonText(CDC *DC, CRect R, const char *Buf, COLORREF TextColor)
  150. {
  151.     COLORREF prevColor = DC->SetTextColor(TextColor);
  152.     DC->SetBkMode(TRANSPARENT);
  153. DC->DrawText(Buf, strlen(Buf), R, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
  154. DC->SetTextColor(prevColor);
  155. }