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

📄 listendlg.cpp

📁 毕业设计——本程序用于网络检测——监听的一种代码。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		strncpy(szSourceIP,m_szSourceIP,16);

	if(strcmp(m_HostIp,m_szDestIP)==0)
	{
		strcat(szDestIP,"(本机)");
		strcat(szDestIP,m_szDestIP);
	}
	else
		strncpy(szDestIP,m_szDestIP,16);

	strncpy(szSourcePort,m_szSourcePort,5);
	strncpy(szDestPort,m_szDestPort,5);
	strncpy(szDestIPt,m_szSourceIP,32);
	strncpy(szSourceIPt,m_szDestIP,32);
	strncpy(szProto,m_szProtocol,16);

	wsprintf(szWprot,"%d",GetDlgItemInt(IDC_EDIT1));
	GetDlgItem(IDC_EDIT2)->GetWindowText(szIP,32);
	 
	BOOL bProt=m_IsAllPort|(strcmp(szSourcePort,szWprot)==0)|
		(strcmp(szDestPort,szWprot)==0); //判断端口规则
	BOOL bIp=m_IsAllIp|(strcmp(szSourceIPt,szIP)==0)|
		(strcmp(szDestIPt,szIP)==0); //判断IP规则
	BOOL bProto=m_IsAllProto|(strcmp(szProto,m_szProto)==0); //判断协议规则
 	if(bProt&&bIp&&bProto)
	{
/*--------------------------------------------------------------
		添加的基于CListCtrl控件的代码
---------------------------------------------------------------*/
			CString sNowTime=GetNowTime();
 			int count = ((CListCtrl *)GetDlgItem(IDC_LIST3)) ->GetItemCount();
			str.Format("(头/总)%d/%d",m_ihLen,len);
			char buffer[32];
			_tcscpy(buffer,str);
	        ((CListCtrl *)GetDlgItem(IDC_LIST3)) ->InsertItem(count,"");
			((CListCtrl *)GetDlgItem(IDC_LIST3)) ->SetItemText(count,0,m_szProtocol);
			((CListCtrl *)GetDlgItem(IDC_LIST3)) ->SetItemText(count,1,szSourceIP);
			((CListCtrl *)GetDlgItem(IDC_LIST3)) ->SetItemText(count,2,m_szSourcePort);
			((CListCtrl *)GetDlgItem(IDC_LIST3)) ->SetItemText(count,3,szDestIP);
			((CListCtrl *)GetDlgItem(IDC_LIST3)) ->SetItemText(count,4,m_szDestPort);
			((CListCtrl *)GetDlgItem(IDC_LIST3)) ->SetItemText(count,5,buffer);
			((CListCtrl *)GetDlgItem(IDC_LIST3)) ->SetItemText(count,6,sNowTime.GetBuffer(sNowTime.GetLength()));
			((CListCtrl *)GetDlgItem(IDC_LIST3)) ->SetTextBkColor(RGB(195,200,195));
			
			
			//绑定数据
			IPDATA* pIpData = new IPDATA;
			pIpData->len = len;
			pIpData->buf = new char[len];
			memcpy(pIpData->buf, pData, len);
			((CListCtrl *)GetDlgItem(IDC_LIST3)) ->SetItemData(count,(DWORD)pIpData);



			int nCheck=((CButton*)GetDlgItem(IDC_CHECK1))->GetCheck();
			if(nCheck)
				CreateLogFile(str.GetBuffer(/*str.GetLength()*/0));

	}
}

//端口过滤
void CListenDlg::OnRadio2() 
{
	m_IsAllPort=TRUE;
	GetDlgItem(IDC_EDIT1)->EnableWindow(FALSE);
}

void CListenDlg::OnRadio3() 
{
	m_IsAllPort=FALSE;
	GetDlgItem(IDC_EDIT1)->EnableWindow(TRUE);
}


//IP过滤
void CListenDlg::OnRadio1() 
{
	// TODO: Add your control notification handler code here
	m_IsAllIp=TRUE;
	GetDlgItem(IDC_EDIT2)->EnableWindow(FALSE);
}

void CListenDlg::OnRadio4() 
{
	// TODO: Add your control notification handler code here
	m_IsAllIp=FALSE;
	GetDlgItem(IDC_EDIT2)->EnableWindow(TRUE);
}



