DrawCircle.cpp
资源名称:DrawSys.zip [点击查看]
上传用户:mosfetic
上传日期:2022-06-16
资源大小:4612k
文件大小:4k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- // DrawCircle.cpp: implementation of the CDrawCircle class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "DrawSys.h"
- #include "DrawCircle.h"
- #include <cmath>
- #include "ViewProperty.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- IMPLEMENT_SERIAL(CDrawCircle, CObject, 1)
- CDrawCircle::CDrawCircle()
- {
- m_ptCenter.x = m_ptCenter.y = 0;
- m_ptTemp.x = m_ptTemp.y = 0;
- m_nRadius = 0;
- m_bStart = FALSE;
- }
- CDrawCircle::~CDrawCircle()
- {
- }
- void CDrawCircle::Draw(CDC* pDC)
- {
- CRect rcDraw;
- CBrush* pBrush = NULL;
- CBrush* pOldBrush = NULL;
- pBrush = CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
- pOldBrush = pDC->SelectObject(pBrush);
- rcDraw.left = m_ptCenter.x - m_nRadius;
- rcDraw.top = m_ptCenter.y - m_nRadius;
- rcDraw.right = m_ptCenter.x + m_nRadius;
- rcDraw.bottom = m_ptCenter.y + m_nRadius;
- pDC->Ellipse(&rcDraw);
- pDC->SelectObject(pOldBrush);
- }
- void CDrawCircle::OnMouseMove(CDC* pDC, UINT nFlags, CPoint point)
- {
- int iDrawMode = 0;
- CRect rcDraw;
- CBrush* pBrush = NULL;
- CBrush* pOldBrush = NULL;
- if(m_bStart)
- {
- if(pDC)
- {
- pBrush = CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
- pOldBrush = pDC->SelectObject(pBrush);
- iDrawMode = pDC->SetROP2(R2_NOTXORPEN);
- rcDraw.left = m_ptCenter.x - m_nRadius;
- rcDraw.top = m_ptCenter.y - m_nRadius;
- rcDraw.right = m_ptCenter.x + m_nRadius;
- rcDraw.bottom = m_ptCenter.y + m_nRadius;
- pDC->Ellipse(&rcDraw);
- m_ptTemp = point;
- m_nRadius = CalcRadius(m_ptCenter, m_ptTemp);
- rcDraw.left = m_ptCenter.x - m_nRadius;
- rcDraw.top = m_ptCenter.y - m_nRadius;
- rcDraw.right = m_ptCenter.x + m_nRadius;
- rcDraw.bottom = m_ptCenter.y + m_nRadius;
- pDC->Ellipse(&rcDraw);
- pDC->SetROP2(iDrawMode);
- pDC->SelectObject(pOldBrush);
- }
- }
- }
- void CDrawCircle::OnLButtonDown(CDC* pDC, UINT nFlags, CPoint point)
- {
- m_ptCenter = point;
- m_ptTemp = point;
- m_bStart = TRUE;
- }
- BOOL CDrawCircle::OnLButtonUp(CDC* pDC, UINT nFlags, CPoint point)
- {
- int iDrawMode = 0;
- CRect rcDraw;
- CBrush* pBrush = NULL;
- CBrush* pOldBrush = NULL;
- if(m_bStart)
- {
- if(pDC)
- {
- pBrush = CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
- pOldBrush = pDC->SelectObject(pBrush);
- iDrawMode = pDC->SetROP2(R2_NOTXORPEN);
- rcDraw.left = m_ptCenter.x - m_nRadius;
- rcDraw.top = m_ptCenter.y - m_nRadius;
- rcDraw.right = m_ptCenter.x + m_nRadius;
- rcDraw.bottom = m_ptCenter.y + m_nRadius;
- pDC->Ellipse(&rcDraw);
- m_ptTemp = point;
- m_nRadius = CalcRadius(m_ptCenter, m_ptTemp);
- rcDraw.left = m_ptCenter.x - m_nRadius;
- rcDraw.top = m_ptCenter.y - m_nRadius;
- rcDraw.right = m_ptCenter.x + m_nRadius;
- rcDraw.bottom = m_ptCenter.y + m_nRadius;
- pDC->SetROP2(R2_COPYPEN);
- Draw(pDC);
- pDC->SetROP2(iDrawMode);
- pDC->SelectObject(pOldBrush);
- }
- m_bStart = FALSE;
- }
- return (!rcDraw.IsRectEmpty());
- }
- LONG CDrawCircle::CalcRadius(CPoint& ptFirst, CPoint& ptSecond)
- {
- double dRadius = 0.0;
- dRadius = pow(ptSecond.x-ptFirst.x, 2) + pow(ptSecond.y-ptFirst.y, 2);
- dRadius = sqrt(dRadius);
- return (LONG)dRadius;
- }
- void CDrawCircle::Serialize(CArchive& ar)
- {
- if (ar.IsStoring())
- {
- // TODO: add storing code here
- ar << m_ptCenter << m_nRadius;
- }
- else
- {
- // TODO: add loading code here
- ar >> m_ptCenter >> m_nRadius;
- }
- }
- BOOL CDrawCircle::PtInShape(CPoint point)
- {
- CRgn rgn;
- CRect rcDraw;
- BOOL m_bRetVal = FALSE;
- rcDraw.left = m_ptCenter.x - m_nRadius;
- rcDraw.top = m_ptCenter.y - m_nRadius;
- rcDraw.right = m_ptCenter.x + m_nRadius;
- rcDraw.bottom = m_ptCenter.y + m_nRadius;
- rgn.CreateEllipticRgnIndirect(&rcDraw);
- m_bRetVal = rgn.PtInRegion(point);
- rgn.DeleteObject();
- return m_bRetVal;
- }
- void CDrawCircle::ShowProperty()
- {
- CRect rcDraw;
- CString strID;
- CString strTime;
- CViewProperty dlg;
- rcDraw.left = m_ptCenter.x - m_nRadius;
- rcDraw.top = m_ptCenter.y - m_nRadius;
- rcDraw.right = m_ptCenter.x + m_nRadius;
- rcDraw.bottom = m_ptCenter.y + m_nRadius;
- dlg.m_strType = "Բ";
- MyFormatString(dlg.m_strID, m_iID);
- dlg.m_iLength = rcDraw.Width();
- dlg.m_iWidth = rcDraw.Height();
- dlg.m_strTime = m_tmCreate.Format("%Y - %m - %d");
- dlg.DoModal();
- }