Rect.cpp
资源名称:CAD2006.rar [点击查看]
上传用户:ckg1000
上传日期:2013-01-26
资源大小:630k
文件大小:11k
源码类别:
CAD
开发平台:
Visual C++
- // Rect.cpp: implementation of the CRect class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "CAD2006.h"
- #include "Rect.h"
- #include "Trigon.h"
- #include "math.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- extern enum STATUS;
- static bool rectkillround = false;
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CRectangle::CRectangle() //构造
- {
- m_status = NoClicked;
- }
- CRectangle::~CRectangle()
- {
- }
- void CRectangle::OnLbuttondown(CDC *pDC, CPoint point)
- {
- POINT curPoint; //左键点击的点
- curPoint.x = point.x;
- curPoint.y = point.y;
- switch(m_status)
- {
- case NoClicked:
- {
- m_begPt = m_oldPt = curPoint;
- m_status = FirstClicked; //点击之后状态就变了
- }
- break;
- case FirstClicked:
- {
- m_endPt = curPoint;
- m_status = NoClicked;
- }
- break;
- }
- }
- //鼠标移动(画矩形)
- void CRectangle::Onmousemove(CDC *pDC, CPoint point)
- {
- POINT curPoint;
- curPoint.x = point.x;
- curPoint.y = point.y;
- if( m_status == FirstClicked )
- {
- m_endPt = curPoint;
- Draw(pDC);
- m_oldPt = curPoint; //此次画完了之后就让当前点为老点,以便让老线重画后变成透明
- }
- }
- void CRectangle::Draw(CDC *pDC) //画方法
- {
- HPEN newPen = CreatePen(m_borderStyle, //创建新画笔
- m_borderWidth,m_borderColor^pDC->GetBkColor());
- CBrush newbrush; //创建新画刷
- newbrush.CreateSolidBrush(m_backColor^pDC->GetBkColor());
- pDC->SelectObject(newbrush);
- HPEN oldPen = (HPEN)pDC->SelectObject(newPen);
- int oldR2 = pDC->SetROP2(R2_XORPEN); //设置画笔画刷与背景以异或的方式显示
- pDC->Rectangle(m_begPt.x,m_begPt.y, m_oldPt.x,m_oldPt.y); //擦出以前的矩形
- pDC->Rectangle(m_begPt.x,m_begPt.y, m_endPt.x,m_endPt.y); //画新的
- pDC->SetROP2(oldR2);
- pDC->SelectObject(oldPen);
- ::DeleteObject(newPen);
- ::DeleteObject(oldPen);
- }
- bool CRectangle::IsOnRect(POINT curPt) //判断一个点是否在矩形上
- {
- bool yesno = false;
- POINT begPtR; //右上角
- begPtR.x = m_endPt.x, begPtR.y = m_begPt.y;
- POINT endPtL; //左下角
- endPtL.x = m_begPt.x, endPtL.y = m_endPt.y;
- int rectLen = abs(m_begPt.x - m_endPt.x); //长
- int rectWth = abs(m_begPt.y - m_endPt.y); //宽
- if( Distance(curPt, m_begPt) + Distance(curPt, begPtR) <= rectLen +1 ||
- Distance(curPt, endPtL) + Distance(curPt, m_endPt) <= rectLen +1 ||
- Distance(curPt, m_begPt) + Distance(curPt, endPtL) <= rectWth +1 ||
- Distance(curPt, begPtR) + Distance(curPt, m_endPt) <= rectWth +1 )
- {
- yesno = true;
- }
- return yesno;
- }
- //选定
- bool CRectangle::Select(CDC *pDC, CPoint point)
- {
- bool yesno = false;
- POINT curPt;
- curPt.x = point.x;
- curPt.y = point.y;
- if(IsOnRect(curPt) == true)
- {
- DrawRound(pDC);
- m_oldPt = curPt;
- yesno = true;
- }
- return yesno;
- }
- //画小圆圈
- void CRectangle::DrawRound(CDC *pDC,COLORREF colorRound)
- {
- POINT begPtL;
- begPtL.x = m_begPt.x-7, begPtL.y = m_begPt.y-7;
- POINT begPtR;
- begPtR.x = m_begPt.x+1, begPtR.y = m_begPt.y+1;
- POINT RbegPtL;
- RbegPtL.x = m_endPt.x-1, RbegPtL.y = m_begPt.y-7;
- POINT RbegPtR;
- RbegPtR.x = m_endPt.x+7, RbegPtR.y = m_begPt.y+1;
- POINT endPtL;
- endPtL.x = m_endPt.x-1, endPtL.y = m_endPt.y-1;
- POINT endPtR;
- endPtR.x = m_endPt.x+7, endPtR.y = m_endPt.y+7;
- POINT LendPtL;
- LendPtL.x = m_begPt.x-7, LendPtL.y = m_endPt.y-1;
- POINT LendPtR;
- LendPtR.x = m_begPt.x+1, LendPtR.y = m_endPt.y+7;
- /* int i = pDC->SetROP2(R2_XORPEN);
- HPEN hpen = ::CreatePen(0,0,colorRound^pDC->GetBkColor());
- HPEN holdpen =(HPEN)pDC->SelectObject(hpen);
- pDC->SelectObject(GetStockObject(NULL_BRUSH));
- */
- HPEN newPen = ::CreatePen(0,0, colorRound);
- HBRUSH newBrush = ::CreateSolidBrush(colorRound);
- pDC->SelectObject(newPen);
- pDC->SelectObject(newBrush);
- pDC->Ellipse(begPtL.x,begPtL.y,begPtR.x,begPtR.y);
- pDC->Ellipse(RbegPtL.x,RbegPtL.y,RbegPtR.x,RbegPtR.y);
- pDC->Ellipse(endPtL.x,endPtL.y,endPtR.x,endPtR.y);
- pDC->Ellipse(LendPtL.x,LendPtL.y,LendPtR.x,LendPtR.y);
- // pDC->SetROP2(i);
- DeleteObject(newPen);
- DeleteObject(newBrush);
- }
- CPoint CRectangle::GetPosBegin() //返回开始点
- {
- return m_begPt;
- }
- CPoint CRectangle::GetPosEnd() //返回结束点
- {
- return m_endPt;
- }
- void CRectangle::SetPosBegin(CPoint point)
- {
- m_begPt = point;
- }
- void CRectangle::SetPosEnd(CPoint point)
- {
- m_endPt = point;
- }
- void CRectangle::Update(CDC *pDC) //更新
- {
- HDC hdc = pDC->GetSafeHdc();
- DrawRound(pDC,RGB(255,255,255)); //先用白色擦掉小圆圈
- KillMirLine(pDC); //擦掉镜像直线
- if( rectkillround )
- {
- this->KillRound(pDC);
- rectkillround = false;
- }
- }
- void CRectangle::KillMirLine(CDC *pDC)
- {
- HDC hdc = pDC->GetSafeHdc();
- HPEN newPen = CreatePen(0,0,GetBkColor(hdc)); //用背景色重画
- HPEN oldPen = (HPEN)SelectObject(hdc,newPen);
- ::MoveToEx(hdc,m_MirBegPt.x,m_MirBegPt.y,NULL);
- ::LineTo(hdc,m_MirEndPt.x,m_MirEndPt.y);
- SelectObject(hdc,oldPen);
- ::DeleteObject(newPen);
- ::DeleteObject(oldPen);
- }
- //移动事件
- void CRectangle::Move(CDC *pDC, CPoint point)
- {
- POINT curPoint;
- curPoint.x = point.x;
- curPoint.y = point.y;
- POINT begOldPt = m_begPt; //存老点
- POINT endOldPt = m_endPt;
- m_begPt.x = begOldPt.x + curPoint.x - m_oldPt.x; //重新计算每点坐标
- m_begPt.y = begOldPt.y + curPoint.y - m_oldPt.y;
- m_endPt.x = endOldPt.x + curPoint.x - m_oldPt.x;
- m_endPt.y = endOldPt.y + curPoint.y - m_oldPt.y;
- DrawMove(pDC, m_begPt,m_endPt, begOldPt,endOldPt);
- m_oldPt = curPoint;
- }
- //移动时矩形的画方法
- void CRectangle::DrawMove(CDC *pDC, POINT begPt,POINT endPt, POINT begOldPt, POINT endOldPt)
- {
- HDC hdc = pDC->GetSafeHdc();
- HPEN newPen = CreatePen(m_borderStyle,
- m_borderWidth,m_borderColor^pDC->GetBkColor());
- HPEN oldPen = (HPEN)SelectObject(hdc,newPen);
- int oldR2 = SetROP2(hdc,R2_XORPEN); //设置画笔画刷与背景以异或的方式显示
- SelectObject(hdc,GetStockObject(NULL_BRUSH)); //空画刷
- ::Rectangle(hdc,begOldPt.x,begOldPt.y, endOldPt.x,endOldPt.y); //擦出以前的矩形
- ::Rectangle(hdc,begPt.x,begPt.y, endPt.x,endPt.y);
- SetROP2(hdc,oldR2);
- SelectObject(hdc,oldPen);
- ::DeleteObject(newPen);
- ::DeleteObject(oldPen);
- }
- //缩放
- void CRectangle::Zoom(CDC *pDoc, CPoint point)
- {
- Update(pDoc);
- POINT curPt;
- curPt.x = point.x,curPt.y = point.y;
- POINT begOldPt = m_begPt;
- POINT endOldPt = m_endPt;
- if(m_oldPt.y == begOldPt.y) //选到上边
- {
- m_begPt.y = curPt.y;
- }
- if(m_oldPt.y == endOldPt.y) //下边
- {
- m_endPt.y = curPt.y;
- }
- if(m_oldPt.x == begOldPt.x) //左边
- {
- m_begPt.x = curPt.x;
- }
- if(m_oldPt.x == endOldPt.x) //右边
- {
- m_endPt.x = curPt.x;
- }
- DrawMove(pDoc, m_begPt,m_endPt, begOldPt,endOldPt);
- m_oldPt = curPt;
- }
- //镜象
- void CRectangle::Mirror(CDC *pDoc, CPoint point)
- {
- POINT curPt = point;
- m_oldPt = m_MirEndPt;
- m_MirEndPt = curPt;
- POINT FirstMirOldPt = m_FirstMirPt; //存第1个镜像点
- POINT SecondMirOldPt = m_SecondMirPt; //存第2个镜像点
- POINT ThirdMirOldPt = m_ThirdMirPt; //存第3个镜像点
- POINT FourMirOldPt = m_FourMirPt; //存第4个镜像点
- POINT begPtR; //矩形右上角那个点
- begPtR.x = m_endPt.x, begPtR.y = m_begPt.y;
- POINT endPtL; //矩形左小角那个点
- endPtL.x = m_begPt.x, endPtL.y = m_endPt.y;
- m_FirstMirPt = GetMirPt(m_MirBegPt, m_MirEndPt, m_begPt);
- m_SecondMirPt = GetMirPt(m_MirBegPt, m_MirEndPt, begPtR);
- m_ThirdMirPt = GetMirPt(m_MirBegPt, m_MirEndPt, m_endPt);
- m_FourMirPt = GetMirPt(m_MirBegPt, m_MirEndPt, endPtL);
- DrawMirLine(pDoc,m_MirBegPt,m_MirEndPt); //镜像线
- DrawFourSdMe(pDoc,m_FirstMirPt,m_SecondMirPt,m_ThirdMirPt,m_FourMirPt,
- FirstMirOldPt,SecondMirOldPt,ThirdMirOldPt,FourMirOldPt);
- rectkillround = true;
- }
- void CRectangle::DrawMirLine(CDC *pDC,POINT MirBegPt, POINT MirEndPt)
- {
- HDC hdc = pDC->GetSafeHdc();
- HPEN newPen = CreatePen(2,0,RGB(255,0,0)^GetBkColor(hdc));
- HPEN oldPen = (HPEN)SelectObject(hdc,newPen);
- int oldR2 = SetROP2(hdc,R2_XORPEN);
- ::MoveToEx(hdc,MirBegPt.x,MirBegPt.y,NULL);
- ::LineTo(hdc,m_oldPt.x,m_oldPt.y);
- ::MoveToEx(hdc,MirBegPt.x,MirBegPt.y,NULL);
- ::LineTo(hdc,MirEndPt.x,MirEndPt.y);
- SetROP2(hdc,oldR2);
- SelectObject(hdc,oldPen);
- ::DeleteObject(newPen);
- ::DeleteObject(oldPen);
- }
- //四边形移动时的画方法
- void CRectangle::DrawFourSdMe(CDC *pDC, POINT onePt, POINT twoPt, POINT threePt, POINT fourPt,
- POINT oneOdPt,POINT twoOdPt, POINT threeOdPt, POINT fourOdPt)
- {
- HDC hdc = pDC->GetSafeHdc();
- HPEN newPen = ::CreatePen(m_borderStyle,
- m_borderWidth,m_borderColor^pDC->GetBkColor());
- HBRUSH newBrush = ::CreateSolidBrush(m_backColor^ GetBkColor(hdc));
- ::SelectObject(hdc,newPen);
- ::SelectObject(hdc,newBrush);
- int oldR2 = ::SetROP2(hdc,R2_XORPEN);
- POINT topPt[4] ={onePt.x,onePt.y, twoPt.x,twoPt.y,threePt.x,threePt.y, fourPt.x,fourPt.y};
- POINT OldTopPt[4] ={oneOdPt.x,oneOdPt.y, twoOdPt.x,twoOdPt.y,threeOdPt.x,threeOdPt.y, fourOdPt.x,fourOdPt.y};
- Polygon( hdc, OldTopPt, 4 );
- Polygon( hdc, topPt, 4 );
- SetROP2(hdc,oldR2);
- ::DeleteObject(newPen);
- ::DeleteObject(newBrush);
- }
- /*功 能:求一点关于直线对称的点
- 输入参数:直线起点:ptBegin,直线终点:ptEnd,待求点:ptCur
- 返 回 值:对称点ptNew */
- POINT CRectangle::GetMirPt(const POINT& ptBegin, const POINT& ptEnd,POINT ptCur)
- {
- POINT ptNew;
- double length = Distance(ptBegin,ptEnd);
- if(length == 0)
- return ptNew=ptCur;
- double t1 = 2. * ((ptEnd.x-ptBegin.x)/length) * ((ptEnd.y-ptBegin.y)/length);
- double t2 = ((ptEnd.x-ptBegin.x)/length) * ((ptEnd.x-ptBegin.x)/length)
- - ((ptEnd.y-ptBegin.y)/length) * ((ptEnd.y-ptBegin.y)/length);
- ptNew.x =(int)(ptCur.x*t2 + ptCur.y*t1 + ptBegin.x*(-t2) - ptBegin.y*t1 + ptBegin.x);
- ptNew.y =(int)(ptCur.x*t1 + ptCur.y*(-t2) + ptBegin.y*t2 - ptBegin.x*t1 + ptBegin.y);
- return ptNew;
- }
- //求两点之间的距离
- int CRectangle::Distance(POINT Pt1, POINT Pt2)
- {
- int nDist;
- nDist =(int)(sqrt((Pt1.x - Pt2.x)*(Pt1.x - Pt2.x) + (Pt1.y - Pt2.y)*(Pt1.y - Pt2.y)));
- return nDist;
- }
- //镜像时左键点击
- void CRectangle::OnMirLBtnDn(CDC *pDC,CPoint point)
- {
- HDC hdc = pDC->GetSafeHdc();
- /* POINT curPoint; //左键点击的点
- curPoint.x = LOWORD(lParam); //取低16位 x 值
- curPoint.y = HIWORD(lParam); //取高16位 y 值*/
- m_MirBegPt = m_MirEndPt = m_oldPt = point;
- }
- void CRectangle::Delete(CDC *pDC) //删除
- {
- Update(pDC);
- HDC hdc = pDC->GetSafeHdc();
- HPEN newPen = CreatePen(m_borderStyle,
- m_borderWidth,GetBkColor(hdc));
- SelectObject(hdc,GetStockObject(NULL_BRUSH)); //空画刷
- ::SelectObject(hdc,newPen);
- Rectangle(hdc,m_begPt.x,m_begPt.y, m_endPt.x,m_endPt.y);
- ::DeleteObject(newPen);
- }