//文本框只允许输入数字
BOOL CListenDlg::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	if(WM_CHAR==pMsg->message)
	{
		int nChar=(int)(pMsg->wParam);
		if((nChar<48||nChar>57)&&(nChar!=8)&&(nChar!=46))
			return TRUE;
	}
	return CDialog::PreTranslateMessage(pMsg);
}

//协议过滤
void CListenDlg::OnRadio6() 
{
	// TODO: Add your control notification handler code here
	m_IsAllProto=FALSE;
	strncpy(m_szProto,"TCP ",5);
}
void CListenDlg::OnRadio5() 
{
	// TODO: Add your control notification handler code here
	m_IsAllProto=TRUE;
}

void CListenDlg::OnRadio7() 
{
	// TODO: Add your control notification handler code here
	m_IsAllProto=FALSE;
	strncpy(m_szProto,"UDP ",5);
}


void CListenDlg::OnRadio8() 
{
	// TODO: Add your control notification handler code here
	m_IsAllProto=FALSE;
	strncpy(m_szProto,"ICMP",5);
}

//复制源IP,右键菜单
void CListenDlg::OnCopySip() 
{
	// TODO: Add your command handler code here
	POSITION pos = m_list.GetFirstSelectedItemPosition(); 
	//Gets the position of the first selected item in the list view control

 	int n=m_list.GetNextSelectedItem(pos);
//Gets the index of the list item identified by pos, then sets pos to the POSITION 
//value
//pos 
//A reference to a POSITION value returned by a previous call to GetNextSelectedItem 
//or GetFirstSelectedItemPosition. The value is updated to the next position by this
//call. 
 	CString  sSIp ;
 	sSIp =	m_list.GetItemText(n,1);
	
 	SetClipboard(sSIp.GetBuffer(sSIp.GetLength())); //拷入剪贴板
}

//复制目标IP,右键菜单
void CListenDlg::OnCopyDip() 
{
	// TODO: Add your command handler code here
	POSITION pos = m_list.GetFirstSelectedItemPosition();

 	int n=m_list.GetNextSelectedItem(pos);
	CString  sDIp ;
 	sDIp =	m_list.GetItemText(n,3);
	BOOL N=SetClipboard(sDIp.GetBuffer(sDIp.GetLength())); //拷入剪贴板

}
//关于
void CListenDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if(IDM_SYSMENU==nID)
		DialogBox(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDD_DIALOG1),m_hWnd,DialogProc);
	CDialog::OnSysCommand (nID, lParam);
}
BOOL CALLBACK DialogProc(HWND hwndDlg,UINT uMsg,
						 WPARAM wParam,LPARAM lParam)
{
	if(uMsg==WM_COMMAND&&LOWORD(wParam)==IDCLOSE)
	{
		EndDialog(hwndDlg,1);
		return TRUE;
	}
	return FALSE;
}

//DEL void CListenDlg::OnSelchangeList1()  //选择列表
//DEL {
//DEL 	// TODO: Add your control notification handler code here
//DEL 
//DEL 	int n=m_NewList.GetCurSel();
//DEL //	CString SelAllStr;
//DEL 	DWORD data=m_NewList.GetItemData(n); //得到绑定数据在m_IpShowDlg中显示
//DEL 	if (data<1)
//DEL 		return;
//DEL 	IPDATA* pIpData=(IPDATA*)data;
//DEL 	m_IpShowDlg.ShowIpData(pIpData->buf,pIpData->len);
//DEL //	CreateLogFile(pIpData->buf);
//DEL //	AfxMessageBox("幅度撒");
//DEL 
//DEL  
//DEL }

//清除和ListBox绑定的数据和列表
void CListenDlg::DelListBuf()
{
	for(int i=0;i<m_list.GetItemCount();i++)
	{
		DWORD data=m_list.GetItemData(i);
			if (data>0)
			{
				IPDATA* pData=(IPDATA*)data;
				delete pData; //清除堆内存
			}
	}
	m_IpShowDlg.m_CharEdit.SetWindowText("");
	m_IpShowDlg.m_HexEdit.SetWindowText("");
	m_IpShowDlg.m_NumEdit.SetWindowText("");
}

//程序退出
void CListenDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	DelListBuf();
	DeleteCriticalSection(&m_ls);
}

void CListenDlg::OnClickList3(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
 	UINT  n = m_list.GetHotItem( );
	if(n== -1)return ;
 	DWORD data=m_list.GetItemData(n); //得到绑定数据在m_IpShowDlg中显示
	if (data<1)
		return;
	IPDATA* pIpData=(IPDATA*)data;
	m_IpShowDlg.ShowIpData(pIpData->buf,pIpData->len);

	*pResult = 0;
}

