⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 netexplorerview.cpp

📁 一个windows系统下查看网络拓扑结构的程序
💻 CPP
字号:

// NetExplorerView.cpp : CNetExplorerView 类的实现
//

#include "stdafx.h"
#include "NetExplorer.h"
#include "RouterButton.h"
#include "NetExplorerDoc.h"
#include "NetExplorerView.h"
#include ".\netexplorerview.h"
#include "ExplorerThread.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#define ADDROUTER (WM_USER+615)
// CNetExplorerView

IMPLEMENT_DYNCREATE(CNetExplorerView, CScrollView)

BEGIN_MESSAGE_MAP(CNetExplorerView, CScrollView)
	// 标准打印命令
	ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
	ON_COMMAND(ID_32782, OnBeginExplorer)
	ON_COMMAND(ID_32781, OnSetParament)
	ON_MESSAGE(WM_DRAWTAIL,OnDrawTail)
	ON_MESSAGE(WM_ENDEXPLORER,OnEndExplorer)
	ON_MESSAGE(WM_REEXPLORER,OnReExplorer)
	//ON_COMMAND(ID_32780, OnChangeCommutity)
	ON_WM_CHAR()
	ON_COMMAND(ID_32786, OnPingAllHost)
	ON_WM_ERASEBKGND()
END_MESSAGE_MAP()

// CNetExplorerView 构造/析构

CNetExplorerView::CNetExplorerView()
{
	// TODO: 在此处添加构造代码
	for(int i=0;i<100;i++)
	{
		this->DrawRow[i]=0;
	}
	this->ExplorerStop=false;
	this->ReferencePoint.y=this->ReferencePoint.x=0;
}

CNetExplorerView::~CNetExplorerView()
{
}

BOOL CNetExplorerView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改 CREATESTRUCT cs 来修改窗口类或
	// 样式
	
	BOOL ReturnValue=CScrollView::PreCreateWindow(cs);

	return  ReturnValue;
}

// CNetExplorerView 绘制

void CNetExplorerView::OnDraw(CDC* /*pDC*/)
{
	CNetExplorerDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
	{
		return;
	}
	else
	{
		CClientDC Dc(this);
		POSITION p;
		this->GetDocument()->AllData.Section.Lock();
		p=this->GetDocument()->AllData.AllLine.GetHeadPosition();
		for(int i=0;i<this->GetDocument()->AllData.AllLine.GetCount();i++)
		{
			Line* P=this->GetDocument()->AllData.AllLine.GetNext(p);
			CPoint StartPoint,EndPoint;
			StartPoint.x=((RouterEnty *)(P->PFrom))->ButtonEnty.OldPositionX;
			StartPoint.y=((RouterEnty *)(P->PFrom))->ButtonEnty.OldPositionY;
			EndPoint.x=((RouterEnty *)(P->PTo))->ButtonEnty.OldPositionX;
			EndPoint.y=((RouterEnty *)(P->PTo))->ButtonEnty.OldPositionY;
			Dc.MoveTo(StartPoint);
			Dc.LineTo(EndPoint);
		}
		p=this->GetDocument()->AllData.AllRouterEnty.GetHeadPosition();
		for(int i=0;i<this->GetDocument()->AllData.AllRouterEnty.GetCount();i++)
		{
			RouterEnty *P=this->GetDocument()->AllData.AllRouterEnty.GetNext(p);
			Dc.TextOut(P->ButtonEnty.OldPositionX,P->ButtonEnty.OldPositionY+60,this->NetIpToString(P->ButtonEnty.ValidIp)); 
			P->ButtonEnty.Invalidate();
		}
		this->GetDocument()->AllData.Section.Unlock();
	}
}

void CNetExplorerView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();
	CSize sizeTotal;
	// TODO: 计算此视图的合计大小
	sizeTotal.cx = sizeTotal.cy = 2000;
	SetScrollSizes(MM_TEXT, sizeTotal);
	if(this->PropertyDialog.DoModal()!=IDOK)
	{
		this->MessageBox("在程序运行前必须输入参数","Error",MB_ICONWARNING|MB_OK);
		exit(0);
	}
	this->GetDocument()->GroupID=this->PropertyDialog.GroupID;
	this->GetDocument()->StratRouterIp=htonl(this->PropertyDialog.DestIP) ;
	this->GetDocument()->SearchDepth=this->PropertyDialog.SerachDepth;
	this->GetDocument()->PNetExplorerView=this;
}



