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

📄 scanlanview.cpp

📁 扫描局域网
💻 CPP
字号:
// ScanLANView.cpp : implementation of the CScanLANView class
//

#include "stdafx.h"
#include "ScanLAN.h"

#include "ScanLANDoc.h"
#include "ScanLANView.h"
#include "MainFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CScanLANView
extern CStringArray m_strArrayIP;

IMPLEMENT_DYNCREATE(CScanLANView, CView)

BEGIN_MESSAGE_MAP(CScanLANView, CView)
	//{{AFX_MSG_MAP(CScanLANView)
	ON_WM_LBUTTONDOWN()
	ON_COMMAND(ID_MENU_LESSON, OnMenuLesson)
	ON_WM_LBUTTONDBLCLK()
	ON_COMMAND(IDM_DISPLAY_DEFENCE, OnDisplayDefence)
	ON_COMMAND(IDM_DISPLAY_DETECT, OnDisplayDetect)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
	ON_MESSAGE(WM_MY_NETMESSAGE,MyNETMessage)
	ON_MESSAGE(WM_MY_NETMESSAGE_D,MyNETMessage_D)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CScanLANView construction/destruction
LRESULT CScanLANView::MyNETMessage(WPARAM wParam, LPARAM lParam)
{
	CString str;

	switch(wParam){
	case MYNET_CLIENTREAD:
//		str = m_clientSocket.GetMessStr();
		m_message = str;
			break;
	case MYNET_CLIENTSEND:
			break;
	case MYNET_CONNECT:
		m_message = "已连接上服务器 !";
			break;
	case MYNET_SERVERCLOSE:
		m_message = "服务器已关闭";
			break;

	case MYNET_SERVERREAD:
		{
			str = m_srvrSocket.GetMessStr();
			m_message = str;
			DealData(str);
		}
		break;
	case MYNET_SERVERSEND:
			break;
	case MYNET_ACCEPT:
		m_message = "已接受客户机请求 !";
			break;
	case MYNET_CLIENTCLOSE:
		m_message = "客户机已关闭";
			break;
	}
//	MessageBox(m_message);
	return TRUE;
}

LRESULT CScanLANView::MyNETMessage_D(WPARAM wParam, LPARAM lParam)
{
	CString str;

	switch(wParam){
	case MYNET_CLIENTREAD:
//		str = m_clientSocket.GetMessStr();
		m_message = str;
			break;
	case MYNET_CLIENTSEND:
			break;
	case MYNET_CONNECT:
		m_message = "已连接上服务器 !";
			break;
	case MYNET_SERVERCLOSE:
		m_message = "服务器已关闭";
			break;

	case MYNET_SERVERREAD:
		{
			str = m_srvrSocket_D.GetMessStr();
			m_message = str;
			DealData(str);
		}
		break;
	case MYNET_SERVERSEND:
			break;
	case MYNET_ACCEPT:
		m_message = "已接受客户机请求 !";
			break;
	case MYNET_CLIENTCLOSE:
		m_message = "客户机已关闭";
			break;
	}
	MessageBox(m_message);
	return TRUE;
}

CScanLANView::CScanLANView()
{
	// TODO: add construction code here
	for(int i=0; i<m_strArrayIP.GetSize(); i++)
	{
		if(i%2==0)
		{
			m_element[8+i/2][6].Set(1,2,i+1,m_strArrayIP[i]);
		}
		else
		{
			m_element[8-i/2-1][6].Set(1,2,i+1,m_strArrayIP[i]);
		}
	}
	b_flag=FALSE;
}

CScanLANView::~CScanLANView()
{
//	KillTimer(1);
	m_srvrSocket.CloseServer();
}

BOOL CScanLANView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CScanLANView drawing

void CScanLANView::OnDraw(CDC* pDC)
{
	CScanLANDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	CMainFrame* pFrame = (CMainFrame*)(AfxGetApp()->m_pMainWnd);
	pFrame->m_wndEdit.SetWindowText(m_strCEdit);
	CString str;
	str.Format("%s", inet_ntoa(pFrame->inadGateway));
	m_element[8][2].Set(1,1,0,str);

	CBitmap bitmap, bitmap1,bitmap2, bitmap3;
	bitmap.LoadBitmap(IDB_BITMAP_SWITCH);
	bitmap1.LoadBitmap(IDB_BITMAP_CLIENT);
	bitmap2.LoadBitmap(IDB_BITMAP_HIDE);
	bitmap3.LoadBitmap(IDB_BITMAP_DEFENCE);

	CDC dcCompatible;
	dcCompatible.CreateCompatibleDC(pDC);

	dcCompatible.SelectObject(&bitmap);
	CRect rect;
	GetClientRect(&rect);
	pDC->BitBlt(8*35, 2*35, rect.Width(), rect.Height(), &dcCompatible, 0, 0, SRCCOPY);

	dcCompatible.SelectObject(&bitmap1);

	for(int i=0; i<m_strArrayIP.GetSize(); i++)
	{
		if(i%2==0)
		{
			if(m_element[8+i/2][6].state==1)
				dcCompatible.SelectObject(&bitmap1);
			else if(m_element[8+i/2][6].state==2)
				dcCompatible.SelectObject(&bitmap2);
			else
				dcCompatible.SelectObject(&bitmap3);

			pDC->BitBlt((8+i/2)*35, 6*35, rect.Width(), rect.Height(), &dcCompatible, 0, 0, SRCCOPY);
			pDC->MoveTo((8+i/2)*35+17, 6*35);
			pDC->LineTo(8*35+17,3*35-1);
		}
		else
		{
			if(m_element[8-i/2-1][6].state==1)
				dcCompatible.SelectObject(&bitmap1);
			else if(m_element[8-i/2-1][6].state==2)
				dcCompatible.SelectObject(&bitmap2);
			else
				dcCompatible.SelectObject(&bitmap3);
			pDC->BitBlt((8-i/2-1)*35, 6*35, rect.Width(), rect.Height(), &dcCompatible, 0, 0, SRCCOPY);
			pDC->MoveTo((8-i/2-1)*35+17, 6*35);
			pDC->LineTo(8*35+17,3*35-1);
		}
	}
}