void CListenDlg::OnRclickList3(NMHDR* pNMHDR, LRESULT* pResult) 
{
	CPoint point;
	GetCursorPos(&point);
  	CMenu menu;
	menu.LoadMenu (IDR_MENU1);
	CMenu* pContextMenu = menu.GetSubMenu (0);
	if (m_list.GetHotItem( )!=-1)
		pContextMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_RIGHTBUTTON,point.x,point.y,AfxGetMainWnd ()/*pWnd*/);	
	*pResult = 0;
}

void CListenDlg::AddTaskIcon()
{
	NOTIFYICONDATA d;
	d.cbSize=sizeof(NOTIFYICONDATA);
	d.hWnd=GetSafeHwnd();
	d.uID=121;
	d.uFlags=NIF_ICON | NIF_TIP | NIF_MESSAGE;
	d.uCallbackMessage=WM_MY_TRAY;
	d.hIcon=AfxGetApp() ->LoadIcon(IDI_HUDIE);
	memset(d.szTip,0,64);
	strncpy(d.szTip , "网络监视程序",12);

	 Shell_NotifyIcon(NIM_ADD,&d);
	 return ;

}

void CListenDlg::OnTray(WPARAM wparam, LPARAM lparam)
{
	UINT uMouseMsg=LOWORD(lparam);
	switch(uMouseMsg)
	{
	case WM_LBUTTONDOWN:
		{
			ShowWindow(IsWindowVisible()?SW_HIDE:SW_SHOWNORMAL);
		
		}break;

	case WM_RBUTTONDOWN:
		{
			CPoint point;
				GetCursorPos(&point);
 				COfficeXPMenu menu, *pSubMenu;
				menu.LoadMenu(IDR_MENU2);
				menu.SetType(TYPE_XP);
				if(!(pSubMenu = (COfficeXPMenu *)menu.GetSubMenu(0))) return ;
				pSubMenu->TrackPopupMenu(TPM_RIGHTBUTTON,point.x,point.y,this);
				menu.DestroyMenu();
		}
		break;  
	}	
}

void CListenDlg::OnCancel()
{
	ShowWindow(SW_HIDE);	
}

void CListenDlg::OnQuit() 
{
	NOTIFYICONDATA d;
	d.cbSize=sizeof(NOTIFYICONDATA);
	d.hWnd=GetSafeHwnd();
	d.uID=121;
	d.uFlags=NIF_ICON | NIF_TIP | NIF_MESSAGE;
	d.uCallbackMessage=WM_MY_TRAY;
 	Shell_NotifyIcon(NIM_DELETE,&d);
	exit(0);
	//CDialog::OnOK();	
}

void CListenDlg::BeginOrStopListen(int n)
{
		
	CButton* pBtn=(CButton *)GetDlgItem(IDC_BUTTON1);
	pBtn->GetCheck();
	CWinThread *pThread=NULL;

	m_HostIp=GetIp(); //得到本机IP
	if(1==n) //开始监听
	{	
		//设置状态栏时间
				CTime time=CTime::GetCurrentTime();	
				CString m_strTime="开始监听时间" + time.Format("%X");	
				m_wndStatusBar.SetText(m_strTime,1,0);

				//设置任务栏图标
					NOTIFYICONDATA d;
					d.cbSize=sizeof(NOTIFYICONDATA);
					d.hWnd=GetSafeHwnd();
					d.uID=121;
					d.uFlags=NIF_ICON | NIF_TIP | NIF_MESSAGE;
					d.uCallbackMessage=WM_MY_TRAY;
					d.hIcon=AfxGetApp() ->LoadIcon(IDI_ICON1);
					memset(d.szTip,0,64);
					strncpy(d.szTip , "网络监视程序",12);

					Shell_NotifyIcon(NIM_MODIFY,&d);


		if(CreateSock()!=0) //建立
		{
			AfxMessageBox("WinSock设置失败");
			DestroyWindow();
		}
		else
		{
			
			b_IsRun=TRUE;
			pThread=AfxBeginThread(RecvIpPro,this);
				

		}
		pBtn->SetWindowText("停止监听");
	}
	else //停止监听
	{ 
		CTime time=CTime::GetCurrentTime();	
		CString m_strTime="结束上次监听时间" + time.Format("%X");	
		m_wndStatusBar.SetText(m_strTime,1,0);
				//设置任务栏图标
					NOTIFYICONDATA d;
					d.cbSize=sizeof(NOTIFYICONDATA);
					d.hWnd=GetSafeHwnd();
					d.uID=121;
					d.uFlags=NIF_ICON | NIF_TIP | NIF_MESSAGE;
					d.uCallbackMessage=WM_MY_TRAY;
					d.hIcon=AfxGetApp() ->LoadIcon(IDI_HUDIE);
					memset(d.szTip,0,64);
					strncpy(d.szTip , "网络监视程序",12);

					Shell_NotifyIcon(NIM_MODIFY,&d);

		b_IsRun=FALSE;
		if(pThread)
		{
			TerminateThread(pThread->m_hThread, 0);
			CloseHandle(pThread->m_hThread);
		}

		if(m_RawSock)
			closesocket(m_RawSock);
		pBtn->SetWindowText("开始监听");
	}

}


