Circle.cpp
上传用户:hehe2haha
上传日期:2013-08-16
资源大小:161k
文件大小:2k
源码类别:

CAD

开发平台:

Visual C++

  1. // Circle.cpp: implementation of the CCircle class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "Circle.h"
  6. #include "WinApp.h"
  7. extern CWinApp g_theApp;
  8. //////////////////////////////////////////////////////////////////////
  9. // Construction/Destruction
  10. //////////////////////////////////////////////////////////////////////
  11. CCircle::CCircle()
  12. {
  13. strcpy(m_className,"CCircle");
  14. nRadius = 0;
  15. }
  16. CCircle::~CCircle()
  17. {
  18. }
  19. void CCircle::Draw(HDC hdc)
  20. {
  21. HPEN hPen = ::CreatePen( fnPenStyle,nWidth,crColor);
  22. HPEN hOldPen = (HPEN)::SelectObject(hdc,hPen);
  23. //不进行填充
  24. ::SelectObject(hdc,GetStockObject(NULL_BRUSH));
  25. if (nRadius == 0)
  26. {
  27. nRadius = CDraw::Distance(m_ptFirstPos,m_ptSecondPos);
  28. }
  29. CDraw::DrawCircle(hdc,m_ptFirstPos,nRadius);
  30. ::SelectObject(hdc,hOldPen);
  31. ::DeleteObject(hPen);
  32. if(isPick == TRUE)
  33. {
  34. //恢复填充
  35. ::SelectObject(hdc,GetStockObject(WHITE_BRUSH));
  36. DrawRect(hdc);
  37. }
  38. }
  39. void CCircle::DrawRect(HDC hdc)
  40. {
  41. POINT t_ptFirstPos,t_ptSecondPos;
  42. //外接矩形的左上点和右下点坐标
  43. t_ptFirstPos.x  = m_ptFirstPos.x - nRadius;
  44. t_ptSecondPos.x = m_ptFirstPos.x + nRadius;
  45. t_ptFirstPos.y  = m_ptFirstPos.y - nRadius;
  46. t_ptSecondPos.y = m_ptFirstPos.y + nRadius;
  47. //圆心的坐标
  48. int mPtx = m_ptFirstPos.x;
  49. int mPty = m_ptFirstPos.y;
  50. //top left
  51. Ellipse(hdc,t_ptFirstPos.x - Rect,t_ptFirstPos.y - Rect,
  52. t_ptFirstPos.x + Rect,t_ptFirstPos.y + Rect);
  53. //top right
  54. Ellipse(hdc,t_ptSecondPos.x - Rect,t_ptFirstPos.y - Rect,
  55. t_ptSecondPos.x + Rect,t_ptFirstPos.y + Rect);
  56. //top middle
  57. Ellipse(hdc,mPtx - Rect,t_ptFirstPos.y - Rect,
  58. mPtx + Rect,t_ptFirstPos.y + Rect);
  59. //left middle
  60. Ellipse(hdc,t_ptFirstPos.x - Rect,mPty - Rect,
  61. t_ptFirstPos.x + Rect,mPty + Rect);
  62. //right middle
  63. Ellipse(hdc,t_ptSecondPos.x - Rect,mPty - Rect,
  64. t_ptSecondPos.x + Rect,mPty + Rect);
  65. //buttom middle
  66. Ellipse(hdc,mPtx - Rect,t_ptSecondPos.y - Rect,
  67. mPtx + Rect,t_ptSecondPos.y + Rect);
  68. //buttom left
  69. Ellipse(hdc,t_ptFirstPos.x - Rect,t_ptSecondPos.y - Rect,
  70. t_ptFirstPos.x + Rect,t_ptSecondPos.y + Rect);
  71. //buttom right
  72. Ellipse(hdc,t_ptSecondPos.x - Rect,t_ptSecondPos.y - Rect,
  73. t_ptSecondPos.x + Rect,t_ptSecondPos.y + Rect);
  74. }
  75. BOOL CCircle::pick(POINT pt)
  76. {
  77. //计算当前鼠标位置与圆心的距离
  78. int dblLength = CDraw::Distance(m_ptFirstPos,pt);
  79. if (fabs(dblLength - nRadius) > 5)
  80. return FALSE;
  81. return TRUE;
  82. }