NetFinderDlg.cpp
资源名称:NetFinder.rar [点击查看]
上传用户:qiye66692
上传日期:2022-04-25
资源大小:72k
文件大小:8k
源码类别:
SNMP编程
开发平台:
Visual C++
- // NetFinderDlg.cpp : 实现文件
- //
- #include "stdafx.h"
- #include "NetFinder.h"
- #include "NetFinderDlg.h"
- #include ".netfinderdlg.h"
- #include ".func.h"
- #include ".msg.h"
- #include ".SubNetDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- // CNetFinderDlg 对话框
- CNetFinderDlg::CNetFinderDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CNetFinderDlg::IDD, pParent)
- , m_depth(1)
- , m_community(_T("public"))
- , m_ip(_T("192.168.1.2"))
- , m_msg(_T("显示信息"))
- , m_routerIndex(0)
- , m_subNetIndex(0)
- {
- m_hIcon = AfxGetApp()->LoadIcon(IDI_ROUTER);
- pHeadRouter = new CRouter();
- pCurrRouter = pHeadRouter;
- pTailRouter = pHeadRouter;
- m_treeImageList.Create( 16, 16, ILC_COLOR4, 1, 0 );
- HICON hRouterIcon = ::AfxGetApp()->LoadIcon(IDI_ROUTER);
- m_treeImageList.SetBkColor( COLORREF RGB(255,255,255) );
- m_treeImageList.Add( hRouterIcon );
- m_listImageList.Create( 32, 32, ILC_COLOR4, 1, 0 );
- HICON hSubnetIcon = ::AfxGetApp()->LoadIcon(IDI_NET);
- m_listImageList.SetBkColor( COLORREF RGB(255,255,255) );
- m_listImageList.Add( hSubnetIcon );
- }
- void CNetFinderDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- DDX_Text(pDX, IDC_DEPTH_EDIT, m_depth);
- DDV_MinMaxUInt(pDX, m_depth, 1, 100);
- DDX_Text(pDX, IDC_COMMUNITY_EDIT, m_community);
- DDX_Control(pDX, IDC_IPADDRESS, m_ipCtrl);
- DDX_Text(pDX, IDC_ROUTER_MSG_STATIC, m_msg);
- DDX_Control(pDX, IDC_TREE, m_routerTree);
- DDX_Control(pDX, IDC_LIST, m_subNetList);
- }
- BEGIN_MESSAGE_MAP(CNetFinderDlg, CDialog)
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- //}}AFX_MSG_MAP
- ON_BN_CLICKED(IDC_START_BUTTON, OnBnClickedStartButton)
- //user defined message map
- ON_MESSAGE(MSG_FOUND_ROUTER, OnFoundRouter)
- ON_MESSAGE(MSG_SEARCH_ROUTER_END, OnSearchRouterEnd)
- ON_MESSAGE(MSG_SHOW_SUBNET, OnShowSubnet)
- ON_NOTIFY(TVN_SELCHANGED, IDC_TREE, OnTvnSelchangedTree)
- ON_BN_CLICKED(IDC_STOP_BUTTON, OnBnClickedStopButton)
- ON_NOTIFY(NM_DBLCLK, IDC_LIST, OnNMDblclkList)
- END_MESSAGE_MAP()
- // CNetFinderDlg 消息处理程序
- BOOL CNetFinderDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
- // 执行此操作
- SetIcon(m_hIcon, TRUE); // 设置大图标
- SetIcon(m_hIcon, FALSE); // 设置小图标
- // TODO: 在此添加额外的初始化代码
- UpdateData(false);
- m_ipCtrl.SetAddress(192, 168, 1, 2);
- m_routerTree.SetImageList( &m_treeImageList, TVSIL_NORMAL );
- m_subNetList.ModifyStyle( NULL, LVS_ICON );
- m_subNetList.SetImageList( &m_listImageList, LVSIL_NORMAL );
- return TRUE; // 除非设置了控件的焦点,否则返回 TRUE
- }
- // 如果向对话框添加最小化按钮,则需要下面的代码
- // 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
- // 这将由框架自动完成。
- void CNetFinderDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // 用于绘制的设备上下文
- SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
- // 使图标在工作矩形中居中
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // 绘制图标
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
- //当用户拖动最小化窗口时系统调用此函数取得光标显示。
- HCURSOR CNetFinderDlg::OnQueryDragIcon()
- {
- return static_cast<HCURSOR>(m_hIcon);
- }
- void CNetFinderDlg::OnBnClickedStartButton()
- {
- // TODO: 在此添加控件通知处理程序代码
- UpdateData();
- BYTE f1, f2, f3, f4;
- m_ipCtrl.GetAddress( f1, f2, f3, f4 );
- m_ip.Format("%d.%d.%d.%d", f1, f2, f3, f4);
- //再次搜索前先清空array
- if( routerArray.GetSize() ) {
- routerArray.RemoveAll();
- }
- m_routerTree.DeleteAllItems(); //清空树
- m_msg = _T("正在搜索路由...");
- UpdateData(false);
- pThread = AfxBeginThread( SearchRouter, this ); //新开线程进行路由器搜索,执行SerachRouter(它是全局的函数)
- GetDlgItem(IDC_START_BUTTON)->EnableWindow(false);
- GetDlgItem(IDC_START_BUTTON)->SetWindowText(_T("正在搜索"));
- GetDlgItem(IDC_STOP_BUTTON)->EnableWindow(true);
- }
- LRESULT CNetFinderDlg::OnFoundRouter( WPARAM wParam, LPARAM lParam )
- {
- pCurrRouter = pTailRouter;
- routerArray.Add( pCurrRouter );
- pTailRouter->next = new CRouter();
- pTailRouter = pTailRouter->next; //链尾指向新建的CRouter实例
- UINT depth;
- depth = wParam;
- CRouter* router;
- router = (CRouter*)routerArray.GetAt( routerArray.GetSize()-1 );
- if( depth == 1 )
- {
- hItem[1] = m_routerTree.InsertItem( router->m_strName, TVI_ROOT); //往树上加节点
- }
- else
- {
- hItem[depth] = m_routerTree.InsertItem( router->m_strName, hItem[depth-1] );
- }
- m_routerTree.SetItemData( hItem[depth], routerArray.GetSize()-1 );//data用于标志,以显示子网
- m_routerTree.Invalidate();
- m_router_added.SetEvent();
- return 0L;
- }
- LRESULT CNetFinderDlg::OnSearchRouterEnd( WPARAM wParam, LPARAM lParam )
- {
- m_msg = _T("路由搜索完成!");
- UpdateData(false);
- GetDlgItem(IDC_START_BUTTON)->EnableWindow(true);
- GetDlgItem(IDC_START_BUTTON)->SetWindowText(_T("开始搜索"));
- GetDlgItem(IDC_STOP_BUTTON)->EnableWindow(false);
- this->m_routerTree.Invalidate();
- return 0L;
- }
- LRESULT CNetFinderDlg::OnShowSubnet( WPARAM wParam, LPARAM lParam )
- {
- CRouter* pRouter;
- pRouter = (CRouter*)routerArray.GetAt(wParam);
- m_msg = pRouter->m_strDescr;
- if(m_msg.Trim().GetLength() != 0) {
- m_msg += "rnUpTime: " + pRouter->m_strUpTime;
- }
- UpdateData(false);
- m_routerIndex = wParam;
- m_subNetList.DeleteAllItems();
- CString ip, mask;
- for( int i = 0; i < pRouter->m_IpArray.GetSize(); i ++ )
- {
- ip = pRouter->m_IpArray.GetAt(i);
- mask = pRouter->m_IpMaskArray.GetAt(i);
- if( ip.Left(0)!="0" && ip.Left(3)!="127" && ip.Left(3)!="255" )
- {
- DWORD dNetMask = inet_addr(mask);//converts dotted address into a proper address
- DWORD dNetAddr = inet_addr(ip);
- dNetMask = ntohl(dNetMask);//converts a u_long from TCP/IP network order to host byte order
- dNetAddr = ntohl(dNetAddr);
- DWORD dSubNet = dNetAddr & dNetMask;
- BYTE *byte = (BYTE *)&dSubNet;
- CString strIP;
- strIP.Format("%d.%d.%d.%d",
- (int)byte[3], (int)byte[2], (int)byte[1], (int)byte[0]);
- int index = m_subNetList.InsertItem( 0, strIP );
- m_subNetList.SetItemData( index, i );
- }
- }
- return 0L;
- }
- void CNetFinderDlg::OnTvnSelchangedTree(NMHDR *pNMHDR, LRESULT *pResult)
- {
- LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
- // TODO: 在此添加控件通知处理程序代码
- HTREEITEM hItem = pNMTreeView->itemNew.hItem;//新选中的节点
- if ( hItem != NULL) //表示选中
- {
- int data = m_routerTree.GetItemData( hItem );
- SendMessage(MSG_SHOW_SUBNET, data, 0 );
- }
- *pResult = 0;
- }
- void CNetFinderDlg::OnBnClickedStopButton()
- {
- // TODO: 在此添加控件通知处理程序代码
- TerminateThread( pThread->m_hThread, 0 );
- m_msg = _T("搜索线程被终止!");
- MessageBox(m_msg);
- UpdateData(false);
- GetDlgItem(IDC_START_BUTTON)->EnableWindow(true);
- GetDlgItem(IDC_START_BUTTON)->SetWindowText(_T("开始搜索"));
- GetDlgItem(IDC_STOP_BUTTON)->EnableWindow(false);
- }
- void CNetFinderDlg::OnNMDblclkList(NMHDR *pNMHDR, LRESULT *pResult)
- {
- // TODO: 在此添加控件通知处理程序代码
- LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
- int nSelected = pNMLV->iItem;
- if( nSelected >=0 ) {
- CSubNetDlg dlg(this);
- this->m_subNetIndex = this->m_subNetList.GetItemData(nSelected);
- CRouter* pRouter = (CRouter*)routerArray.GetAt(this->m_routerIndex);
- dlg.m_ip = pRouter->m_IpArray.GetAt(this->m_subNetIndex);
- dlg.m_mask = pRouter->m_IpMaskArray.GetAt(this->m_subNetIndex);
- dlg.m_community = this->m_community;
- dlg.DoModal();
- }
- *pResult = 0;
- }