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

CAD

开发平台:

Visual C++

  1. // CircleTool.cpp: implementation of the CCircleTool class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "CircleTool.h"
  6. #include "Circle.h"
  7. #include "WinApp.h"
  8. extern CWinApp g_theApp;
  9. //////////////////////////////////////////////////////////////////////
  10. // Construction/Destruction
  11. //////////////////////////////////////////////////////////////////////
  12. CCircleTool::CCircleTool()
  13. {
  14. }
  15. CCircleTool::~CCircleTool()
  16. {
  17. }
  18. void CCircleTool::OnMouseMove(HWND hWnd, UINT nFlags, POINT ptPos) 
  19. {
  20. CDataBase *pDataBase = &(g_theApp.m_DataBase);
  21. if(m_eToolState == FirstHit)
  22. {
  23. HDC  hdc      = ::GetDC(hWnd);
  24. int  nOldMode = ::SetROP2(hdc,R2_XORPEN);
  25. HPEN hPen     = ::CreatePen( pDataBase->m_Shape.fnPenStyle,
  26.              pDataBase->m_Shape.nWidth,
  27.      pDataBase->m_Shape.crColor^ GetBkColor(hdc) );
  28.  
  29.  
  30. HPEN hOldPen = (HPEN)::SelectObject(hdc,hPen);
  31. //不进行填充
  32. ::SelectObject(hdc,GetStockObject(NULL_BRUSH));
  33. //擦去前一次线段
  34. int nRadius = CDraw::Distance(m_ptBeginPos,m_ptPrePos );
  35. CDraw::DrawCircle(hdc,m_ptBeginPos,nRadius);
  36. //画出这一次线段
  37. nRadius = CDraw::Distance(m_ptBeginPos,ptPos );
  38. CDraw::DrawCircle(hdc,m_ptBeginPos,nRadius);
  39. ::SetROP2(hdc,nOldMode);
  40. ::SelectObject(hdc,hOldPen);
  41. ::DeleteObject(hPen);
  42. ::ReleaseDC(hWnd,hdc);
  43. m_ptPrePos = ptPos;
  44. }
  45. }
  46. void CCircleTool::OnRButtonDown(HWND hWnd,UINT nFlags, POINT ptPos)
  47. {
  48. CDataBase *pDataBase = &(g_theApp.m_DataBase);
  49. if(m_eToolState == FirstHit)
  50. {
  51. HDC  hdc      = ::GetDC(hWnd);
  52. int  nOldMode = ::SetROP2(hdc,R2_XORPEN);
  53. HPEN hPen     = ::CreatePen( pDataBase->m_Shape.fnPenStyle,
  54.              pDataBase->m_Shape.nWidth,
  55.      pDataBase->m_Shape.crColor^ GetBkColor(hdc) );
  56. ::SelectObject(hdc,hPen);
  57. //不进行填充
  58. ::SelectObject(hdc,GetStockObject(NULL_BRUSH));
  59. //擦除
  60. int nRadius = CDraw::Distance(m_ptBeginPos,m_ptPrePos );
  61. CDraw::DrawCircle(hdc,m_ptBeginPos,nRadius);
  62. ::SetROP2(hdc,nOldMode);
  63. ::ReleaseDC(hWnd,hdc);
  64. }
  65. //取消画图状态
  66. OverCurTool(hWnd);
  67. }
  68. void CCircleTool::OnLButtonDown(HWND hWnd,UINT nFlags, POINT ptPos)
  69. {
  70. CDataBase *pDataBase         = &(g_theApp.m_DataBase);
  71. CArray<CShape*> *pArrayShape = &(g_theApp.m_DataBase.m_ArrayShape);
  72. CCircle *pShape              = NULL;
  73. if(m_eToolState == Init_State)
  74. {
  75. m_eToolState = FirstHit;
  76. m_ptBeginPos = m_ptPrePos = ptPos;
  77. }
  78. else if(m_eToolState == FirstHit)
  79. {
  80. m_eToolState = SecondHit;
  81. pShape       = new CCircle;
  82. //保存图形样式
  83. pShape->crColor    = pDataBase->m_Shape.crColor;
  84. pShape->fnPenStyle = pDataBase->m_Shape.fnPenStyle;
  85. pShape->nWidth     = pDataBase->m_Shape.nWidth;
  86. pShape->m_ptFirstPos  = m_ptBeginPos;
  87. pShape->m_ptSecondPos = ptPos;
  88. pShape->nRadius       = CDraw::Distance(pShape->m_ptFirstPos,
  89.                     pShape->m_ptSecondPos);
  90. //第一点和第二点不重合才保存
  91. if (pShape->m_ptFirstPos.x != ptPos.x || 
  92. pShape->m_ptFirstPos.y != ptPos.y)
  93. {
  94. //保存至数组
  95. pArrayShape->Add(pShape);
  96. }
  97. else
  98. {
  99. delete pShape;
  100. pShape = NULL;
  101. }
  102. //取消画图状态
  103. OverCurTool(hWnd);
  104. }
  105. }