// CNetExplorerView 诊断

#ifdef _DEBUG
void CNetExplorerView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CNetExplorerView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

CNetExplorerDoc* CNetExplorerView::GetDocument() const // 非调试版本是内联的
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CNetExplorerDoc)));
	return (CNetExplorerDoc*)m_pDocument;
}
#endif //_DEBUG


// CNetExplorerView 消息处理程序

void CNetExplorerView::OnBeginExplorer()
{

	static bool IfStart=false;
	if(IfStart==false)
	{
		CString StateInfo;
		StateInfo.Format("开始以路由器“%s”为根节点探测路由器拓扑结构",NetIpToString(this->GetDocument()->StratRouterIp));
		CNetExplorerApp *PNetExplorerApp=(CNetExplorerApp *)AfxGetApp();
		CStatusBar* pStatus=(CStatusBar *)(PNetExplorerApp->m_pMainWnd->GetDescendantWindow(ID_VIEW_STATUS_BAR));
		pStatus->SetPaneText(0,StateInfo.GetBuffer());
		ExplorerParament *TheParament=new ExplorerParament;
		sprintf(TheParament->CommunityName,"%s",this->GetDocument()->GroupID.GetString());//,this->GetDocument()->GroupID.GetLength());
		TheParament->CurrentDepth=0;
		TheParament->Father=NULL;
		TheParament->MessageReceiver=this;	
		TheParament->OriginalIp=this->GetDocument()->StratRouterIp;
		TheParament->PData=&(this->GetDocument()->AllData);
		TheParament->RecursionDepth=this->GetDocument()->SearchDepth;
		AfxBeginThread(ExplorerThreadFunc,TheParament);
		IfStart=true;
		this->ExplorerStop=false;
	}
	else
	{
		if(this->ExplorerStop==false)
		{
			this->MessageBox("上次探测未结束!","Error");
			return;
		}
		this->GetDocument()->PLeftView->GetTreeCtrl().DeleteAllItems();
		this->GetDocument()->PLeftView->Root=this->GetDocument()->PLeftView->GetTreeCtrl().InsertItem("Internet",0,0);
		this->GetDocument()->AllData.Destroy();
		this->Invalidate();
		this->OnSetParament();
		for(int i=0;i<100;i++)
		{
			this->DrawRow[i]=0;
		}
		CString StateInfo;
		StateInfo.Format("开始以路由器“%s”为根节点探测路由器拓扑结构",NetIpToString(this->GetDocument()->StratRouterIp));
		CNetExplorerApp *PNetExplorerApp=(CNetExplorerApp *)AfxGetApp();
		CStatusBar* pStatus=(CStatusBar *)(PNetExplorerApp->m_pMainWnd->GetDescendantWindow(ID_VIEW_STATUS_BAR));
		pStatus->SetPaneText(0,StateInfo.GetBuffer());
		ExplorerParament *TheParament=new ExplorerParament;
		sprintf(TheParament->CommunityName,"%s",this->GetDocument()->GroupID.GetString());//,this->GetDocument()->GroupID.GetLength());
		TheParament->CurrentDepth=0;
		TheParament->Father=NULL;
		TheParament->MessageReceiver=this;	
		TheParament->OriginalIp=this->GetDocument()->StratRouterIp;
		TheParament->PData=&(this->GetDocument()->AllData);
		TheParament->RecursionDepth=this->GetDocument()->SearchDepth;
		AfxBeginThread(ExplorerThreadFunc,TheParament);
		this->ExplorerStop=false;
		IfStart=true;
	}
}
LRESULT CNetExplorerView::OnReExplorer(WPARAM wp, LPARAM lp)
{
	CPoint Point ;
	Point.x=((CPoint *)wp)->x;
	Point.y=((CPoint *)wp)->y;
	delete ((CPoint *)wp);
	CString NewCommunityName;
	NewCommunityName=*((CString *)lp);
	delete ((CString *)lp);
	POSITION Prel=this->GetDocument()->AllData.AllRouterEnty.GetHeadPosition();
	RouterEnty *ReExplorerEnty;
	for(int i=0;i<this->GetDocument()->AllData.AllRouterEnty.GetCount();i++)
	{
		ReExplorerEnty=this->GetDocument()->AllData.AllRouterEnty.GetNext(Prel);
		if(ReExplorerEnty->ButtonEnty.OldPositionX==Point.x&&ReExplorerEnty->ButtonEnty.OldPositionY==Point.y)
		{
			break;
		}
	}
	if(i==this->GetDocument()->AllData.AllRouterEnty.GetCount())
	{
		this->MessageBox("Redirect ERROR");
		return 0;
	}
	else
	{
		//this->MessageBox(,);
		CString StateInfo;
		StateInfo.Format("正在以团体名“%s”重新探测路由器“%s”",NewCommunityName,NetIpToString(ReExplorerEnty->ButtonEnty.IPList.GetHead()));
		CNetExplorerApp *PNetExplorerApp=(CNetExplorerApp *)AfxGetApp();
		CStatusBar* pStatus=(CStatusBar *)(PNetExplorerApp->m_pMainWnd->GetDescendantWindow(ID_VIEW_STATUS_BAR));
		pStatus->SetPaneText(0,StateInfo.GetBuffer());
		ExplorerParament *TheParament=new ExplorerParament;
		sprintf(TheParament->CommunityName,"%s",this->GetDocument()->GroupID.GetString());//,this->GetDocument()->GroupID.GetLength());
		TheParament->CurrentDepth=ReExplorerEnty->CurrentPosition;
		TheParament->Father=ReExplorerEnty;
		TheParament->MessageReceiver=this;	
		TheParament->OriginalIp=ReExplorerEnty->RouterIP.GetHead();
		TheParament->PData=&(this->GetDocument()->AllData);
		TheParament->RecursionDepth=this->GetDocument()->SearchDepth+ReExplorerEnty->CurrentPosition;
		AfxBeginThread(ReExplorerThreadFunc,TheParament);
	}
	return 0;
}
LRESULT CNetExplorerView::OnEndExplorer(WPARAM wp, LPARAM lp)
{
	this->ExplorerStop=true;
	if(wp!=-1)
	{
		if(wp==2)
		{
			CRouterButton *p=(CRouterButton *)lp;
			CString Tip;
			Tip.Append("This is a Router.\nThe host IP List: \n");
			POSITION pos=p->IPList.GetHeadPosition();
			for(int i=0;i<p->IPList.GetCount();i++)
			{
				Tip.Append(NetIpToString(p->IPList.GetNext(pos)));
				Tip.Append("\n");
			}
			p->ChangeTipText(Tip.GetBuffer());
			return 0;
		}
		else
		{
			CString StateInfo;
			StateInfo.Format("路由器拓扑结构探测结束");
			CNetExplorerApp *PNetExplorerApp=(CNetExplorerApp *)AfxGetApp();
			CStatusBar* pStatus=(CStatusBar *)(PNetExplorerApp->m_pMainWnd->GetDescendantWindow(ID_VIEW_STATUS_BAR));
			pStatus->SetPaneText(0,StateInfo.GetBuffer());
			return this->MessageBox("路由器拓扑结构探测结束","Message",MB_OK|MB_ICONINFORMATION);
		}
	}
	else
	{
		CString StateInfo;
		StateInfo.Format("使用新的团体名也无法与该路由器通讯");
		CNetExplorerApp *PNetExplorerApp=(CNetExplorerApp *)AfxGetApp();
		CStatusBar* pStatus=(CStatusBar *)(PNetExplorerApp->m_pMainWnd->GetDescendantWindow(ID_VIEW_STATUS_BAR));
		pStatus->SetPaneText(0,StateInfo.GetBuffer());
		return this->MessageBox("使用新的团体名也无法与该路由器通讯","Warning",MB_OK|MB_ICONWARNING);
	}
}
LRESULT CNetExplorerView::OnDrawTail(WPARAM wp, LPARAM lp)
{

	RouterEnty *p=this->GetDocument()->AllData.AllRouterEnty.GetTail();
	this->DrawRouter(p,(int)wp);
	return 0;
}
void CNetExplorerView::OnSetParament()
{
	// TODO: 在此添加命令处理程序代码
	if(this->PropertyDialog.DoModal()==IDOK)
	{
		this->GetDocument()->GroupID=this->PropertyDialog.GroupID;
		this->GetDocument()->StratRouterIp=htonl(this->PropertyDialog.DestIP) ;
		this->GetDocument()->SearchDepth=this->PropertyDialog.SerachDepth;
	}
}


