RectangleTool.cpp
资源名称:CAD.zip [点击查看]
上传用户:hehe2haha
上传日期:2013-08-16
资源大小:161k
文件大小:3k
源码类别:
CAD
开发平台:
Visual C++
- // RectangleTool.cpp: implementation of the CRectangleTool class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "RectangleTool.h"
- #include "Rectangle.h"
- #include "WinApp.h"
- extern CWinApp g_theApp;
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CRectangleTool::CRectangleTool()
- {
- }
- CRectangleTool::~CRectangleTool()
- {
- }
- void CRectangleTool::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);
- //不进行填充
- ::SelectObject(hdc,GetStockObject(NULL_BRUSH));
- //擦去前一次线段
- ::Rectangle( hdc,
- m_ptBeginPos.x,
- m_ptBeginPos.y,
- m_ptPrePos.x,
- m_ptPrePos.y);
- //画出这一次线段
- ::Rectangle( hdc,
- m_ptBeginPos.x,
- m_ptBeginPos.y,
- ptPos.x,
- ptPos.y);
- ::SetROP2(hdc,nOldMode);
- ::SelectObject(hdc,hOldPen);
- ::DeleteObject(hPen);
- ::ReleaseDC(hWnd,hdc);
- m_ptPrePos = ptPos;
- }
- }
- void CRectangleTool::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) );
- ::SelectObject(hdc,hPen);
- //不进行填充
- ::SelectObject(hdc,GetStockObject(NULL_BRUSH));
- //擦去前一次线段
- ::Rectangle( hdc,
- m_ptBeginPos.x,
- m_ptBeginPos.y,
- m_ptPrePos.x,
- m_ptPrePos.y);
- ::SetROP2(hdc,nOldMode);
- ::ReleaseDC(hWnd,hdc);
- }
- //取消画图状态
- OverCurTool(hWnd);
- }
- void CRectangleTool::OnLButtonDown(HWND hWnd,UINT nFlags, POINT ptPos)
- {
- CDataBase *pDataBase = &(g_theApp.m_DataBase);
- CArray<CShape*> *pArrayShape = &(g_theApp.m_DataBase.m_ArrayShape);
- CRectangle *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 CRectangle;
- //保存图形样式
- pShape->crColor = pDataBase->m_Shape.crColor;
- pShape->fnPenStyle = pDataBase->m_Shape.fnPenStyle;
- pShape->nWidth = pDataBase->m_Shape.nWidth;
- pShape->m_ptFirstPos = m_ptBeginPos;
- pShape->m_ptSecondPos = ptPos;
- //第一点和第二点不重合才保存
- if (pShape->m_ptFirstPos.x != ptPos.x ||
- pShape->m_ptFirstPos.y != ptPos.y)
- {
- //保存至数组
- pArrayShape->Add(pShape);
- }
- else
- {
- delete pShape;
- pShape = NULL;
- }
- //取消画图状态
- OverCurTool(hWnd);
- }
- }