LineTool.cpp
资源名称:CAD.zip [点击查看]
上传用户:hehe2haha
上传日期:2013-08-16
资源大小:161k
文件大小:3k
源码类别:
CAD
开发平台:
Visual C++
- // LineTool.cpp: implementation of the CLineTool class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "Line.h"
- #include "LineTool.h"
- #include "WinApp.h"
- extern CWinApp g_theApp;
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CLineTool::CLineTool()
- {
- }
- CLineTool::~CLineTool()
- {
- }
- void CLineTool::OnMouseMove(HWND hWnd, UINT nFlags, POINT ptPos)
- {
- CDataBase *pDataBase = &(g_theApp.m_DataBase);
- if(m_eToolState == FirstHit)
- {
- HDC hdc = ::GetDC(hWnd);
- //设置异或模式
- int nOldMode = ::SetROP2(hdc,R2_XORPEN);
- //新建逻辑笔
- HPEN hPen = ::CreatePen( pDataBase->m_Shape.fnPenStyle,
- pDataBase->m_Shape.nWidth,
- pDataBase->m_Shape.crColor^ GetBkColor(hdc) );
- HPEN hOldPen = (HPEN)::SelectObject(hdc,hPen);
- //擦去前一次线段
- CDraw::DrawLine(hdc,m_ptBeginPos,m_ptPrePos);
- //画出这一次线段
- CDraw::DrawLine(hdc,m_ptBeginPos,ptPos);
- //回恢复原有模式
- ::SetROP2(hdc,nOldMode);
- //放回原来的笔
- ::SelectObject(hdc,hOldPen);
- //删除新建的笔
- ::DeleteObject(hPen);
- ::ReleaseDC(hWnd,hdc);
- m_ptPrePos = ptPos;
- }
- }
- //功能:取消画线
- void CLineTool::OnRButtonDown(HWND hWnd,UINT nFlags, POINT ptPos)
- {
- CDataBase *pDataBase = &(g_theApp.m_DataBase);
- if(m_eToolState == FirstHit)
- {
- HDC hdc = ::GetDC(hWnd);
- int nOldMode = ::SetROP2(hdc,R2_XORPEN);
- HPEN hPen = ::CreatePen( pDataBase->m_Shape.fnPenStyle,
- pDataBase->m_Shape.nWidth,
- pDataBase->m_Shape.crColor^ GetBkColor(hdc) );
- HPEN hOldPen = (HPEN)::SelectObject(hdc,hPen);
- //擦去前一次线段
- CDraw::DrawLine(hdc,m_ptBeginPos,m_ptPrePos);
- ::SetROP2(hdc,nOldMode);
- ::SelectObject(hdc,hOldPen);
- ::DeleteObject(hPen);
- ::ReleaseDC(hWnd,hdc);
- }
- //取消画图状态
- OverCurTool(hWnd);
- }
- void CLineTool::OnLButtonDown(HWND hWnd,UINT nFlags, POINT ptPos)
- {
- CDataBase *pDataBase = &(g_theApp.m_DataBase);
- CArray<CShape*> *pArrayShape = &(g_theApp.m_DataBase.m_ArrayShape);
- CLine *pShape = NULL;
- if(m_eToolState == Init_State)
- {
- m_eToolState = FirstHit;
- m_ptBeginPos = m_ptPrePos = ptPos;
- }
- else if(m_eToolState == FirstHit)
- {
- m_eToolState = SecondHit;
- pShape = new CLine;
- pShape->m_ptFirstPos = m_ptBeginPos;
- pShape->m_ptSecondPos = ptPos;
- //保存图形样式
- pShape->crColor = pDataBase->m_Shape.crColor;
- pShape->fnPenStyle = pDataBase->m_Shape.fnPenStyle;
- pShape->nWidth = pDataBase->m_Shape.nWidth;
- //第一点和第二点不重合才保存
- if (pShape->m_ptFirstPos.x != ptPos.x ||
- pShape->m_ptFirstPos.y != ptPos.y)
- {
- //保存至数组
- pArrayShape->Add(pShape);
- }
- else
- {
- delete pShape;
- pShape = NULL;
- }
- //取消画图状态
- OverCurTool(hWnd);
- }
- }