/////////////////////////////////////////////////////////////////////////////
// CScanLANView printing

BOOL CScanLANView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CScanLANView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CScanLANView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CScanLANView diagnostics

#ifdef _DEBUG
void CScanLANView::AssertValid() const
{
	CView::AssertValid();
}

void CScanLANView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CScanLANDoc* CScanLANView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CScanLANDoc)));
	return (CScanLANDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CScanLANView message handlers

void CScanLANView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
//	CMainFrame* pFrame = (CMainFrame*)(AfxGetApp()->m_pMainWnd);
	CView::OnLButtonDown(nFlags, point);
}

//DEL void CScanLANView::OnPaint() 
//DEL {
//DEL 	CPaintDC dc(this); // device context for painting
//DEL 	
//DEL 	// TODO: Add your message handler code here
//DEL 	
//DEL 	// Do not call CView::OnPaint() for painting messages
//DEL }

void CScanLANView::OnMenuLesson() 
{
	// TODO: Add your command handler code here
	if(b_flag == false)
	{
		m_dlgDefence.Create(IDD_DEFENCE,this);
		m_dlgDetect.Create(IDD_DETECT,this);
		if(!m_srvrSocket.CreateAndListen(this, WM_MY_NETMESSAGE,12345))
			MessageBox("失败");
		if(!m_srvrSocket_D.CreateAndListen(this, WM_MY_NETMESSAGE_D,12346))
			MessageBox("失败");
		SetTimer(1,30000,NULL);  
		b_flag = TRUE;
	}
}

CString CScanLANView::UL2IP(unsigned long ulval)
{
	CString str;
	str.Format("%d.%d.%d.%d",(ulval>>24)&0xff,(ulval>>16)&0xff,(ulval>>8)&0xff,ulval&0xff);
	return str;
}

CString CScanLANView::I2CStr(int i)
{
	CString str;
	str.Format("%d",i);
	return str;
}

void CScanLANView::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(m_element[point.x/35][point.y/35].kind!=0)
	{
		m_strCEdit = m_element[point.x/35][point.y/35].IP;
		Invalidate(NULL);
	}
	
	CView::OnLButtonDblClk(nFlags, point);
}

void CScanLANView::OnDisplayDefence() 
{
	// TODO: Add your command handler code here
	m_dlgDefence.ShowWindow(SW_SHOW);
}

void CScanLANView::OnDisplayDetect() 
{
	// TODO: Add your command handler code here
	m_dlgDetect.ShowWindow(SW_SHOW);
}

void CScanLANView::DealData(CString str)
{
//	m_message = str;
	CString strTemp;
	int nMode;
	int indexFrom=0;
	int indexTo=str.Find(':');
	if(indexTo == -1) return;
	nMode=atoi(str.Mid(indexFrom, indexTo));
	if(nMode == 1 || nMode ==2)
	{
		m_MDD = CStr2MDD(str, nMode);
		for(int i=0; i<17; i++)
			for(int j=0; j<8; j++)
			{
				if(m_element[i][j].IP == UL2IP(m_MDD.addSrc))
				{
					m_strCEdit="源地址:"+UL2IP(m_MDD.addSrc)+"\r\n目的地址:"+UL2IP(m_MDD.addDes)
						+"\r\n源端口:"+I2CStr(m_MDD.portSrc)+"\r\n目的端口:"+I2CStr(m_MDD.portDes);
					if(nMode == 1)
					{
						if(m_MDD.bDrop == 1)
							m_element[i][j].state = 2;
						else
							m_element[i][j].state = 1;
					}
					else if(nMode==2)
					{
						if(m_MDD.bDrop == 1)
							m_element[i][j].state = 3;
						else
							m_element[i][j].state = 1;
					}
				}
		}
		Invalidate(NULL);
	}
	else if(nMode == 3)
	{
		m_MDVC = CStr2MDefVC(str, nMode);
		m_dlgDefence.Insert(m_MDVC);
		m_dlgDefence.ShowData();
	}
	else if(nMode==4)
	{
		int from = 0;
		int to=0;
		while((to = str.Find(';',from))!=-1)
		{
			strTemp = str.Mid(from, to-from+1);
			m_MDVC = CStr2MDefVC(strTemp, nMode);
			m_dlgDetect.Insert(m_MDVC);
			m_dlgDetect.ShowData();
			from = to +1;
		}
	}
}

void CScanLANView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
// 	MessageBox("hello");
	if(nIDEvent == 1)
 	{
 		m_srvrSocket.SendStr("pleasehandin");
 		m_srvrSocket_D.SendStr("pleasehandin");
 	}
	
	CView::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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