📄 drawvw.cpp
字号:
// add the object to the document
GetDocument()->Add(pObj);
m_selection.AddTail(pObj);
ClientToDoc( point );
pObj->MoveTo( CRect( point, pObj->m_extent ), this );
// try to get initial presentation data
pItem->UpdateLink();
pItem->UpdateExtent();
}
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();
}
void CDrawView::DrawGrid(CDC* pDC)
{
CDrawDoc* pDoc = GetDocument();
COLORREF oldBkColor = pDC->SetBkColor(pDoc->GetPaperColor());
CRect rect;
rect.left = -pDoc->GetSize().cx / 2;
rect.top = -pDoc->GetSize().cy / 2;
rect.right = rect.left + pDoc->GetSize().cx;
rect.bottom = rect.top + pDoc->GetSize().cy;
// Center lines
CPen penDash;
penDash.CreatePen(PS_DASH, 1, m_gridColor);
CPen* pOldPen = pDC->SelectObject(&penDash);
//pDC->MoveTo(0, rect.top);
//pDC->LineTo(0, rect.bottom);
//pDC->MoveTo(rect.left, 0);
//pDC->LineTo(rect.right, 0);
// Major unit lines
CPen penDot;
penDot.CreatePen(PS_DOT, 1, m_gridColor);
pDC->SelectObject(&penDot);
for (int x = rect.left /*/ 100 * 100*/; x < m_CanvasWidth/*rect.right*/; x += 100)
{
//if (x != 0)
//{
pDC->MoveTo(x, -m_CanvasWidth/*rect.top*/);
pDC->LineTo(x, rect.bottom);
//}
}
for (int y = -m_CanvasHeight/*rect.top / 100 * 100*/; y < rect.bottom; y += 100)
{
//if (y != 0)
//{
pDC->MoveTo(/*rect.left*/-m_CanvasHeight, y);
pDC->LineTo(/*rect.right*/m_CanvasHeight, 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), 3000);
//size.cy = MulDiv(size.cy, dc.GetDeviceCaps(LOGPIXELSY), 3000);
CSize sizeTotal;
sizeTotal.cx = m_CanvasWidth;
sizeTotal.cy = m_CanvasHeight;
SetScrollSizes(MM_TEXT, sizeTotal);
}
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();
////////////////////////增加打印处理/////////////////
/*
CDrawDoc* pDoc = GetDocument();
s_PageHeight = GetDocument()->GetSize().cx;//pDC->GetDeviceCaps(VERTRES);
s_PageWidth = GetDocument()->GetSize().cy;//pDC->GetDeviceCaps(HORZRES);
s_TotalCols=(m_CoorRight)/s_PageWidth+((m_CoorRight)%s_PageWidth>0);
s_TotalRows=(m_CoorBottom)/s_PageHeight+((m_CoorBottom)%s_PageHeight>0);
pInfo->SetMinPage(1);
pInfo->SetMaxPage(s_TotalCols*s_TotalRows);*/
/*
// isBusy = 1;
CSelPPage slp;
slp.m_draw.m_mWidth = pDC->GetDeviceCaps(HORZSIZE);
slp.m_draw.m_mHeight= pDC->GetDeviceCaps(VERTSIZE);
//设备上每英寸点数
int PixelsX=pDC->GetDeviceCaps(LOGPIXELSX);
int PixelsY=pDC->GetDeviceCaps(LOGPIXELSY);
//设备上每MM点数
slp.mmp = (double)PixelsX/25.4;
slp.m_vlscal= g_fZoom;//pvComm->vlScal;
slp.fx = m_MapRight - m_MapLeft;//(int)(pvComm->vgMaxX- pvComm->vgMinX);
slp.fy = m_MapBottom - m_MapTop;//(int)(pvComm->vgMaxY- pvComm->vgMinY);
// slp.pviewx1 = pviewx1;
// slp.pviewy1 = pviewy1;
// slp.pviewx2 = pviewx2;
// slp.pviewy2 = pviewy2;
slp.m_draw.pWidth = pDC->GetDeviceCaps(HORZRES);
slp.m_draw.pHeight = pDC->GetDeviceCaps(VERTRES);
slp.DoModal();
// iph = slp.m_draw.ih;
// ipw = slp.m_draw.iw;
// ipw = ipw * slp.m_draw.pWidth;
// iph = iph * slp.m_draw.pHeight;
// pArea = 0;
// if (slp.pArea)
// {
// pArea = 1;
// ipw = slp.m_draw.pminx;
// iph = slp.m_draw.pminy;
// }
// kvlScal = pvComm->vlScal;
// pvlScal = slp.m_vlscal;
*/
}
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)
{
CString str;
str.Format("X=%d;Y=%d",point.x,point.y);
((CMainFrame *)AfxGetMainWnd())->m_wndStatusBar.SetPaneText(1,str);
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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -