NetTrafficButtonDlg.cpp
上传用户:tintim
上传日期:2022-05-14
资源大小:1573k
文件大小:5k
源码类别:

网络截获/分析

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "NetTrafficButton.h"
  3. #include "NetTrafficButtonDlg.h"
  4. #ifdef _DEBUG
  5. #define new DEBUG_NEW
  6. #undef THIS_FILE
  7. static char THIS_FILE[] = __FILE__;
  8. #endif
  9. // 自身的一个指针,这是为回调函数服务的
  10. CNetTrafficButtonDlg* me;
  11. // 记录选择的接口号
  12. int SelectedInterface;
  13. // 一个回调函数
  14. void CALLBACK interfaceHasChanged(int interfacenumber);
  15. class CAboutDlg : public CDialog
  16. {
  17. public:
  18. CAboutDlg();
  19. //{{AFX_DATA(CAboutDlg)
  20. enum { IDD = IDD_ABOUTBOX };
  21. //}}AFX_DATA
  22. //{{AFX_VIRTUAL(CAboutDlg)
  23. protected:
  24. virtual void DoDataExchange(CDataExchange* pDX);
  25. //}}AFX_VIRTUAL
  26. protected:
  27. //{{AFX_MSG(CAboutDlg)
  28. //}}AFX_MSG
  29. DECLARE_MESSAGE_MAP()
  30. };
  31. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  32. {
  33. //{{AFX_DATA_INIT(CAboutDlg)
  34. //}}AFX_DATA_INIT
  35. }
  36. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  37. {
  38. CDialog::DoDataExchange(pDX);
  39. //{{AFX_DATA_MAP(CAboutDlg)
  40. //}}AFX_DATA_MAP
  41. }
  42. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  43. //{{AFX_MSG_MAP(CAboutDlg)
  44. // Keine Nachrichten-Handler
  45. //}}AFX_MSG_MAP
  46. END_MESSAGE_MAP()
  47. /////////////////////////////////////////////////////////////////////////////
  48. // CNetTrafficButtonDlg Dialogfield
  49. CNetTrafficButtonDlg::CNetTrafficButtonDlg(CWnd* pParent /*=NULL*/)
  50. : CDialog(CNetTrafficButtonDlg::IDD, pParent)
  51. {
  52. //{{AFX_DATA_INIT(CNetTrafficButtonDlg)
  53. //}}AFX_DATA_INIT
  54. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  55. //为回调函数服务的
  56. me = this;
  57. }
  58. void CNetTrafficButtonDlg::DoDataExchange(CDataExchange* pDX)
  59. {
  60. CDialog::DoDataExchange(pDX);
  61. //{{AFX_DATA_MAP(CNetTrafficButtonDlg)
  62. DDX_Control(pDX, IDC_BUTTON3, m_cOutgoing);
  63. DDX_Control(pDX, IDC_BUTTON2, m_cIncoming);
  64. DDX_Control(pDX, IDC_BUTTON1, m_cTotalTraffic);
  65. //}}AFX_DATA_MAP
  66. }
  67. BEGIN_MESSAGE_MAP(CNetTrafficButtonDlg, CDialog)
  68. //{{AFX_MSG_MAP(CNetTrafficButtonDlg)
  69. ON_WM_SYSCOMMAND()
  70. ON_WM_PAINT()
  71. ON_WM_QUERYDRAGICON()
  72. ON_BN_CLICKED(IDC_BUTTON4, OnAbout)
  73. //}}AFX_MSG_MAP
  74. END_MESSAGE_MAP()
  75. /////////////////////////////////////////////////////////////////////////////
  76. // CNetTrafficButtonDlg Message handler
  77. BOOL CNetTrafficButtonDlg::OnInitDialog()
  78. {
  79. CDialog::OnInitDialog();
  80. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  81. ASSERT(IDM_ABOUTBOX < 0xF000);
  82. CMenu* pSysMenu = GetSystemMenu(FALSE);
  83. if (pSysMenu != NULL)
  84. {
  85. CString strAboutMenu;
  86. strAboutMenu.LoadString(IDS_ABOUTBOX);
  87. if (!strAboutMenu.IsEmpty())
  88. {
  89. pSysMenu->AppendMenu(MF_SEPARATOR);
  90. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  91. }
  92. }
  93. SetIcon(m_hIcon, TRUE);
  94. SetIcon(m_hIcon, FALSE);
  95. // 使用第一个发现的网络连接
  96. int SelectedInterface = 0;
  97. // 每秒钟刷新一次网络状态
  98. UINT timerresolution = 1000;
  99. // To fake the Taskmanager....
  100. UINT gridresolution = 100;
  101. // 监视的接口
  102. m_cTotalTraffic.SetInterfaceNumber(SelectedInterface);
  103. // 这个button关注的时总流量
  104. m_cTotalTraffic.SelectTrafficType(MFTrafficButton::Traffic_Total);
  105. // 设置得到通知的回调函数
  106. m_cTotalTraffic.SetInterfaceNumberNotificationFunction(interfaceHasChanged);
  107. //设置更新速率
  108. m_cTotalTraffic.SetUpdateSpeed(timerresolution, gridresolution);
  109. m_cOutgoing.SetInterfaceNumber(SelectedInterface);
  110. m_cOutgoing.SelectTrafficType(MFTrafficButton::Traffic_Outgoing);
  111. m_cOutgoing.SetInterfaceNumberNotificationFunction(interfaceHasChanged);
  112. m_cOutgoing.SetUpdateSpeed(timerresolution, gridresolution);
  113. m_cIncoming.SetInterfaceNumber(SelectedInterface);
  114. m_cIncoming.SelectTrafficType(MFTrafficButton::Traffic_Incoming);
  115. m_cIncoming.SetInterfaceNumberNotificationFunction(interfaceHasChanged);
  116. m_cIncoming.SetUpdateSpeed(timerresolution, gridresolution);
  117. return TRUE;  
  118. }
  119. void CNetTrafficButtonDlg::OnSysCommand(UINT nID, LPARAM lParam)
  120. {
  121. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  122. {
  123. CAboutDlg dlgAbout;
  124. dlgAbout.DoModal();
  125. }
  126. else
  127. {
  128. CDialog::OnSysCommand(nID, lParam);
  129. }
  130. }
  131. void CNetTrafficButtonDlg::OnPaint() 
  132. {
  133. if (IsIconic())
  134. {
  135. CPaintDC dc(this);
  136. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  137. int cxIcon = GetSystemMetrics(SM_CXICON);
  138. int cyIcon = GetSystemMetrics(SM_CYICON);
  139. CRect rect;
  140. GetClientRect(&rect);
  141. int x = (rect.Width() - cxIcon + 1) / 2;
  142. int y = (rect.Height() - cyIcon + 1) / 2;
  143. dc.DrawIcon(x, y, m_hIcon);
  144. }
  145. else
  146. {
  147. CDialog::OnPaint();
  148. }
  149. }
  150. HCURSOR CNetTrafficButtonDlg::OnQueryDragIcon()
  151. {
  152. return (HCURSOR) m_hIcon;
  153. }
  154. //回调函数
  155. void CALLBACK interfaceHasChanged(int interfacenumber)
  156. {
  157. SelectedInterface = interfacenumber;
  158. CString text;
  159. text.Format("NetTrafficButton using Interface: %d",SelectedInterface);
  160. me->SetWindowText(text);
  161. }
  162. void CNetTrafficButtonDlg::OnAbout() 
  163. {
  164. // TODO: Add your control notification handler code here
  165. CAboutDlg about;
  166. about.DoModal();
  167. ::AfxGetMainWnd();
  168. }