📄 drawvw.cpp
字号:
{
if (y != 0)
{
pDC->MoveTo(rect.left, y);
pDC->LineTo(rect.right, y);
}
}
// Outlines
CPen penSolid;
penSolid.CreatePen(PS_SOLID, 1, m_gridColor);
pDC->SelectObject(&penSolid);
pDC->MoveTo(rect.left, rect.top);
pDC->LineTo(rect.right, rect.top);
pDC->LineTo(rect.right, rect.bottom);
pDC->LineTo(rect.left, rect.bottom);
pDC->LineTo(rect.left, rect.top);
pDC->SelectObject(pOldPen);
pDC->SetBkColor(oldBkColor);
}
void CDrawView::OnInitialUpdate()
{
CSize size = GetDocument()->GetSize();
CClientDC dc(NULL);
size.cx = MulDiv(size.cx, dc.GetDeviceCaps(LOGPIXELSX), 100);
size.cy = MulDiv(size.cy, dc.GetDeviceCaps(LOGPIXELSY), 100);
SetScrollSizes(MM_TEXT, size);
}
void CDrawView::SetPageSize(CSize size)
{
CClientDC dc(NULL);
size.cx = MulDiv(size.cx, dc.GetDeviceCaps(LOGPIXELSX), 100);
size.cy = MulDiv(size.cy, dc.GetDeviceCaps(LOGPIXELSY), 100);
SetScrollSizes(MM_TEXT, size);
GetDocument()->UpdateAllViews(NULL, HINT_UPDATE_WINDOW, NULL);
}
/////////////////////////////////////////////////////////////////////////////
// CDrawView printing
BOOL CDrawView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CDrawView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
CScrollView::OnBeginPrinting(pDC,pInfo);
// check page size -- user could have gone into print setup
// from print dialog and changed paper or orientation
GetDocument()->ComputePageSize();
}
void CDrawView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// OLE Client support and commands
BOOL CDrawView::IsSelected(const CObject* pDocItem) const
{
CDrawObj* pDrawObj = (CDrawObj*)pDocItem;
if (pDocItem->IsKindOf(RUNTIME_CLASS(CDrawItem)))
pDrawObj = ((CDrawItem*)pDocItem)->m_pDrawObj;
return m_selection.Find(pDrawObj) != NULL;
}
void CDrawView::OnInsertObject()
{
// Invoke the standard Insert Object dialog box to obtain information
// for new CDrawItem object.
COleInsertDialog dlg;
if (dlg.DoModal() != IDOK)
return;
BeginWaitCursor();
// First create the C++ object
CDrawOleObj* pObj = new CDrawOleObj(GetInitialPosition());
ASSERT_VALID(pObj);
CDrawItem* pItem = new CDrawItem(GetDocument(), pObj);
ASSERT_VALID(pItem);
pObj->m_pClientItem = pItem;
// Now create the OLE object/item
TRY
{
if (!dlg.CreateItem(pObj->m_pClientItem))
AfxThrowMemoryException();
// add the object to the document
GetDocument()->Add(pObj);
// try to get initial presentation data
pItem->UpdateLink();
pItem->UpdateExtent();
// if insert new object -- initially show the object
if (dlg.GetSelectionType() == COleInsertDialog::createNewItem)
pItem->DoVerb(OLEIVERB_SHOW, this);
}
CATCH_ALL(e)
{
// clean up item
pItem->Delete();
pObj->m_pClientItem = NULL;
GetDocument()->Remove(pObj);
pObj->Remove();
AfxMessageBox(IDP_FAILED_TO_CREATE);
}
END_CATCH_ALL
EndWaitCursor();
}
// The following command handler provides the standard keyboard
// user interface to cancel an in-place editing session.
void CDrawView::OnCancelEdit()
{
// deactivate any in-place active item on this view!
COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
if (pActiveItem != NULL)
{
// if we found one, deactivate it
pActiveItem->Close();
}
ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
// escape also brings us back into select mode
ReleaseCapture();
CDrawTool* pTool = CDrawTool::FindTool(CDrawTool::c_drawShape);
if (pTool != NULL)
pTool->OnCancel();
CDrawTool::c_drawShape = selection;
}
void CDrawView::OnSetFocus(CWnd* pOldWnd)
{
COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
if (pActiveItem != NULL &&
pActiveItem->GetItemState() == COleClientItem::activeUIState)
{
// need to set focus to this item if it is in the same view
CWnd* pWnd = pActiveItem->GetInPlaceWindow();
if (pWnd != NULL)
{
pWnd->SetFocus();
return;
}
}
CScrollView::OnSetFocus(pOldWnd);
}
CRect CDrawView::GetInitialPosition()
{
CRect rect(10, 10, 10, 10);
ClientToDoc(rect);
return rect;
}
void CDrawView::ClientToDoc(CPoint& point)
{
CClientDC dc(this);
OnPrepareDC(&dc, NULL);
dc.DPtoLP(&point);
}
void CDrawView::ClientToDoc(CRect& rect)
{
CClientDC dc(this);
OnPrepareDC(&dc, NULL);
dc.DPtoLP(rect);
ASSERT(rect.left <= rect.right);
ASSERT(rect.bottom <= rect.top);
}
void CDrawView::DocToClient(CPoint& point)
{
CClientDC dc(this);
OnPrepareDC(&dc, NULL);
dc.LPtoDP(&point);
}
void CDrawView::DocToClient(CRect& rect)
{
CClientDC dc(this);
OnPrepareDC(&dc, NULL);
dc.LPtoDP(rect);
rect.NormalizeRect();
}
void CDrawView::Select(CDrawObj* pObj, BOOL bAdd)
{
if (!bAdd)
{
OnUpdate(NULL, HINT_UPDATE_SELECTION, NULL);
m_selection.RemoveAll();
}
if (pObj == NULL || IsSelected(pObj))
return;
m_selection.AddTail(pObj);
InvalObj(pObj);
}
// rect is in device coordinates
void CDrawView::SelectWithinRect(CRect rect, BOOL bAdd)
{
if (!bAdd)
Select(NULL);
ClientToDoc(rect);
CDrawObjList* pObList = GetDocument()->GetObjects();
POSITION posObj = pObList->GetHeadPosition();
while (posObj != NULL)
{
CDrawObj* pObj = pObList->GetNext(posObj);
if (pObj->Intersects(rect))
Select(pObj, TRUE);
}
}
void CDrawView::Deselect(CDrawObj* pObj)
{
POSITION pos = m_selection.Find(pObj);
if (pos != NULL)
{
InvalObj(pObj);
m_selection.RemoveAt(pos);
}
}
void CDrawView::CloneSelection()
{
POSITION pos = m_selection.GetHeadPosition();
while (pos != NULL)
{
CDrawObj* pObj = m_selection.GetNext(pos);
pObj->Clone(pObj->m_pDocument);
// copies object and adds it to the document
}
}
void CDrawView::UpdateActiveItem()
{
COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
if (pActiveItem != NULL &&
pActiveItem->GetItemState() == COleClientItem::activeUIState)
{
// this will update the item rectangles by calling
// OnGetPosRect & OnGetClipRect.
pActiveItem->SetItemRects();
}
}
/////////////////////////////////////////////////////////////////////////////
// CDrawView message handlers
void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
if (!m_bActive)
return;
CDrawTool* pTool = CDrawTool::FindTool(CDrawTool::c_drawShape);
if (pTool != NULL)
pTool->OnLButtonDown(this, nFlags, point);
}
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
if (!m_bActive)
return;
CDrawTool* pTool = CDrawTool::FindTool(CDrawTool::c_drawShape);
if (pTool != NULL)
pTool->OnLButtonUp(this, nFlags, point);
}
void CDrawView::OnMouseMove(UINT nFlags, CPoint point)
{
if (!m_bActive)
return;
CDrawTool* pTool = CDrawTool::FindTool(CDrawTool::c_drawShape);
if (pTool != NULL)
pTool->OnMouseMove(this, nFlags, point);
}
void CDrawView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
if (!m_bActive)
return;
CDrawTool* pTool = CDrawTool::FindTool(CDrawTool::c_drawShape);
if (pTool != NULL)
pTool->OnLButtonDblClk(this, nFlags, point);
}
void CDrawView::OnDestroy()
{
CScrollView::OnDestroy();
// deactivate the inplace active item on this view
COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
if (pActiveItem != NULL && pActiveItem->GetActiveView() == this)
{
pActiveItem->Deactivate();
ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
}
}
void CDrawView::OnDrawSelect()
{
CDrawTool::c_drawShape = selection;
}
void CDrawView::OnDrawRoundRect()
{
CDrawTool::c_drawShape = roundRect;
}
void CDrawView::OnDrawRect()
{
CDrawTool::c_drawShape = rect;
}
void CDrawView::OnDrawLine()
{
CDrawTool::c_drawShape = line;
}
void CDrawView::OnDrawEllipse()
{
CDrawTool::c_drawShape = ellipse;
}
void CDrawView::OnDrawPolygon()
{
CDrawTool::c_drawShape = poly;
}
void CDrawView::OnUpdateDrawEllipse(CCmdUI* pCmdUI)
{
pCmdUI->SetRadio(CDrawTool::c_drawShape == ellipse);
}
void CDrawView::OnUpdateDrawLine(CCmdUI* pCmdUI)
{
pCmdUI->SetRadio(CDrawTool::c_drawShape == line);
}
void CDrawView::OnUpdateDrawRect(CCmdUI* pCmdUI)
{
pCmdUI->SetRadio(CDrawTool::c_drawShape == rect);
}
void CDrawView::OnUpdateDrawRoundRect(CCmdUI* pCmdUI)
{
pCmdUI->SetRadio(CDrawTool::c_drawShape == roundRect);
}
void CDrawView::OnUpdateDrawSelect(CCmdUI* pCmdUI)
{
pCmdUI->SetRadio(CDrawTool::c_drawShape == selection);
}
void CDrawView::OnUpdateSingleSelect(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_selection.GetCount() == 1);
}
void CDrawView::OnEditSelectAll()
{
CDrawObjList* pObList = GetDocument()->GetObjects();
POSITION pos = pObList->GetHeadPosition();
while (pos != NULL)
Select(pObList->GetNext(pos), TRUE);
}
void CDrawView::OnUpdateEditSelectAll(CCmdUI* pCmdUI)
{
pCmdUI->Enable(GetDocument()->GetObjects()->GetCount() != 0);
}
void CDrawView::OnEditClear()
{
// update all the views before the selection goes away
GetDocument()->UpdateAllViews(NULL, HINT_DELETE_SELECTION, &m_selection);
OnUpdate(NULL, HINT_UPDATE_SELECTION, NULL);
// now remove the selection from the document
POSITION pos = m_selection.GetHeadPosition();
while (pos != NULL)
{
CDrawObj* pObj = m_selection.GetNext(pos);
GetDocument()->Remove(pObj);
pObj->Remove();
}
m_selection.RemoveAll();
}
void CDrawView::OnUpdateAnySelect(CCmdUI* pCmdUI)
{
pCmdUI->Enable(!m_selection.IsEmpty());
}
void CDrawView::OnUpdateDrawPolygon(CCmdUI* pCmdUI)
{
pCmdUI->SetRadio(CDrawTool::c_drawShape == poly);
}
void CDrawView::OnSize(UINT nType, int cx, int cy)
{
CScrollView::OnSize(nType, cx, cy);
UpdateActiveItem();
}
void CDrawView::OnViewGrid()
{
m_bGrid = !m_bGrid;
Invalidate(FALSE);
}
void CDrawView::OnUpdateViewGrid(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck(m_bGrid);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -