📄 drawobj.cpp
字号:
return CDrawObj::GetHandle(nHandle);
}
HCURSOR CDrawRect::GetHandleCursor(int nHandle)
{
ASSERT_VALID(this);
if (m_nShape == line && nHandle == 2)
nHandle = 5;
else if (m_nShape == roundRectangle && nHandle == 9)
return AfxGetApp()->LoadStandardCursor(IDC_SIZEALL);
return CDrawObj::GetHandleCursor(nHandle);
}
// point is in logical coordinates
void CDrawRect::MoveHandleTo(int nHandle, CPoint point, CDrawView* pView)
{
ASSERT_VALID(this);
if (m_nShape == line && nHandle == 2)
nHandle = 5;
else if (m_nShape == roundRectangle && nHandle == 9)
{
CRect rect = m_position;
rect.NormalizeRect();
if (point.x > rect.right - 1)
point.x = rect.right - 1;
else if (point.x < rect.left + rect.Width() / 2)
point.x = rect.left + rect.Width() / 2;
if (point.y > rect.bottom - 1)
point.y = rect.bottom - 1;
else if (point.y < rect.top + rect.Height() / 2)
point.y = rect.top + rect.Height() / 2;
m_roundness.x = 2 * (rect.right - point.x);
m_roundness.y = 2 * (rect.bottom - point.y);
m_pDocument->SetModifiedFlag();
if (pView == NULL)
Invalidate();
else
pView->InvalObj(this);
return;
}
CDrawObj::MoveHandleTo(nHandle, point, pView);
}
// rect must be in logical coordinates
BOOL CDrawRect::Intersects(const CRect& rect)
{
ASSERT_VALID(this);
CRect rectT = rect;
rectT.NormalizeRect();
CRect fixed = m_position;
fixed.NormalizeRect();
if ((rectT & fixed).IsRectEmpty())
return FALSE;
CRgn rgn;
switch (m_nShape)
{
case rectangle:
return TRUE;
case roundRectangle:
rgn.CreateRoundRectRgn(fixed.left, fixed.top, fixed.right, fixed.bottom,
m_roundness.x, m_roundness.y);
break;
case ellipse:
rgn.CreateEllipticRgnIndirect(fixed);
break;
case line:
{
int x = (m_logpen.lopnWidth.x + 5) / 2;
int y = (m_logpen.lopnWidth.y + 5) / 2;
POINT points[4];
points[0].x = fixed.left;
points[0].y = fixed.top;
points[1].x = fixed.left;
points[1].y = fixed.top;
points[2].x = fixed.right;
points[2].y = fixed.bottom;
points[3].x = fixed.right;
points[3].y = fixed.bottom;
if (fixed.left < fixed.right)
{
points[0].x -= x;
points[1].x += x;
points[2].x += x;
points[3].x -= x;
}
else
{
points[0].x += x;
points[1].x -= x;
points[2].x -= x;
points[3].x += x;
}
if (fixed.top < fixed.bottom)
{
points[0].y -= y;
points[1].y += y;
points[2].y += y;
points[3].y -= y;
}
else
{
points[0].y += y;
points[1].y -= y;
points[2].y -= y;
points[3].y += y;
}
rgn.CreatePolygonRgn(points, 4, ALTERNATE);
}
break;
}
return rgn.RectInRegion(fixed);
}
CDrawObj* CDrawRect::Clone(CDrawDoc* pDoc)
{
ASSERT_VALID(this);
CDrawRect* pClone = new CDrawRect(m_position);
pClone->m_bPen = m_bPen;
pClone->m_logpen = m_logpen;
pClone->m_bBrush = m_bBrush;
pClone->m_logbrush = m_logbrush;
pClone->m_nShape = m_nShape;
pClone->m_roundness = m_roundness;
ASSERT_VALID(pClone);
if (pDoc != NULL)
pDoc->Add(pClone);
ASSERT_VALID(pClone);
return pClone;
}
////////////////////////////////////////////////////////////////////////////
// CDrawPoly
IMPLEMENT_SERIAL(CDrawPoly, CDrawObj, 0)
CDrawPoly::CDrawPoly()
{
m_points = NULL;
m_nPoints = 0;
m_nAllocPoints = 0;
}
CDrawPoly::CDrawPoly(const CRect& position)
: CDrawObj(position)
{
m_points = NULL;
m_nPoints = 0;
m_nAllocPoints = 0;
m_bPen = TRUE;
m_bBrush = FALSE;
}
CDrawPoly::~CDrawPoly()
{
if (m_points != NULL)
delete[] m_points;
}
void CDrawPoly::Serialize( CArchive& ar )
{
int i;
CDrawObj::Serialize( ar );
if( ar.IsStoring() )
{
ar << (WORD) m_nPoints;
ar << (WORD) m_nAllocPoints;
for (i = 0;i< m_nPoints; i++)
ar << m_points[i];
}
else
{
WORD wTemp;
ar >> wTemp; m_nPoints = wTemp;
ar >> wTemp; m_nAllocPoints = wTemp;
m_points = new CPoint[m_nAllocPoints];
for (i = 0;i < m_nPoints; i++)
ar >> m_points[i];
}
}
void CDrawPoly::Draw(CDC* pDC)
{
ASSERT_VALID(this);
CBrush brush;
if (!brush.CreateBrushIndirect(&m_logbrush))
return;
CPen pen;
if (!pen.CreatePenIndirect(&m_logpen))
return;
CBrush* pOldBrush;
CPen* pOldPen;
if (m_bBrush)
pOldBrush = pDC->SelectObject(&brush);
else
pOldBrush = (CBrush*)pDC->SelectStockObject(NULL_BRUSH);
if (m_bPen)
pOldPen = pDC->SelectObject(&pen);
else
pOldPen = (CPen*)pDC->SelectStockObject(NULL_PEN);
pDC->Polygon(m_points, m_nPoints);
pDC->SelectObject(pOldBrush);
pDC->SelectObject(pOldPen);
}
// position must be in logical coordinates
void CDrawPoly::MoveTo(const CRect& position, CDrawView* pView)
{
ASSERT_VALID(this);
if (position == m_position)
return;
if (pView == NULL)
Invalidate();
else
pView->InvalObj(this);
for (int i = 0; i < m_nPoints; i += 1)
{
m_points[i].x += position.left - m_position.left;
m_points[i].y += position.top - m_position.top;
}
m_position = position;
if (pView == NULL)
Invalidate();
else
pView->InvalObj(this);
m_pDocument->SetModifiedFlag();
}
int CDrawPoly::GetHandleCount()
{
return m_nPoints;
}
CPoint CDrawPoly::GetHandle(int nHandle)
{
ASSERT_VALID(this);
ASSERT(nHandle >= 1 && nHandle <= m_nPoints);
return m_points[nHandle - 1];
}
HCURSOR CDrawPoly::GetHandleCursor(int )
{
return AfxGetApp()->LoadStandardCursor(IDC_ARROW);
}
// point is in logical coordinates
void CDrawPoly::MoveHandleTo(int nHandle, CPoint point, CDrawView* pView)
{
ASSERT_VALID(this);
ASSERT(nHandle >= 1 && nHandle <= m_nPoints);
if (m_points[nHandle - 1] == point)
return;
m_points[nHandle - 1] = point;
RecalcBounds(pView);
if (pView == NULL)
Invalidate();
else
pView->InvalObj(this);
m_pDocument->SetModifiedFlag();
}
// rect must be in logical coordinates
BOOL CDrawPoly::Intersects(const CRect& rect)
{
ASSERT_VALID(this);
CRgn rgn;
rgn.CreatePolygonRgn(m_points, m_nPoints, ALTERNATE);
return rgn.RectInRegion(rect);
}
CDrawObj* CDrawPoly::Clone(CDrawDoc* pDoc)
{
ASSERT_VALID(this);
CDrawPoly* pClone = new CDrawPoly(m_position);
pClone->m_bPen = m_bPen;
pClone->m_logpen = m_logpen;
pClone->m_bBrush = m_bBrush;
pClone->m_logbrush = m_logbrush;
pClone->m_points = new CPoint[m_nAllocPoints];
memcpy(pClone->m_points, m_points, sizeof(CPoint) * m_nPoints);
pClone->m_nAllocPoints = m_nAllocPoints;
pClone->m_nPoints = m_nPoints;
ASSERT_VALID(pClone);
if (pDoc != NULL)
pDoc->Add(pClone);
ASSERT_VALID(pClone);
return pClone;
}
// point is in logical coordinates
void CDrawPoly::AddPoint(const CPoint& point, CDrawView* pView)
{
ASSERT_VALID(this);
if (m_nPoints == m_nAllocPoints)
{
CPoint* newPoints = new CPoint[m_nAllocPoints + 10];
if (m_points != NULL)
{
memcpy(newPoints, m_points, sizeof(CPoint) * m_nAllocPoints);
delete[] m_points;
}
m_points = newPoints;
m_nAllocPoints += 10;
}
if (m_nPoints == 0 || m_points[m_nPoints - 1] != point)
{
m_points[m_nPoints++] = point;
if (!RecalcBounds(pView))
{
if (pView == NULL)
Invalidate();
else
pView->InvalObj(this);
}
m_pDocument->SetModifiedFlag();
}
}
BOOL CDrawPoly::RecalcBounds(CDrawView* pView)
{
ASSERT_VALID(this);
if (m_nPoints == 0)
return FALSE;
CRect bounds(m_points[0], CSize(0, 0));
for (int i = 1; i < m_nPoints; ++i)
{
if (m_points[i].x < bounds.left)
bounds.left = m_points[i].x;
if (m_points[i].x > bounds.right)
bounds.right = m_points[i].x;
if (m_points[i].y < bounds.top)
bounds.top = m_points[i].y;
if (m_points[i].y > bounds.bottom)
bounds.bottom = m_points[i].y;
}
if (bounds == m_position)
return FALSE;
if (pView == NULL)
Invalidate();
else
pView->InvalObj(this);
m_position = bounds;
if (pView == NULL)
Invalidate();
else
pView->InvalObj(this);
return TRUE;
}
////////////////////////////////////////////////////////////////////////////
IMPLEMENT_SERIAL(CDrawOleObj, CDrawObj, 0)
BOOL CDrawOleObj::c_bShowItems = TRUE;
CDrawOleObj::CDrawOleObj() : m_extent(0,0)
{
m_pClientItem = NULL;
}
CDrawOleObj::CDrawOleObj(const CRect& position)
: CDrawObj(position), m_extent(0, 0)
{
m_pClientItem = NULL;
}
CDrawOleObj::~CDrawOleObj()
{
if (m_pClientItem != NULL)
{
m_pClientItem->Release();
m_pClientItem = NULL;
}
}
void CDrawOleObj::Remove()
{
if (m_pClientItem != NULL)
{
m_pClientItem->Delete();
m_pClientItem = NULL;
}
CDrawObj::Remove();
}
void CDrawOleObj::Serialize( CArchive& ar )
{
ASSERT_VALID(this);
CDrawObj::Serialize(ar);
if (ar.IsStoring())
{
ar << m_extent;
ar << m_pClientItem;
}
else
{
ar >> m_extent;
ar >> m_pClientItem;
m_pClientItem->m_pDrawObj = this;
}
}
CDrawObj* CDrawOleObj::Clone(CDrawDoc* pDoc)
{
ASSERT_VALID(this);
AfxGetApp()->BeginWaitCursor();
CDrawOleObj* pClone = NULL;
CDrawItem* pItem = NULL;
TRY
{
// perform a "deep copy" -- need to copy CDrawOleObj and the CDrawItem
// that it points to.
CDrawOleObj* pClone = new CDrawOleObj(m_position);
CDrawItem* pItem = new CDrawItem(m_pDocument, pClone);
if (!pItem->CreateCloneFrom(m_pClientItem))
AfxThrowMemoryException();
pClone->m_pClientItem = pItem;
pClone->m_bPen = m_bPen;
pClone->m_logpen = m_logpen;
pClone->m_bBrush = m_bBrush;
pClone->m_logbrush = m_logbrush;
ASSERT_VALID(pClone);
if (pDoc != NULL)
pDoc->Add(pClone);
}
CATCH_ALL(e)
{
pItem->Delete();
pClone->m_pClientItem = NULL;
pClone->Remove();
AfxGetApp()->EndWaitCursor();
THROW_LAST();
}
END_CATCH_ALL
AfxGetApp()->EndWaitCursor();
return pClone;
}
void CDrawOleObj::Draw(CDC* pDC)
{
ASSERT_VALID(this);
CDrawItem* pItem = m_pClientItem;
if (pItem != NULL)
{
// draw the OLE item itself
pItem->Draw(pDC, m_position);
// don't draw tracker in print preview or on printer
if (!pDC->IsPrinting())
{
// use a CRectTracker to draw the standard effects
CRectTracker tracker;
tracker.m_rect = m_position;
pDC->LPtoDP(tracker.m_rect);
if (c_bShowItems)
{
// put correct border depending on item type
if (pItem->GetType() == OT_LINK)
tracker.m_nStyle |= CRectTracker::dottedLine;
else
tracker.m_nStyle |= CRectTracker::solidLine;
}
// put hatching over the item if it is currently open
if (pItem->GetItemState() == COleClientItem::openState ||
pItem->GetItemState() == COleClientItem::activeUIState)
{
tracker.m_nStyle |= CRectTracker::hatchInside;
}
tracker.Draw(pDC);
}
}
}
void CDrawOleObj::OnOpen(CDrawView* pView)
{
AfxGetApp()->BeginWaitCursor();
m_pClientItem->DoVerb(
GetKeyState(VK_CONTROL) < 0 ? OLEIVERB_OPEN : OLEIVERB_PRIMARY,
pView);
AfxGetApp()->EndWaitCursor();
}
void CDrawOleObj::OnEditProperties()
{
// using COlePropertiesDialog directly means no scaling
COlePropertiesDialog dlg(m_pClientItem, 100, 100, NULL);
dlg.DoModal();
}
// position is in logical
void CDrawOleObj::MoveTo(const CRect& position, CDrawView* pView)
{
ASSERT_VALID(this);
if (position == m_position)
return;
// call base class to update position
CDrawObj::MoveTo(position, pView);
// update position of in-place editing session on position change
if (m_pClientItem->IsInPlaceActive())
m_pClientItem->SetItemRects();
}
/////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -