DrawRect.cpp
资源名称:DrawSys.zip [点击查看]
上传用户:mosfetic
上传日期:2022-06-16
资源大小:4612k
文件大小:4k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- // DrawRect.cpp: implementation of the CDrawRect class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "DrawSys.h"
- #include "DrawRect.h"
- #include "ViewProperty.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- IMPLEMENT_SERIAL(CDrawRect, CObject, 1)
- CDrawRect::CDrawRect()
- {
- m_ptFirst.x = m_ptFirst.y = 0;
- m_ptSecond.x = m_ptSecond.y = 0;
- m_ptTemp.x = m_ptTemp.y = 0;
- m_bStart = FALSE;
- }
- CDrawRect::~CDrawRect()
- {
- }
- void CDrawRect::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_ptFirst.x;
- rcDraw.top = m_ptFirst.y;
- rcDraw.right = m_ptSecond.x;
- rcDraw.bottom = m_ptSecond.y;
- pDC->Rectangle(&rcDraw);
- pDC->SelectObject(pOldBrush);
- }
- void CDrawRect::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_ptFirst.x;
- rcDraw.top = m_ptFirst.y;
- rcDraw.right = m_ptTemp.x;
- rcDraw.bottom = m_ptTemp.y;
- pDC->Rectangle(&rcDraw);
- m_ptTemp = point;
- rcDraw.right = m_ptTemp.x;
- rcDraw.bottom = m_ptTemp.y;
- pDC->Rectangle(&rcDraw);
- pDC->SetROP2(iDrawMode);
- pDC->SelectObject(pOldBrush);
- }
- }
- }
- void CDrawRect::OnLButtonDown(CDC* pDC, UINT nFlags, CPoint point)
- {
- m_ptFirst = point;
- m_ptTemp = point;
- m_bStart = TRUE;
- }
- BOOL CDrawRect::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_ptFirst.x;
- rcDraw.top = m_ptFirst.y;
- rcDraw.right = m_ptTemp.x;
- rcDraw.bottom = m_ptTemp.y;
- pDC->Rectangle(&rcDraw);
- m_ptSecond = point;
- m_ptTemp = point;
- rcDraw.left = MIN(m_ptFirst.x, m_ptSecond.x);
- rcDraw.top = MIN(m_ptFirst.y, m_ptSecond.y);
- rcDraw.right = MAX(m_ptFirst.x, m_ptSecond.x);
- rcDraw.bottom = MAX(m_ptFirst.y, m_ptSecond.y);
- m_ptFirst.x = rcDraw.left;
- m_ptFirst.y = rcDraw.top;
- m_ptSecond.x = rcDraw.right;
- m_ptSecond.y = rcDraw.bottom;
- pDC->SetROP2(R2_COPYPEN);
- Draw(pDC);
- pDC->SetROP2(iDrawMode);
- pDC->SelectObject(pOldBrush);
- }
- m_bStart = FALSE;
- }
- return (!rcDraw.IsRectEmpty());
- }
- void CDrawRect::Serialize(CArchive& ar)
- {
- CDrawShape::Serialize(ar);
- if (ar.IsStoring())
- {
- // TODO: add storing code here
- ar << m_ptFirst << m_ptSecond;
- }
- else
- {
- // TODO: add loading code here
- ar >> m_ptFirst >> m_ptSecond;
- }
- }
- BOOL CDrawRect::PtInShape(CPoint point)
- {
- CRect rcDraw;
- rcDraw.left = MIN(m_ptFirst.x, m_ptSecond.x);
- rcDraw.top = MIN(m_ptFirst.y, m_ptSecond.y);
- rcDraw.right = MAX(m_ptFirst.x, m_ptSecond.x);
- rcDraw.bottom = MAX(m_ptFirst.y, m_ptSecond.y);
- return rcDraw.PtInRect(point);
- }
- void CDrawRect::ShowProperty()
- {
- CRect rcDraw;
- CString strID;
- CString strTime;
- CViewProperty dlg;
- rcDraw.left = m_ptFirst.x;
- rcDraw.top = m_ptFirst.y;
- rcDraw.right = m_ptSecond.x;
- rcDraw.bottom = m_ptSecond.y;
- 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();
- }