void CListenDlg::Start()
{
	NOTIFYICONDATA d;
	d.cbSize=sizeof(NOTIFYICONDATA);
	d.hWnd=GetSafeHwnd();
	d.uID=121;
	d.uFlags=NIF_ICON | NIF_TIP | NIF_MESSAGE;
	d.uCallbackMessage=WM_MY_TRAY;
	d.hIcon=AfxGetApp() ->LoadIcon(IDI_ICON1);
	memset(d.szTip,0,64);
	strncpy(d.szTip , "网络监视程序",12);

	Shell_NotifyIcon(NIM_MODIFY,&d);

	BeginOrStopListen(1);
}

void CListenDlg::OnStop() 
{
	// TODO: Add your command handler code here

	NOTIFYICONDATA d;
	d.cbSize=sizeof(NOTIFYICONDATA);
	d.hWnd=GetSafeHwnd();
	d.uID=121;
	d.uFlags=NIF_ICON | NIF_TIP | NIF_MESSAGE;
	d.uCallbackMessage=WM_MY_TRAY;
	d.hIcon=AfxGetApp() ->LoadIcon(IDI_HUDIE);
	memset(d.szTip,0,64);
	strncpy(d.szTip , "网络监视程序",12);
	Shell_NotifyIcon(NIM_MODIFY,&d);

	BeginOrStopListen(0);

	
}

void CListenDlg::OnTop() 
{
	// TODO: Add your command handler code here
	UpdateData(true);

	if(m_bTopMost)
	{
		AfxGetMainWnd()->SetWindowPos(&CWnd::wndTopMost,0,0,0,0,SWP_NOSIZE | SWP_NOMOVE);
		m_bTopMost=false;
	}
	else
	{
		AfxGetMainWnd()->SetWindowPos(&CWnd::wndNoTopMost ,0,0,0,0,SWP_NOSIZE | SWP_NOMOVE);
		m_bTopMost=true;
	}
	
	
}

void CListenDlg::OnShowmainwindow() 
{
	ShowWindow(SW_SHOWNORMAL);	
}

void CListenDlg::OnButton4() 
{
	// TODO: Add your control notification handler code here
	m_Indlg.DoModal();
	
}

void CListenDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	CTime time=CTime::GetCurrentTime();	
	CString m_strTime="当前时间" + time.Format("%X");	
	m_wndStatusBar.SetText(m_strTime,2,0);

	CDialog::OnTimer(nIDEvent);
}

void CListenDlg::OnMenuTransport() 
{
	// TODO: Add your command handler code here
  	m_myqqdlg.Create(IDD_MYQQ_DIALOG);
 
	m_myqqdlg.ShowWindow(1);
	
}

void CListenDlg::OnButtonFliter() 
{
	// TODO: Add your control notification handler code here

	m_setdlg.DoModal();
	
}

void CListenDlg::OnMenuDir() 
{
	m_myddlg.ShowWindow(1);
}

void CListenDlg::OnMenuQuit() 
{
	NOTIFYICONDATA d;
	d.cbSize=sizeof(NOTIFYICONDATA);
	d.hWnd=GetSafeHwnd();
	d.uID=121;
	d.uFlags=NIF_ICON | NIF_TIP | NIF_MESSAGE;
	d.uCallbackMessage=WM_MY_TRAY;
	d.hIcon=AfxGetApp() ->LoadIcon(IDI_HUDIE);
	memset(d.szTip,0,64);
	strncpy(d.szTip , "网络监视程序",12);
	Shell_NotifyIcon(NIM_DELETE,&d);

	CDialog::OnOK();	
}

⌨️ 快捷键说明

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