www.pudn.com > CapturePacket.rar > CapturePacketDlg.cpp
// CapturePacketDlg.cpp : implementation file // #include "stdafx.h" #include "CapturePacket.h" #include "CapturePacketDlg.h" #include#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CCapturePacketDlg dialog CCapturePacketDlg::CCapturePacketDlg(CWnd* pParent /*=NULL*/) : CDialog(CCapturePacketDlg::IDD, pParent) { //{{AFX_DATA_INIT(CCapturePacketDlg) m_count = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CCapturePacketDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CCapturePacketDlg) DDX_Control(pDX, IDC_PACKET_LIST, m_packet); DDX_Control(pDX, IDC_START_BUTTON, m_startBtn); DDX_Control(pDX, IDC_END_BUTTON, m_endBtn); DDX_Control(pDX, IDC_NETCARD_COMBO, m_netCard); DDX_Text(pDX, IDC_COUNT_EDIT, m_count); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CCapturePacketDlg, CDialog) //{{AFX_MSG_MAP(CCapturePacketDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_START_BUTTON, OnStartButton) ON_BN_CLICKED(IDC_END_BUTTON, OnEndButton) ON_WM_CLOSE() ON_WM_TIMER() ON_WM_VSCROLL() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CCapturePacketDlg message handlers UINT FindData(LPVOID pParam) { CCapturePacketDlg *dlg = (CCapturePacketDlg *)AfxGetApp()->m_pMainWnd; dlg->PacMsg.getPacketInfo(dlg->m_netCard.GetCurSel()); return TRUE; } BOOL CCapturePacketDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here //开始捕获 m_pThread=AfxBeginThread(FindData, this, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED, NULL); // 创建并挂起线程 m_netCard.SetWindowText("------------------Please select netcard!-------------------"); m_packet.InsertColumn(0,"No.",LVCFMT_CENTER,50); m_packet.InsertColumn(1,"Time",LVCFMT_CENTER,90); m_packet.InsertColumn(2,"SourceAddress",LVCFMT_CENTER,165); m_packet.InsertColumn(3,"DestinanionAddress",LVCFMT_CENTER,165); m_packet.InsertColumn(4,"PacketLen",LVCFMT_CENTER,82); PacMsg.getNetCardInfo(); m_startBtn.EnableWindow(TRUE); m_endBtn.EnableWindow(FALSE); for(int i=0;i<20;i++) { draw[i]=0; } oldpacketCount=0; return TRUE; // return TRUE unless you set the focus to a control } void CCapturePacketDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CCapturePacketDlg::OnPaint() { /*if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle 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; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); /}*/ CPaintDC dc(this); CBrush brush; brush.CreateSolidBrush(RGB(240,240,240)); CBrush *pOldBrush=dc.SelectObject(&brush); CRect rect; GetClientRect(rect); dc.SetMapMode(MM_ISOTROPIC); dc.SetWindowExt(580,350); dc.SetViewportExt(rect.right,rect.bottom); dc.SetViewportOrg(0,0); dc.MoveTo(415,35); dc.LineTo(410,40); dc.MoveTo(415,35); dc.LineTo(420,40); dc.MoveTo(415,305); dc.LineTo(415,35); dc.MoveTo(415,305); dc.LineTo(575,305); dc.MoveTo(575,305); dc.LineTo(570,310); dc.MoveTo(575,305); dc.LineTo(570,300); dc.TextOut(420,35,"PacketCount"); dc.TextOut(555,310,"Time"); CString scale; for (int j=0;j<27;j++) { scale.Format("%d",j*5); int y=305-j*10; dc.MoveTo(415,y); dc.LineTo(417,y); if(j*10%50==0) dc.TextOut(398,y-5,scale); } /*CPen pen; pen.CreatePen(PS_SOLID,2,RGB(0,255,0)); dc.SelectObject(pen);*/ dc.MoveTo(415,305); for(int i=0;i<20;i++) { int x,y; x=7*i+415; y=305-draw[i]*2; if(y<45) y=45; dc.LineTo(x,y); dc.MoveTo(x,y); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CCapturePacketDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CCapturePacketDlg::OnStartButton() { CString str; int nIndex; nIndex=m_netCard.GetCurSel(); str.Format("%d",nIndex); //AfxMessageBox(str); if (str=="-1") { AfxMessageBox("Please select netcard!"); m_packet.SetWindowText("Please select netcard!"); } else if(str=="0") { AfxMessageBox("Selected netcard is not uesd!"); m_packet.SetWindowText("Please select netcard!"); } else { m_packet.DeleteAllItems(); m_startBtn.EnableWindow(FALSE); m_endBtn.EnableWindow(TRUE); m_pThread->ResumeThread(); // 恢复线程运行 SetTimer(1,1000,NULL); } CTime tm=CTime::GetCurrentTime(); packetTime.Format("Capture Packet time:%d-%d-%d %d:%d:%d",tm.GetYear(),tm.GetMonth(),tm.GetDay(),tm.GetHour(),tm.GetMinute(),tm.GetSecond()); } void CCapturePacketDlg::OnEndButton() { // TODO: Add your control notification handler code here m_endBtn.EnableWindow(FALSE); m_startBtn.EnableWindow(TRUE); m_pThread->SuspendThread(); KillTimer(1); CStdioFile m_logfile; if(!m_logfile.Open("CapPac.log",CFile::modeWrite)) { AfxMessageBox("Can not open CapPac.log"); return; } CString loginfo,netCardDescription; m_netCard.GetWindowText(netCardDescription); loginfo.Format("***************************************************Capture Packet*************************************************\n%s\n",netCardDescription); m_logfile.SeekToEnd(); m_logfile.WriteString(loginfo); m_logfile.SeekToEnd(); m_logfile.WriteString(packetTime); loginfo.Format("\nPacket Count:%d",PacMsg.packetCount); m_logfile.WriteString(loginfo); m_logfile.SeekToEnd(); CTime tm; tm=CTime::GetCurrentTime(); packetTime.Format("\nStop Capturing time:%d-%d-%d %d:%d:%d",tm.GetYear(),tm.GetMonth(),tm.GetDay(),tm.GetHour(),tm.GetMinute(),tm.GetSecond()); m_logfile.WriteString(packetTime); m_logfile.SeekToEnd(); loginfo.Format("\n*********************************************Stop Capturing Packet************************************************\n\n"); m_logfile.WriteString(loginfo); m_logfile.Close(); PacMsg.packetCount=0; oldpacketCount=0; } void CCapturePacketDlg::OnClose() { // TODO: Add your message handler code here and/or call default WSACleanup(); CDialog::OnClose(); } void CCapturePacketDlg::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default drawNum=PacMsg.packetCount-oldpacketCount; oldpacketCount=PacMsg.packetCount; for (int i=0;i<20;i++) { draw[i]=draw[i+1]; } draw[19]=drawNum; CString str; str.Format("%d",drawNum); m_count=str; UpdateData(FALSE); OnPaint(); Invalidate(); CDialog::OnTimer(nIDEvent); } void CCapturePacketDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) { // TODO: Add your message handler code here and/or call default CDialog::OnVScroll(nSBCode, nPos, pScrollBar); }