Rectangle.cpp
资源名称:CAD.zip [点击查看]
上传用户:hehe2haha
上传日期:2013-08-16
资源大小:161k
文件大小:3k
源码类别:
CAD
开发平台:
Visual C++
- // Rectangle.cpp: implementation of the CRectangle class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "Rectangle.h"
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CRectangle::CRectangle()
- {
- strcpy(m_className,"CRectangle");
- }
- CRectangle::~CRectangle()
- {
- }
- void CRectangle::Draw(HDC hdc)
- {
- HPEN hPen = ::CreatePen( fnPenStyle,nWidth,crColor);
- HPEN hOldPen = (HPEN)::SelectObject(hdc,hPen);
- //不进行填充
- ::SelectObject(hdc,GetStockObject(NULL_BRUSH));
- Rectangle( hdc,
- m_ptFirstPos.x,
- m_ptFirstPos.y,
- m_ptSecondPos.x,
- m_ptSecondPos.y
- );
- ::SelectObject(hdc,hOldPen);
- ::DeleteObject(hPen);
- if(isPick == TRUE)
- {
- //恢复填充
- ::SelectObject(hdc,GetStockObject(WHITE_BRUSH));
- DrawRect(hdc);
- }
- }
- void CRectangle::DrawRect(HDC hdc)
- {
- //Rect为标记圆的半径
- //top left
- Ellipse(hdc,m_ptFirstPos.x - Rect,m_ptFirstPos.y - Rect,
- m_ptFirstPos.x + Rect,m_ptFirstPos.y + Rect);
- //top right
- Ellipse(hdc,m_ptSecondPos.x - Rect,m_ptFirstPos.y - Rect,
- m_ptSecondPos.x + Rect,m_ptFirstPos.y + Rect);
- //top middle
- Ellipse(hdc,mPtx - Rect,m_ptFirstPos.y - Rect,
- mPtx + Rect,m_ptFirstPos.y + Rect);
- //left middle
- Ellipse(hdc,m_ptFirstPos.x - Rect,mPty - Rect,
- m_ptFirstPos.x + Rect,mPty + Rect);
- //right middle
- Ellipse(hdc,m_ptSecondPos.x - Rect,mPty - Rect,
- m_ptSecondPos.x + Rect,mPty + Rect);
- //buttom middle
- Ellipse(hdc,mPtx - Rect,m_ptSecondPos.y - Rect,
- mPtx + Rect,m_ptSecondPos.y + Rect);
- //buttom left
- Ellipse(hdc,m_ptFirstPos.x - Rect,m_ptSecondPos.y - Rect,
- m_ptFirstPos.x + Rect,m_ptSecondPos.y + Rect);
- //buttom right
- Ellipse(hdc,m_ptSecondPos.x - Rect,m_ptSecondPos.y - Rect,
- m_ptSecondPos.x + Rect,m_ptSecondPos.y + Rect);
- }
- BOOL CRectangle::pick(POINT pt)
- {
- //矩形中心坐标
- mPtx = (m_ptSecondPos.x + m_ptFirstPos.x) / 2;
- mPty = (m_ptSecondPos.y + m_ptFirstPos.y) / 2;
- //矩形的长和宽
- int hLength = abs(m_ptSecondPos.x - m_ptFirstPos.x) / 2;
- int hWidth = abs(m_ptSecondPos.y - m_ptFirstPos.y) / 2;
- //拾取的有效范围正负5个象素
- if (pt.x < m_ptFirstPos.x + 5 && pt.x > m_ptFirstPos.x - 5)
- {
- if (pt.y < mPty + hWidth + 5 && pt.y > mPty - hWidth - 5 )
- return TRUE;
- }
- if (pt.x < m_ptSecondPos.x + 5 && pt.x > m_ptSecondPos.x - 5)
- {
- if (pt.y < mPty + hWidth + 5 && pt.y > mPty - hWidth - 5 )
- return TRUE;
- }
- if (pt.y < m_ptFirstPos.y + 5 && pt.y > m_ptFirstPos.y - 5)
- {
- if (pt.x < mPtx + hLength + 5 && pt.x > mPtx - hLength - 5 )
- return TRUE;
- }
- if (pt.y < m_ptSecondPos.y + 5 && pt.y > m_ptSecondPos.y - 5)
- {
- if (pt.x < mPtx + hLength + 5 && pt.x > mPtx - hLength - 5 )
- return TRUE;
- }
- return FALSE;
- }