bool CNetExplorerView::DrawRouter(RouterEnty * Router,int Line)
{
	if(Line>=100)
	{
		this->MessageBox("您递归的也太深了吧?!");
        return false;
	}
	else
	{
		if(Router==NULL)
		{
			this->MessageBox("error");
			return false;
		}
		CPoint point;
		point.y=150*Line+30;
		point.x=150*this->DrawRow[Line]+50;

		CString Tip;
		Tip.Append("This is a Router.\nThe host IP List: \n");
		POSITION p=Router->RouterIP.GetHeadPosition();
		for(int i=0;i<Router->RouterIP.GetCount();i++)
		{
			Tip.Append(NetIpToString(Router->RouterIP.GetNext(p)));
			Tip.Append("\n");
		}
		Router->ButtonEnty.MyCreate(point,this->GetDocument()->RouterBitMap,(char *)Tip.GetBuffer(),this);
		this->DrawRow[Line]++;
		Router->CurrentPosition=Line;
		this->Invalidate();	
		this->GetDocument()->PLeftView->SendMessage(ADDROUTER,(LPARAM)Router);
		return true;
	}

}

BOOL CNetExplorerView::OnScroll(UINT nScrollCode, UINT nPos, BOOL bDoScroll)
{
	// TODO: 在此添加专用代码和/或调用基类
	
	
	BOOL ReturnValue=CScrollView::OnScroll(nScrollCode, nPos, bDoScroll);
	CPoint ChangedPoint=this->GetDeviceScrollPosition()-this->ReferencePoint;
	this->ReferencePoint=this->GetDeviceScrollPosition();
	this->GetDocument()->AllData.Section.Lock();
	POSITION p=this->GetDocument()->AllData.AllRouterEnty.GetHeadPosition();
	for(int i=0;i<this->GetDocument()->AllData.AllRouterEnty.GetCount();i++)
	{
		RouterEnty * P=this->GetDocument()->AllData.AllRouterEnty.GetNext(p);
		P->ButtonEnty.OldPositionX-=ChangedPoint.x;
		P->ButtonEnty.OldPositionY-=ChangedPoint.y ;
	}
	this->GetDocument()->AllData.Section.Unlock();
	this->Invalidate();
	return ReturnValue;
}


void CNetExplorerView::OnChildrenChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	this->OnChar(nChar,nRepCnt,nFlags);
}
void CNetExplorerView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	if(nChar==32)
	{
		this->OnBeginExplorer();
	}
	if(nChar==80||nChar==112)
	{
		this->OnPingAllHost();
	}
	if(nChar==83||nChar==115)
	{
		this->OnSetParament();
	}
	CScrollView::OnChar(nChar, nRepCnt, nFlags);
}

void CNetExplorerView::OnPingAllHost()
{
	// TODO: 在此添加命令处理程序代码
	CResultOfPing TheApp;
	TheApp.DoModal();
}

BOOL CNetExplorerView::OnEraseBkgnd(CDC* pDC)
{
	CBrush backBrush(RGB(255,240,255 ));

	//保存旧的brush
	CBrush* pOldBrush = pDC->SelectObject(&backBrush);
	CRect rect;
	pDC->GetClipBox(&rect);

	//画需要的区域
	pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY);
	pDC->SelectObject(pOldBrush);

	return TRUE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -