swingbutton.cpp
上传用户:swkcbjrc
上传日期:2016-04-02
资源大小:45277k
文件大小:3k
源码类别:

游戏

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. //#include "  add additional includes here"
  3. #include "swingbutton.h"
  4. #ifdef _DEBUG
  5. #define new DEBUG_NEW
  6. #undef THIS_FILE
  7. static char THIS_FILE[] = __FILE__;
  8. #endif
  9. /////////////////////////////////////////////////////////////////////////////
  10. // CSwingButton
  11. CSwingButton::CSwingButton()
  12. {
  13. nActiveBrush.CreateSolidBrush(RGB(110, 110, 110));
  14. nInactiveBrush.CreateSolidBrush(RGB(204, 204, 204));
  15. nDarkBorder.CreatePen(PS_SOLID, 1, RGB(128, 128, 128));
  16. nWhiteBorder.CreatePen(PS_SOLID, 1, RGB(255, 255, 255));
  17. nSelectedBorder.CreatePen(PS_SOLID, 1, RGB(153, 153, 204));
  18. }
  19. CSwingButton::~CSwingButton()
  20. {
  21. }
  22. BEGIN_MESSAGE_MAP(CSwingButton, CButton)
  23. //{{AFX_MSG_MAP(CSwingButton)
  24. // NOTE - the ClassWizard will add and remove mapping macros here.
  25. //}}AFX_MSG_MAP
  26. END_MESSAGE_MAP()
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CSwingButton message handlers
  29. void CSwingButton::SetButtonText(CString pString)
  30. {
  31. m_strCaption = pString;
  32. }
  33. void CSwingButton::DrawItem(LPDRAWITEMSTRUCT lpDIS) 
  34. {
  35. DrawButton(lpDIS);
  36. // TODO: Add your code to draw the specified item
  37. }
  38. void CSwingButton::DrawButton(LPDRAWITEMSTRUCT lpDIS)
  39. {
  40. CDC* pDC = CDC::FromHandle(lpDIS->hDC);
  41. CRect rectItem(lpDIS->rcItem);
  42. CPoint TopLeft(rectItem.left, rectItem.top);
  43. CPoint BottomRight(rectItem.right - 1, rectItem.bottom - 1);
  44. CPoint TopRight(rectItem.right - 1, rectItem.top);
  45. CPoint BottomLeft(rectItem.left, rectItem.bottom - 1);
  46. if (lpDIS->itemState & ODS_SELECTED)
  47. {
  48. pDC->SelectObject(&nActiveBrush);
  49. pDC->SelectStockObject(NULL_PEN);
  50. pDC->Rectangle(rectItem);
  51. goto WRITE_TEXT;
  52. }
  53. pDC->SelectObject(&nInactiveBrush);
  54. pDC->SelectStockObject(NULL_PEN);
  55. pDC->Rectangle(rectItem);
  56. pDC->SelectObject(&nDarkBorder);
  57. pDC->MoveTo(TopLeft);
  58. pDC->LineTo(TopRight);
  59. pDC->MoveTo(TopLeft);
  60. pDC->LineTo(BottomLeft);
  61. pDC->MoveTo(BottomLeft.x, BottomLeft.y - 1);
  62. pDC->LineTo(BottomRight.x, BottomRight.y - 1);
  63. pDC->MoveTo(BottomRight.x - 1, BottomRight.y);
  64. pDC->LineTo(TopRight.x - 1, TopRight.y);
  65. pDC->SelectObject(&nWhiteBorder);
  66. pDC->MoveTo(BottomLeft);
  67. pDC->LineTo(BottomRight);
  68. pDC->MoveTo(BottomRight);
  69. pDC->LineTo(TopRight);
  70. pDC->MoveTo(TopLeft.x + 1, TopLeft.y + 1);
  71. pDC->LineTo(TopRight.x - 1, TopRight.y + 1);
  72. pDC->MoveTo(TopLeft.x + 1, TopLeft.y + 1);
  73. pDC->LineTo(BottomLeft.x + 1, BottomLeft.y - 1);
  74. WRITE_TEXT:
  75. pDC->SetBkMode(TRANSPARENT);
  76. pDC->SelectStockObject(DEFAULT_GUI_FONT);
  77. CSize pExtent = pDC->GetTextExtent(m_strCaption);
  78. CPoint centerPoint = rectItem.CenterPoint();
  79. CPoint drawText;
  80. drawText.x = centerPoint.x - (pExtent.cx / 2);
  81. drawText.y = centerPoint.y - (pExtent.cy / 2);
  82. pDC->TextOut(drawText.x, drawText.y, m_strCaption);
  83. if (lpDIS->itemState & ODS_FOCUS)
  84. {
  85. pDC->SelectObject(&nSelectedBorder);
  86. pDC->SelectStockObject(NULL_BRUSH);
  87. pDC->Rectangle(drawText.x - 1, drawText.y - 1, 
  88. centerPoint.x + (pExtent.cx / 2) + 3,
  89. centerPoint.y + (pExtent.cy / 2) + 3); 
  90. }
  91. }
  92. void CSwingButton::PreSubclassWindow() 
  93. {
  94. // TODO: Add your specialized code here and/or call the base class
  95. CButton::PreSubclassWindow();
  96. ModifyStyle(0, BS_OWNERDRAW);
  97. }
  98. CString CSwingButton::GetButtonText()
  99. {
  100. return m_strCaption;
  101. }