📄 drawobject.cpp
字号:
#include "StdAfx.h"
#include "DrawObject.h"
CDrawObject::CDrawObject()
{
m_rect.SetRectEmpty();
}
CDrawObject::~CDrawObject(void)
{
}
void CDrawObject::SetBounds(const CPoint& ptTopLeft, const CPoint& ptBottomRigth)
{
m_rect.SetRect(ptTopLeft,ptBottomRigth);
m_rect.NormalizeRect();
}
//--------------------CDrawPen------------------
CDrawPen::CDrawPen()
{
m_penStyle = MyPenStyle::psDot1;
}
CDrawPen::~CDrawPen(void)
{
}
void CDrawPen::SetPenStyle(COLORREF color, UINT style,float fZoom)
{
LOGBRUSH lb;
lb.lbColor = color;
HDC hDc= ::GetDC(NULL);
CUtility::PrepareDC(hDc);
CPoint pt(1,1);
::DPtoLP(hDc,&pt,1);
m_penStyle = style;
m_colorFore = color;
switch (style)
{
case MyPenStyle::psDot1:
{
m_penCur.DeleteObject();
lb.lbStyle = BS_SOLID;
lb.lbHatch = HS_BDIAGONAL;
m_penCur.CreatePen(PS_GEOMETRIC|PS_ENDCAP_SQUARE,30 , &lb, 0, NULL);
break;
}
case MyPenStyle::psDot2:
{
m_penCur.DeleteObject();
m_penCur.CreatePen(PS_SOLID, 100, color);
break;
}
case MyPenStyle::psDot3:
{
m_penCur.DeleteObject();
m_penCur.CreatePen(PS_SOLID, 300, color);
break;
}
default:
{
break;
}
}
}
void CDrawPen::DrawSelf(CDC* pDC)
{
if (!m_pointArray.IsEmpty())
{
if (m_penStyle == MyPenStyle::psRect1
|| m_penStyle == MyPenStyle::psRect2
|| m_penStyle == MyPenStyle::psRect3)
{
CBrush brush (m_colorFore);
int cX = 50;
switch (m_penStyle)
{
case MyPenStyle::psRect2:
cX = 100;
break;
case MyPenStyle::psRect3:
cX = 150;
break;
default:
break;
}
for (int i=0; i < m_pointArray.GetSize(); i++)
{
pDC->FillRect(CRect(m_pointArray[i].x-cX, m_pointArray[i].y-cX,m_pointArray[i].x+cX, m_pointArray[i].y+cX),&brush);
}
}
else
{
CPen* pOldPen = pDC->SelectObject(&m_penCur);
pDC->MoveTo(m_pointArray[0]);
for (int i=0; i < m_pointArray.GetSize(); i++)
{
pDC->LineTo(m_pointArray[i]);
}
pDC->SelectObject(pOldPen);
}
}
}
void CDrawPen::AdjustBounds()
{
if (m_pointArray.GetSize()==0)
{
m_rect.SetRectEmpty();
return;
}
CPoint pt = m_pointArray[0];
m_rect = CRect(pt.x, pt.y, pt.x, pt.y);
for (int i=1; i < m_pointArray.GetSize(); i++)
{
pt = m_pointArray[i];
m_rect.left = min(m_rect.left, pt.x);
m_rect.right = max(m_rect.right, pt.x);
m_rect.top = min(m_rect.top, pt.y);
m_rect.bottom = max(m_rect.bottom, pt.y);
}
m_rect.InflateRect(200,200);
return;
}
//------------------------------------------------------------------------
CDrawShape::CDrawShape(UINT type,const CRect& rect)
{
m_ShapeType = type;
m_rect = rect;
if (m_ShapeType != ShapeType::line)
m_rect.NormalizeRect();
}
void CDrawShape::SetBounds(const CPoint& ptTopLeft, const CPoint& ptBottomRigth)
{
m_rect.SetRect(ptTopLeft,ptBottomRigth);
if (m_ShapeType != ShapeType::line)
m_rect.NormalizeRect();
}
CDrawShape::~CDrawShape(void)
{
}
void CDrawShape::DrawSelf(CDC* pDC)
{
CPen* pOldPen = pDC->SelectObject(&m_penCur);
CBrush* pOldBrush = pDC->SelectObject(&m_brushCur);
switch (m_ShapeType)
{
case ShapeType::line:
{
pDC->MoveTo(m_rect.TopLeft());
pDC->LineTo(m_rect.BottomRight());
break;
}
case ShapeType::rect:
{
pDC->Rectangle(m_rect);
break;
}
case ShapeType::ellipse:
{
pDC->Ellipse(m_rect);
break;
}
case ShapeType::roundrect:
{
CPoint pt(m_rect.Width()/3, m_rect.Height()/3);
pDC->RoundRect(m_rect,pt);
break;
}
default:
break;
}
pDC->SelectObject(pOldPen);
pDC->SelectObject(pOldBrush);
}
void CDrawShape::SetShapeStyle(COLORREF colorFore,COLORREF colorBack,UINT lineWidth,UINT shapeStyle,float fZoom)
{
m_penCur.DeleteObject();
m_brushCur.DeleteObject();
switch (shapeStyle)
{
case ShapeStyle::styleFrameOnly:
{
m_penCur.CreatePen(PS_SOLID,lineWidth,colorFore);
LOGBRUSH lb;
lb.lbStyle = BS_NULL;
m_brushCur.CreateBrushIndirect(&lb);
break;
}
case ShapeStyle::styleFillAndFrame:
{
m_penCur.CreatePen(PS_SOLID,lineWidth,colorFore);
m_brushCur.CreateSolidBrush(colorBack);
break;
}
case ShapeStyle::styleFillOnly:
{
m_penCur.CreatePen(PS_NULL,1,colorFore);
m_brushCur.CreateSolidBrush(colorFore);
break;
}
default:
break;
}
}
//------------------------------------------------------------------------------------
CDrawArea::CDrawArea(COLORREF colorBack)
{
m_colorBack = colorBack;
m_rectOrig.SetRectEmpty();
m_bHasMoved = FALSE;
}
CDrawArea::~CDrawArea(void)
{
m_imgArea.Destroy();
}
void CDrawArea::DrawSelf(CDC* pDC)
{
//背景
CBrush brushBack(m_colorBack);
pDC->FillRect(m_rectOrig,&brushBack);
//剪切区
//CSize sizePix(m_rect.Width(),m_rect.Height());
//pDC->LPtoDP(&sizePix);
if (!m_imgArea.IsNull())
m_imgArea.StretchBlt(pDC->GetSafeHdc(),
m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
0,0,m_imgArea.GetWidth(),m_imgArea.GetHeight(),
SRCCOPY);
//边框
//this->DrawTracker(pDC);
}
void CDrawArea::SetOrigArea(const CPoint& ptTopLeft, const CPoint& ptBottomRigth, HBITMAP hBMP)
{
m_rectOrig.SetRect(ptTopLeft,ptBottomRigth);
m_rectOrig.NormalizeRect();
m_rect.CopyRect(m_rectOrig);
m_imgArea.Destroy();
m_imgArea.Attach(hBMP);
}
void CDrawArea::SetBounds(const CPoint& ptTopLeft, const CPoint& ptBottomRigth)
{
m_bHasMoved = TRUE;
m_rect.SetRect(ptTopLeft,ptBottomRigth);
m_rect.NormalizeRect();
}
// 绘制选中后的边界点
void CDrawObject::DrawTracker(CDC* pDC)
{
CPen pen(PS_DASH,1,RGB(0,0,0));
LOGBRUSH logBrush;
logBrush.lbStyle = BS_NULL;
CBrush brushNull;
brushNull.CreateBrushIndirect(&logBrush);
CPen *pOldPen = pDC->SelectObject(&pen);
CBrush *pOldBrush = pDC->SelectObject(&brushNull);
pDC->Rectangle(m_rect);
pDC->SelectObject(pOldPen);
pDC->SelectObject(pOldBrush);
int nHandleCount = 8;
for (int nHandle = 1; nHandle <= nHandleCount; nHandle += 1)
{
CPoint handle = GetHandle(nHandle);
pDC->PatBlt(handle.x - 50, handle.y - 50, 100, 100, DSTINVERT);
}
}
int CDrawObject::HitTest(CPoint point, BOOL bSelected)
{
if (this)
{
CRect runtimeRect(this->GetBoundRect());
int nHandleCount = 8;
for (int nHandle = 1; nHandle <= nHandleCount; nHandle += 1)
{
// GetHandleRect returns in logical coords
CRect rc = GetHandleRect(nHandle);
if (rc.PtInRect(point))
return nHandle;
}
}
return 0;
}
// 获取拖拽点区域
const int HandleSize = 50;
CRect CDrawObject::GetHandleRect(int nHandleID)
{
CPoint point = GetHandle(nHandleID);
CRect rect(point.x-HandleSize, point.y-HandleSize, point.x+HandleSize, point.y+HandleSize);
rect.NormalizeRect();
return rect;
}
CPoint CDrawObject::GetHandle(int nHandle)
{
int x, y, xCenter, yCenter;
CRect runtimeRect = GetBoundRect();
// this gets the center regardless of left/right and top/bottom ordering
xCenter = runtimeRect.left + runtimeRect.Width() / 2;
yCenter = runtimeRect.top + runtimeRect.Height() / 2;
switch (nHandle)
{
default:
ASSERT(FALSE);
case 1: // left-top
x = runtimeRect.left;
y = runtimeRect.top;
break;
case 2: // top-middle
x = xCenter;
y = runtimeRect.top;
break;
case 3: // right-top
x = runtimeRect.right;
y = runtimeRect.top;
break;
case 4: // right-middle
x = runtimeRect.right;
y = yCenter;
break;
case 5: // right-bottom
x = runtimeRect.right;
y = runtimeRect.bottom;
break;
case 6: // bottom-middle
x = xCenter;
y = runtimeRect.bottom;
break;
case 7: // left-bottom
x = runtimeRect.left;
y = runtimeRect.bottom;
break;
case 8: // left-middle
x = runtimeRect.left;
y = yCenter;
break;
}
return CPoint(x, y);
}
// 根据对象操作状态返回对应光标
HCURSOR CDrawObject::GetHandleCursor(int nHandle)
{
LPCTSTR id;
switch (nHandle)
{
default:
ASSERT(FALSE);
case 1:
case 5:
id = IDC_SIZENWSE;
break;
case 2:
case 6:
id = IDC_SIZENS;
break;
case 3:
case 7:
id = IDC_SIZENESW;
break;
case 4:
case 8:
id = IDC_SIZEWE;
break;
}
return AfxGetApp()->LoadStandardCursor(id);
}
void CDrawObject::MoveHandleTo(int nHandle, CPoint point)
{
CRect position = GetBoundRect();
switch (nHandle)
{
default:
ASSERT(FALSE);
case 1: // 左上点
if (point.x < position.right)
position.left = point.x;
if (point.y < position.bottom)
position.top = point.y;
break;
case 2: // 上中点
if (point.y < position.bottom)
position.top = point.y;
break;
case 3: // 右上点
if (point.x > position.left)
position.right = point.x;
if (point.y < position.bottom)
position.top = point.y;
break;
case 4: // 右中点
if (point.x > position.left)
position.right = point.x;
break;
case 5: // 右下点
if (point.x > position.left)
position.right = point.x;
if (point.y > position.top)
position.bottom = point.y;
break;
case 6: // 下中点
if (point.y > position.top)
position.bottom = point.y;
break;
case 7: // 左下点
if (point.x < position.right)
position.left = point.x;
if (point.y > position.top)
position.bottom = point.y;
break;
case 8: // 左中点
if (point.x < position.right)
position.left = point.x;
break;
}
m_rect = position;
}
//----------------------------------------------------------------
//文本区域
CDrawText::CDrawText()
{
m_strText = _T("");
}
CDrawText::~CDrawText(void)
{
}
void CDrawText::DrawSelf(CDC* pDC)
{
CFont* pOldFont = (CFont*) pDC->SelectObject(&m_font);
pDC->SetTextColor(m_colorText);
pDC->SetBkMode(TRANSPARENT);
pDC->DrawText(m_strText,m_rect,DT_LEFT|DT_WORDBREAK);
pDC->SelectObject(pOldFont);
}
void CDrawText::SetTextStyle(const LOGFONT& lf, COLORREF colorText)
{
m_font.DeleteObject();
m_font.CreateFontIndirectW(&lf);
m_colorText = colorText;
}
void CDrawText::SetText(const CString& str)
{
m_strText = str;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -