Line1.cpp
资源名称:CAD2006.rar [点击查看]
上传用户:ckg1000
上传日期:2013-01-26
资源大小:630k
文件大小:11k
源码类别:
CAD
开发平台:
Visual C++
- // Line1.cpp: implementation of the CLine class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "CAD2006.h"
- #include "Line1.h"
- #include "math.h"
- #include "Trigon.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- extern enum STATE;
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CLine::CLine()
- {
- m_toolState = noClick;
- blnlinetemp = true;
- }
- CLine::~CLine()
- {
- }
- void CLine::Draw(CDC *pDC)
- {
- HPEN hPen = ::CreatePen(m_borderStyle,
- m_borderWidth,m_borderColor^pDC->GetBkColor());
- //将创建的HPEN选入DeviceContext,并把默认HPEN保存到hOldPen
- HPEN hOldPen = (HPEN)pDC->SelectObject(hPen);
- //设置混合模式
- int oldR2 = pDC->SetROP2(R2_XORPEN);
- //以前背景异或方式两次绘制直线
- pDC->MoveTo(m_ptBeginPos.x, m_ptBeginPos.y);
- pDC->LineTo(m_ptOldPos.x, m_ptOldPos.y);
- pDC->MoveTo(m_ptBeginPos.x, m_ptBeginPos.y);
- pDC->LineTo(m_ptEndPos.x, m_ptEndPos.y);
- //恢复默认异或方式
- pDC->SetROP2(oldR2);
- //将默认HPEN返还到DeviceContext
- pDC->SelectObject(hOldPen);
- //删除创建的HPEN
- ::DeleteObject(hPen);
- }
- void CLine::OnLbuttondown(CDC *pDC,CPoint point)
- {
- POINT currentPos; //当前鼠标坐标
- currentPos.x = point.x;
- currentPos.y = point.y;
- switch(m_toolState)
- {
- case noClick: //工具状态为noClick时
- {
- //确定(一次画线行为) 的起始点坐标 m_ptBeginPos
- m_ptBeginPos = m_ptOldPos = point;
- //状态转换为clicked
- m_toolState = Clicked;
- }
- break;
- case Clicked: //工具状态为Clicked时
- {
- //确定(一次画线行为) 的结束点坐标 m_ptEndPos
- m_ptEndPos = point;
- //状态转换为noClick
- m_toolState = noClick;
- }
- break;
- }
- }
- //响应WM_MOUSEMOVE消息
- void CLine::Onmousemove(CDC *pDC,CPoint point)
- {
- POINT currentPos; //当前鼠标坐标
- currentPos.x = point.x;
- currentPos.y = point.y;
- m_ptEndPos = currentPos;
- if(m_toolState == Clicked)
- {
- Draw(pDC);
- m_ptOldPos = point;
- }
- }
- CPoint CLine::GetPosBegin()
- {
- if( blnlinetemp )
- {
- return m_ptBeginPos;
- }
- else
- {
- return m_FirstMirPt;
- }
- }
- CPoint CLine::GetPosEnd()
- {
- if( blnlinetemp )
- {
- return m_ptEndPos;
- }
- else
- {
- return m_SecondMirPt;
- }
- }
- bool CLine::pick(POINT pt)
- {
- int dblLength = Distance(m_ptBeginPos,m_ptEndPos);
- int dblLength1 = Distance(m_ptBeginPos,pt);
- int dblLength2 = Distance(m_ptEndPos,pt);
- if(dblLength1 + dblLength2 - dblLength < 3)
- return true;
- else
- return false;
- }
- int CLine::Distance(POINT ptPos1, POINT ptPos2)
- {
- double dbD = sqrt( (ptPos1.x - ptPos2.x) * (ptPos1.x - ptPos2.x) +
- (ptPos1.y - ptPos2.y) *(ptPos1.y - ptPos2.y) );
- return (int)dbD;
- }
- bool CLine::Select(CDC *pDC,CPoint point)
- {
- if( pick(point) )
- {
- HDC hdc = pDC->GetSafeHdc();
- SelectObject(hdc,GetStockObject(WHITE_BRUSH));
- DrawRect(pDC);
- m_ptOldPos = point;
- // KILLRECT = true;
- return true;
- }
- return false;
- }
- void CLine::DrawRect(CDC *pDC)
- {
- HDC hdc = pDC->GetSafeHdc();
- HPEN hpen;
- int i = ::SetROP2(hdc,R2_XORPEN);
- hpen = ::CreatePen(0,0,RGB(255,0,0)^::GetBkColor(hdc));
- HPEN holdpen =(HPEN) ::SelectObject(hdc,hpen);
- ::SelectObject(hdc,GetStockObject(NULL_BRUSH));
- Ellipse(hdc,m_ptBeginPos.x - 4,m_ptBeginPos.y - 4,
- m_ptBeginPos.x + 4,m_ptBeginPos.y + 4);
- Ellipse(hdc,m_ptEndPos.x - 4,m_ptEndPos.y - 4,
- m_ptEndPos.x + 4,m_ptEndPos.y + 4);
- ::SetROP2(hdc,i);
- ::SelectObject(hdc,holdpen);
- ::DeleteObject(hpen);
- }
- void CLine::SetPosBegin(CPoint point)
- {
- m_ptBeginPos = point;
- }
- void CLine::SetPosEnd(CPoint point)
- {
- m_ptEndPos = point;
- }
- void CLine::Move(CDC *pDoc, CPoint point)
- {
- HDC hdc = pDoc->GetSafeHdc();
- int nOldMode = ::SetROP2(hdc,R2_XORPEN);
- HPEN hPen = ::CreatePen( m_borderStyle,
- m_borderWidth,m_borderColor^pDoc->GetBkColor());
- HPEN hOldPen = (HPEN)::SelectObject(hdc,hPen);
- //擦去前一次线段
- ::MoveToEx(hdc,m_ptBeginPos.x,m_ptBeginPos.y,NULL);
- ::LineTo(hdc,m_ptEndPos.x,m_ptEndPos.y);
- m_ptBeginPos.x += point.x - m_ptOldPos.x;
- m_ptBeginPos.y += point.y - m_ptOldPos.y;
- m_ptEndPos.x += point.x- m_ptOldPos.x;
- m_ptEndPos.y += point.y- m_ptOldPos.y;
- //画出这一次线段
- ::MoveToEx(hdc,m_ptBeginPos.x,m_ptBeginPos.y,NULL);
- ::LineTo(hdc,m_ptEndPos.x,m_ptEndPos.y);
- m_ptOldPos = point;
- ::SetROP2(hdc,nOldMode);
- ::SelectObject(hdc,hOldPen);
- ::DeleteObject(hPen);
- if (KILLRECT == true)
- {
- pDoc->SelectObject(GetStockObject(WHITE_BRUSH));
- DrawRect(pDoc);
- KILLRECT = false;
- }
- }
- void CLine::Rotate(CDC *pDC, CPoint point)
- {
- CPoint centerpoint;
- centerpoint.x =m_ptBeginPos.x - ( m_ptBeginPos.x-m_ptEndPos.x ) / 2;
- centerpoint.y =m_ptBeginPos.y - ( m_ptBeginPos.y-m_ptEndPos.y ) / 2;
- HDC hdc = pDC->GetSafeHdc();
- int nOldMode = ::SetROP2(hdc,R2_XORPEN);
- HPEN hPen = ::CreatePen( m_borderStyle,
- m_borderWidth,m_borderColor^pDC->GetBkColor());
- HPEN hOldPen = (HPEN)::SelectObject(hdc,hPen);
- CPoint oldfirstpoint = m_ptBeginPos;
- CPoint oldsecondpoiont = m_ptEndPos;
- //擦去前一次线段
- ::MoveToEx(hdc,oldfirstpoint.x,oldfirstpoint.y,NULL);
- ::LineTo(hdc,m_ptEndPos.x,m_ptEndPos.y);
- double Pi = 3.14159;
- double radian = Pi * m_angle/180; //化成弧度
- m_ptBeginPos = CalRotatePt(centerpoint,m_ptBeginPos,radian);
- m_ptEndPos = CalRotatePt(centerpoint,m_ptEndPos,radian);
- //画出这一次线段
- ::MoveToEx(hdc,m_ptBeginPos.x,m_ptBeginPos.y,NULL);
- ::LineTo(hdc,m_ptEndPos.x,m_ptEndPos.y);
- m_ptOldPos = point;
- ::SetROP2(hdc,nOldMode);
- ::SelectObject(hdc,hOldPen);
- ::DeleteObject(hPen);
- }
- void CLine::Zoom(CDC *pDoc, CPoint point)
- {
- HDC hdc = pDoc->GetSafeHdc();
- int nOldMode = ::SetROP2(hdc,R2_XORPEN);
- HPEN hPen = ::CreatePen( m_borderStyle,
- m_borderWidth,m_borderColor^pDoc->GetBkColor());
- HPEN hOldPen = (HPEN)::SelectObject(hdc,hPen);
- CPoint oldfirstpoint = m_ptBeginPos;
- // CPoint oldsecondpoiont = m_ptEndPos;
- //擦去前一次线段
- ::MoveToEx(hdc,oldfirstpoint.x,oldfirstpoint.y,NULL);
- ::LineTo(hdc,m_ptEndPos.x,m_ptEndPos.y);
- m_ptBeginPos.x += point.x - m_ptOldPos.x;
- m_ptBeginPos.y += point.y - m_ptOldPos.y;
- // m_ptEndPos.x += point.x- m_ptOldPos.x;
- // m_ptEndPos.y += point.y- m_ptOldPos.y;
- //画出这一次线段
- ::MoveToEx(hdc,m_ptBeginPos.x,m_ptBeginPos.y,NULL);
- ::LineTo(hdc,m_ptEndPos.x,m_ptEndPos.y);
- m_ptOldPos = point;
- ::SetROP2(hdc,nOldMode);
- ::SelectObject(hdc,hOldPen);
- ::DeleteObject(hPen);
- }
- bool CLine::SelectOnePt(CDC *pDC, CPoint point)
- {
- if( point.x >= m_ptBeginPos.x && point.y >= m_ptBeginPos.y && point.x <= m_ptBeginPos.x + 2 && point.y <= m_ptBeginPos.y +2 )
- {
- m_ptOldPos = point;
- DrawRect(pDC);
- return true;
- }
- return false;
- }
- // 功 能: 计算点绕指定点旋转dbAngle个角度后的点
- // 输入参数: 定点:POINT ptCenter 待求点:POINT ptPos
- // 弧度 double dbAngle
- // 返 回 值: 旋转到的位置 POINT ptDest;
- POINT CLine::CalRotatePt(POINT ptCenter,POINT ptPos,double dbAngle)
- {
- struct tagMatrix //旋转矩阵
- {
- double r11, r12, r13;
- double r21, r22, r23;
- double r31, r32, r33;
- }matrix;
- matrix.r11 = (double)cos(dbAngle);
- matrix.r21 = (double)sin(dbAngle);
- matrix.r31 = 0.0;
- matrix.r12 = (double)(-sin(dbAngle));
- matrix.r22 = (double)cos(dbAngle);
- matrix.r32 = 0.0;
- matrix.r13 = (double)((1 - cos(dbAngle)) * ptCenter.x + ptCenter.y * sin(dbAngle));
- matrix.r23 = (double)((1 - cos(dbAngle)) * ptCenter.y - ptCenter.x * sin(dbAngle));
- matrix.r33 = 1.0;
- POINT ptDest;
- ptDest.x = (int)(matrix.r11 * ptPos.x + matrix.r12 * ptPos.y + matrix.r13);
- ptDest.y = (int)(matrix.r21 * ptPos.x + matrix.r22 * ptPos.y + matrix.r23);
- return ptDest;
- }
- void CLine::Mirror(CDC *pDoc, CPoint point)
- {
- POINT curPt;
- curPt = point;
- m_ptOldPos = m_MirEndPt;
- m_MirEndPt = curPt;
- int nOldMode = pDoc->SetROP2(R2_XORPEN);
- POINT FirstMirOldPt = m_FirstMirPt; //镜像后的老点
- POINT SecondMirOldPt = m_SecondMirPt;
- m_FirstMirPt = GetMirPt(m_MirBegPt, m_MirEndPt, m_ptBeginPos);
- m_SecondMirPt = GetMirPt(m_MirBegPt, m_MirEndPt, m_ptEndPos);
- DrawMirLine(pDoc,m_MirBegPt,m_MirEndPt); //镜像线
- HPEN hPen = ::CreatePen( 0,1,RGB(0,0,0)^pDoc->GetBkColor());
- HPEN hOldPen = (HPEN)pDoc->SelectObject(hPen);
- pDoc->MoveTo(FirstMirOldPt.x,FirstMirOldPt.y);
- pDoc->LineTo(SecondMirOldPt.x,SecondMirOldPt.y);
- pDoc->MoveTo(m_FirstMirPt.x,m_FirstMirPt.y);
- pDoc->LineTo(m_SecondMirPt.x,m_SecondMirPt.y);
- m_oldPt = curPt;
- pDoc->SetROP2(nOldMode);
- pDoc->SelectObject(hOldPen);
- ::DeleteObject(hPen);
- blnlinetemp = false;
- }
- void CLine::DrawMirLine(CDC *pDC, POINT MirBegPt, POINT MirEndPt)
- {
- HPEN newPen = CreatePen(0,0,RGB(255,0,0)^pDC->GetBkColor());
- HPEN oldPen = (HPEN)pDC->SelectObject(newPen);
- int oldR2 = pDC->SetROP2(R2_XORPEN);
- pDC->MoveTo(MirBegPt.x,MirBegPt.y);
- pDC->LineTo(m_oldPt.x,m_oldPt.y);
- pDC->MoveTo(MirBegPt.x,MirBegPt.y);
- pDC->LineTo(MirEndPt.x,MirEndPt.y);
- pDC->SetROP2(oldR2);
- pDC->SelectObject(oldPen);
- ::DeleteObject(newPen);
- ::DeleteObject(oldPen);
- }
- void CLine::KillMirLine(CDC *pDC)
- {
- HPEN newPen = CreatePen(0,0,pDC->GetBkColor());
- HPEN oldPen = (HPEN)pDC->SelectObject(newPen);
- pDC->MoveTo(m_MirBegPt.x,m_MirBegPt.y);
- pDC->LineTo(m_MirEndPt.x,m_MirEndPt.y);
- pDC->SelectObject(oldPen);
- DeleteObject(newPen);
- ::DeleteObject(oldPen);
- }
- POINT CLine::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;
- }
- void CLine::OnMirLBtnDn(CDC *pDC, CPoint point)
- {
- POINT curPoint; //左键点击的点
- curPoint.x = point.x ;
- curPoint.y = point.y ;
- m_MirBegPt = m_MirEndPt = m_oldPt = curPoint;
- }
- void CLine::Update(CDC *pDC)
- {
- KillMirLine(pDC);
- }
- void CLine::SetAngle(int m_angle)
- {
- ::m_angle = m_angle;
- }
- void CLine::Delete(CDC *pDC) //删除
- {
- HDC hdc = pDC->GetSafeHdc();
- DrawRect(pDC);
- HPEN newPen = CreatePen(m_borderStyle,
- m_borderWidth,GetBkColor(hdc));
- // SelectObject(hdc,GetStockObject(NULL_BRUSH)); //空画刷
- ::SelectObject(hdc,newPen);
- pDC->MoveTo(m_ptBeginPos);
- pDC->LineTo(m_ptEndPos);
- ::DeleteObject(newPen);
- }