📄 mapview.cpp
字号:
// MapView.cpp : implementation of the CMapView class
//
#include "stdafx.h"
#include "Map.h"
#include "MapDoc.h"
#include "CntrItem.h"
#include "MapView.h"
#include "SetDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMapView
IMPLEMENT_DYNCREATE(CMapView, CScrollView)
BEGIN_MESSAGE_MAP(CMapView, CScrollView)
//{{AFX_MSG_MAP(CMapView)
ON_WM_DESTROY()
ON_WM_SETFOCUS()
ON_WM_SIZE()
ON_COMMAND(ID_OLE_INSERT_NEW, OnInsertObject)
ON_COMMAND(ID_CANCEL_EDIT_CNTR, OnCancelEditCntr)
ON_WM_LBUTTONDOWN()
ON_WM_SETCURSOR()
ON_WM_LBUTTONDBLCLK()
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMapView construction/destruction
CMapView::CMapView()
{
// TODO: add construction code here
}
CMapView::~CMapView()
{
}
BOOL CMapView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CScrollView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMapView drawing
void CMapView::OnDraw(CDC* pDC)
{
CMapDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
// TODO: also draw all OLE items in the document
// Draw the selection at an arbitrary position. This code should be
// removed once your real drawing code is implemented. This position
// corresponds exactly to the rectangle returned by CMapCntrItem,
// to give the effect of in-place editing.
// TODO: remove this code when final draw code is complete.
CPoint point;
CMyMap& map=pDoc->m_map;
CRectTracker rectTracker;
CMapCntrItem*m_pItem,*m_pOtherItem;
pDC->SelectStockObject(NULL_BRUSH);
CMapVertex*vertex=map.m_TopVertex;
if(map.m_nVerbflag>0&&map.m_nVerbflag<4)
{
float x=GetMinDistance(pDC,map.m_nVerbflag);
}
while(vertex!=NULL)
{
m_pItem=GetItem(vertex->m_VertexKey);
m_pItem->AssertValid();
m_pItem->Draw(pDC, m_pItem->m_Rect);
pDC->Ellipse(m_pItem->m_Rect);
if(!vertex->m_msg.IsEmpty())
{
point.x=m_pItem->m_Rect.right;
point.y=m_pItem->m_Rect.bottom;
pDC->TextOut(point.x,point.y,vertex->m_msg);
}
if(m_pItem==m_pSelection)
{
SetUpTracker(m_pItem,rectTracker);
rectTracker.Draw(pDC);
}
vertex=vertex->next;
}
vertex=pDoc->m_map.m_TopVertex;
while(vertex!=NULL)
{
m_pItem=GetItem(vertex->m_VertexKey);
POSITION pos = vertex->m_next.GetStartPosition();
while(pos!=NULL)
{
UINT key;
float fPower;
CString str;
vertex->m_next.GetNextAssoc(pos,key,fPower);
m_pOtherItem=GetItem(key);
m_pItem->LineToItem(pDC,m_pOtherItem);
str.Format("%3.2f",fPower);
point.x=(m_pItem->m_CenterPoint.x+m_pOtherItem->m_CenterPoint.x)/2;
point.y=(m_pItem->m_CenterPoint.y+m_pOtherItem->m_CenterPoint.y)/2;
pDC->TextOut(point.x,point.y,str);
}
vertex=vertex->next;
}
if(map.m_nVerbflag>3&&map.m_nVerbflag<6)
{
if(InsertEdge(pDC,map.m_nVerbflag))
{
m_pSelection=NULL;
Invalidate();
};
}
pDC->TextOut(0,0,pDoc->m_msg);
}
void CMapView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
// TODO: remove this code when final selection model code is written
m_pSelection = NULL; // initialize selection'
CSize sizeTotal;
// TODO: calculate the total size of this view
sizeTotal.cx= 2100;
sizeTotal.cy= 2970;
SetScrollSizes(MM_TEXT, sizeTotal);
// SetScaleToFitSize( sizeTotal );
}
/////////////////////////////////////////////////////////////////////////////
// CMapView printing
BOOL CMapView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMapView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMapView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void CMapView::OnDestroy()
{
// Deactivate the item on destruction; this is important
// when a splitter view is being used.
CScrollView::OnDestroy();
COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
if (pActiveItem != NULL && pActiveItem->GetActiveView() == this)
{
pActiveItem->Deactivate();
ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
}
}
/////////////////////////////////////////////////////////////////////////////
// OLE Client support and commands
BOOL CMapView::IsSelected(const CObject* pDocItem) const
{
// The implementation below is adequate if your selection consists of
// only CMapCntrItem objects. To handle different selection
// mechanisms, the implementation here should be replaced.
// TODO: implement this function that tests for a selected OLE client item
return pDocItem == m_pSelection;
}
void CMapView::OnInsertObject()
{
// Invoke the standard Insert Object dialog box to obtain information
// for new CMapCntrItem object.
COleInsertDialog dlg;
if (dlg.DoModal() != IDOK)
return;
BeginWaitCursor();
CMapCntrItem* pItem = NULL;
TRY
{
// Create new item connected to this document.
CMapDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pItem = new CMapCntrItem(pDoc);
ASSERT_VALID(pItem);
// Initialize the item from the dialog data.
if (!dlg.CreateItem(pItem))
AfxThrowMemoryException(); // any exception will do
ASSERT_VALID(pItem);
if (dlg.GetSelectionType() == COleInsertDialog::createNewItem)
pItem->DoVerb(OLEIVERB_SHOW, this);
ASSERT_VALID(pItem);
// As an arbitrary user interface design, this sets the selection
// to the last item inserted.
// TODO: reimplement selection as appropriate for your application
m_pSelection = pItem; // set selection to last inserted item
pDoc->UpdateAllViews(NULL);
/////////////////////////////////////////////////////////////////////////////
CMapVertex *pNewVertex=new CMapVertex;
pItem->m_Key=pDoc->m_map.m_NewKey;
pNewVertex->m_VertexKey=pDoc->m_map.m_NewKey++;
pDoc->m_map.AddVertex(pNewVertex);
/////////////////////////////////////////////////////////////////////////////
}
CATCH(CException, e)
{
if (pItem != NULL)
{
ASSERT_VALID(pItem);
pItem->Delete();
}
AfxMessageBox(IDP_FAILED_TO_CREATE);
}
END_CATCH
EndWaitCursor();
}
// The following command handler provides the standard keyboard
// user interface to cancel an in-place editing session. Here,
// the container (not the server) causes the deactivation.
void CMapView::OnCancelEditCntr()
{
// Close any in-place active item on this view.
COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
if (pActiveItem != NULL)
{
pActiveItem->Close();
}
ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
}
// Special handling of OnSetFocus and OnSize are required for a container
// when an object is being edited in-place.
void CMapView::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(); // don't call the base class
return;
}
}
CScrollView::OnSetFocus(pOldWnd);
}
void CMapView::OnSize(UINT nType, int cx, int cy)
{
CScrollView::OnSize(nType, cx, cy);
COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
if (pActiveItem != NULL)
pActiveItem->SetItemRects();
}
/////////////////////////////////////////////////////////////////////////////
// CMapView diagnostics
#ifdef _DEBUG
void CMapView::AssertValid() const
{
CScrollView::AssertValid();
}
void CMapView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CMapDoc* CMapView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMapDoc)));
return (CMapDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMapView message handlers
CMapCntrItem* CMapView::HitTest(CPoint point)
{
CMapCntrItem*ActiveItem=NULL;
CMapDoc* pDoc=GetDocument();
POSITION pos=pDoc->GetStartPosition();
while(pos!=NULL)
{
ActiveItem = (CMapCntrItem*)pDoc->GetNextClientItem(pos);
if(ActiveItem->m_Rect.PtInRect(point))return ActiveItem;
}
return NULL;
}
void CMapView::SetUpTracker(CMapCntrItem *pItem, CRectTracker &rectTracker)
{
rectTracker.m_rect=pItem->m_Rect;
if(pItem==m_pSelection)
{
rectTracker.m_nStyle|=CRectTracker::resizeInside;
}
if(pItem->GetType()==OT_LINK)
{
rectTracker.m_nStyle|=CRectTracker::dottedLine;
}
else
{
rectTracker.m_nStyle|=CRectTracker::solidLine;
}
if(pItem->GetItemState()==COleClientItem::openState||
pItem->GetItemState()==COleClientItem::activeUIState)
{
rectTracker.m_nStyle|=CRectTracker::hatchInside;
}
}
void CMapView::SetSelection(CMapCntrItem *pItem)
{
if(pItem==NULL||pItem!=m_pSelection)
{
COleClientItem*pCurActive=GetDocument()->GetInPlaceActiveItem(this);
if(pCurActive!=pItem&&pCurActive!=NULL)
{
pCurActive->Close();
}
}
m_pSelection=pItem;
Invalidate();
}
void CMapView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CMapCntrItem *pItem=HitTest(point);
SetSelection(pItem);
if(pItem==NULL)
{
UpdateWindow();
return;
}
CRectTracker rectTracker;
SetUpTracker(pItem,rectTracker);
if(GetDocument()->m_map.m_nVerbflag!=5)
{
if(rectTracker.Track(this,point))
{
Invalidate();
pItem->ChangePosition(rectTracker.m_rect);
GetDocument()->SetModifiedFlag();
}
}
CScrollView::OnLButtonDown(nFlags, point);
}
BOOL CMapView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// TODO: Add your message handler code here and/or call default
if(pWnd==this&&m_pSelection!=NULL)
{
CRectTracker rectTracker;
SetUpTracker(m_pSelection,rectTracker);
if(rectTracker.SetCursor(this,nHitTest))return true;
}
return CScrollView::OnSetCursor(pWnd, nHitTest, message);
}
void CMapView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
OnLButtonDown(nFlags, point);
if(m_pSelection!=NULL)
{
if(::GetKeyState(VK_CONTROL)>=0)
{
m_pSelection->DoVerb(OLEIVERB_PRIMARY,this);
}
else
{
m_pSelection->DoVerb(OLEIVERB_OPEN,this);
}
}
CScrollView::OnLButtonDblClk(nFlags, point);
}
BOOL CMapView::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
return CScrollView::DestroyWindow();
}
void CMapView::OnAppAbout()
{
// TODO: Add your command handler code here
CMapDoc* pDoc=this->GetDocument();
}
CMapCntrItem* CMapView::GetItem(UINT key)
{
CMapCntrItem*ActiveItem=NULL;
CMapDoc* pDoc=GetDocument();
POSITION pos=pDoc->GetStartPosition();
while(pos!=NULL)
{
ActiveItem = (CMapCntrItem*)pDoc->GetNextClientItem(pos);
if(ActiveItem->m_Key==key)return ActiveItem;
}
return NULL;
}
bool CMapView::InsertEdge(CDC*pdc,int&flag)
{
// TODO: Add your command handler code here
CSetDlg dlg;
CMapDoc* pDoc=GetDocument();
dlg.m_fValue=1.0;
if(flag==4)
{
if(m_pSelection==NULL)
{
pDoc->m_msg="请选择起点节点";
return false;
}
fir=m_pSelection;
flag=5;
}
pDoc->m_msg="请选择终点节点";
if(fir==m_pSelection||m_pSelection==NULL)return false;
if(dlg.DoModal()==IDOK)
{
CMyMap&map=pDoc->m_map;
CMapVertex* pVertex=map.GetVertex(fir->m_Key);
pVertex->m_next[m_pSelection->m_Key]=dlg.m_fValue;
pVertex=map.GetVertex(m_pSelection->m_Key);
pVertex->m_pre[fir->m_Key]=dlg.m_fValue;
pDoc->SetModifiedFlag();
}
pDoc->m_msg.Empty();
flag=0;
return true;
}
float CMapView::GetMinDistance(CDC*pdc,int& flag)
{
// TODO: Add your command handler code here
static float x;
if(flag==1)
{
if(m_pSelection==NULL)
{
GetDocument()->m_msg="选择第一个节点";
return 0;
}
fir=m_pSelection;
flag=2;
}
if(flag==2)
{
GetDocument()->m_msg="请选择终点节点";
if(fir==m_pSelection||m_pSelection==NULL)return 0;
CMyMap&map=GetDocument()->m_map;
if(!map.TopSort())
{
GetDocument()->m_msg="图存在闭环无法执行该操作";
return flag=0;
}
CString str;
GetDocument()->m_msg="最短路径为"+map.MinDistance(fir->m_Key,m_pSelection->m_Key,x);
str.Format("%3.2f",x);
GetDocument()->m_msg+=" 长度为"+str;
flag=0;
}
return x;
}
void CMapView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
{
// TODO: Add your specialized code here and/or call the base class
/* pDC->SetMapMode(MM_ISOTROPIC);
pDC->SetWindowExt(60,60);
pDC->SetWindowOrg(0,0);
CRect rc;
GetClientRect(rc);
pDC->SetViewportOrg(0,0);
pDC->SetViewportExt(rc.Width(),rc.Height());
*/
CScrollView::OnPrepareDC(pDC, pInfo);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -