mftrafficbutton.cpp
上传用户:fangbo2004
上传日期:2022-05-14
资源大小:2257k
文件大小:14k
源码类别:

网络截获/分析

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "MFTrafficButton.h"
  3. #include "MemDC.h"
  4. #include <math.h>
  5. #ifndef _WIN32_IE
  6. #define _WIN32_IE 0x0500
  7. #endif
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. /////////////////////////////////////////////////////////////////////////////
  14. // MFTrafficButton
  15. MFTrafficButton::MFTrafficButton()
  16. {
  17. brushInitalized = FALSE;
  18. interfaceCallBack = NULL;
  19. useAdaptiveScale = FALSE;
  20. gridxstartpos = 0;
  21. gridystartpos = 0;
  22. gridxresolution = GRIDXRESOLUTION;
  23. gridyresolution = GRIDYRESOLUTION;
  24. gridscrollxspeed = GRIDSCROLLXSPEED;
  25. gridscrollyspeed = GRIDSCROLLYSPEED; 
  26. plotgranularity = PLOTGRANULATRITY;
  27. netupdatespeed = NETUPDATESPEED;
  28. gridupdatespeed = GRIDUPDATESPEED;
  29. ToggleStatusText = "局部最大值";
  30. }
  31. MFTrafficButton::~MFTrafficButton()
  32. {
  33. }
  34. BEGIN_MESSAGE_MAP(MFTrafficButton, CButton)
  35. //{{AFX_MSG_MAP(MFTrafficButton)
  36. ON_WM_TIMER()
  37. ON_WM_RBUTTONDOWN()
  38. //}}AFX_MSG_MAP
  39. END_MESSAGE_MAP()
  40. /////////////////////////////////////////////////////////////////////////////
  41. // MFTrafficButton message handlers
  42. void MFTrafficButton::PreSubclassWindow() 
  43. {
  44. CButton::PreSubclassWindow();
  45. // 这个函数确保这个button是用来绘制的而不是接收按下的消息的
  46. ModifyStyle( 0 , BS_OWNERDRAW );
  47. this->GetWindowRect(&TrafficDrawRectangle);
  48. this->GetWindowRect(&TrafficDrawUpdateRectangle);
  49. ScreenToClient(&TrafficDrawUpdateRectangle);
  50. ScreenToClient(&TrafficDrawRectangle);
  51. TGSize.cx = TrafficDrawRectangle.right - TrafficDrawRectangle.left;
  52. TGSize.cy = TrafficDrawRectangle.bottom - TrafficDrawRectangle.top;
  53. initalized = FALSE;
  54. MaxTrafficAmount = 0.0;
  55. SelectedInterface = 0;
  56. smallFont.CreateFont(-10,0,0,0,FW_THIN, FALSE, FALSE, FALSE, DEFAULT_CHARSET,OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, VARIABLE_PITCH, "Times New Roman");
  57. red = RGB(255,125,125);
  58. green = RGB(125,255,125);
  59. cyan = RGB(0,255,255);
  60. darkblue= RGB(0,0,75);
  61. // darkblue= RGB(255,255,255);
  62. darkgray= RGB(50,50,50);
  63. white = RGB(255,255,255);
  64. black = RGB(0,0,0);
  65. lightgreen = RGB(156,255,156);
  66. darkgreen = RGB(32,64,32);
  67. // darkgreen    =0;
  68. greenbrush.CreateSolidBrush(green);
  69. redbrush.CreateSolidBrush(red);
  70. GridPen.CreatePen(PS_SOLID ,1 , darkgreen);
  71. GetWindowRect(&TrafficDrawRectangle);
  72. ScreenToClient(&TrafficDrawRectangle);
  73. TGSize.cx = TrafficDrawRectangle.right - TrafficDrawRectangle.left;
  74. TGSize.cy = TrafficDrawRectangle.bottom - TrafficDrawRectangle.top;
  75. TrafficEntries = TGSize.cx / plotgranularity;
  76. TrafficStats = new TRAFFICENTRY[TrafficEntries+1];
  77. for(DWORD x=0; x<TrafficEntries; x++)
  78. {
  79. TrafficStats[x].connected = TRUE;//FALSE;
  80. TrafficStats[x].value = 0.0;//(float)x * (float)TrafficEntries*0.05;//;10.0 + 10.0*(sin(5.0*(float)x*3.14/180.0));
  81. }
  82. MaxTrafficAmount = 0.0;
  83. SelectedInterface = 0;
  84. CRgn rectRgn, ellRgn, finalRgn;
  85. rectRgn.CreateRectRgn(0,0,TGSize.cx, TGSize.cy);
  86. ShapeWNDRegion.CreateRectRgn(0,0,TGSize.cx, TGSize.cy);;
  87. ShapeDCRegion.CreateRectRgn(0,0,TGSize.cx, TGSize.cy);;
  88. int x1,x2,y1,y2,xe,ye,xs,ys;
  89. int xof, yof;
  90. int r;
  91. xs = TGSize.cx;
  92. ys = TGSize.cy;
  93. x1 = 0;
  94. y1 = 0;
  95. x2 = xs;
  96. y2 = ys;
  97. xe = 5;//Radius of edge
  98. ye = 5;//Radius of edge
  99. xof = (int)( (float)xs*0.0);
  100. yof = (int)( (float)ys*0.0);
  101. r = ellRgn.CreateRoundRectRgn(x1,y1,x2,y2,xe,ye);
  102. r = ellRgn.OffsetRgn(-xof, -yof);
  103. r = ShapeWNDRegion.CombineRgn(&rectRgn, &ellRgn,RGN_AND );
  104. r = ShapeDCRegion.CombineRgn(&rectRgn, &ellRgn,RGN_AND );
  105. this->SetWindowRgn((HRGN)ShapeWNDRegion, TRUE);
  106. initalized = TRUE;
  107. SetTimer(GRIDTIMER, gridupdatespeed, 0);
  108. SetTimer(NETTIMER, netupdatespeed, 0);
  109. }
  110. void MFTrafficButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
  111. {
  112. // TODO: Add your message handler code here and/or call default
  113. // CButton::OnDrawItem(nIDCtl, lpDrawItemStruct);
  114. ASSERT( lpDrawItemStruct != NULL );
  115. CDC * pDC   = CDC::FromHandle( lpDrawItemStruct -> hDC );
  116. int erg = pDC->SelectClipRgn(&ShapeDCRegion);
  117. CRect rect = lpDrawItemStruct -> rcItem;
  118. UINT state = lpDrawItemStruct -> itemState;
  119. UINT nStyle = GetStyle( );
  120. int nSavedDC = pDC -> SaveDC( );
  121. // Create the brush for the color bar
  122. if(brushInitalized == FALSE)
  123. {
  124. CBitmap bmp;
  125. CMemDC *memDC = new CMemDC(pDC);
  126. RECT clipRect;
  127. memDC->GetClipBox(&clipRect);
  128. if(clipRect.right - clipRect.left > 1)
  129. {
  130. bmp.CreateCompatibleBitmap(memDC,plotgranularity, TGSize.cy);
  131. CBitmap *pOld = memDC->SelectObject(&bmp);
  132. CSize bmps = bmp.GetBitmapDimension();
  133. double factor = 255.0 / (float)TGSize.cy;
  134. BYTE r,g,b;
  135. for(int x = 0; x<TGSize.cy; x++)
  136. {
  137. g = (BYTE)(255-factor*x);
  138. r = (BYTE)(factor*x);
  139. b = (BYTE)64;
  140. memDC->SetPixelV(0,x,RGB(r,g,b));
  141. memDC->SetPixelV(1,x,RGB(r,g,b));
  142. }
  143. memDC->SelectObject(pOld);
  144. colorbrush.CreatePatternBrush(&bmp);
  145. brushInitalized = TRUE;
  146. }
  147. }
  148. if(initalized == TRUE)
  149. {
  150. COLORREF backcolor = GetSysColor(COLOR_BTNFACE);
  151. CBrush brush;
  152. CMemDC *memDC = new CMemDC(pDC);
  153. RECT clipRect;
  154. memDC->GetClipBox(&clipRect);
  155. memDC->FillSolidRect(&clipRect,backcolor);
  156. CFont *oldFont;
  157. int xp, yp, xx, yy;
  158. orgBrushOrigin = memDC->GetBrushOrg();
  159. oldFont = memDC->SelectObject(&smallFont);
  160. double scale = (double)TGSize.cy / (double)MaxTrafficAmount;
  161. yp = TrafficDrawRectangle.bottom;
  162. xp = TrafficDrawRectangle.left;
  163. RECT fillrect;
  164. CString tmp;
  165. // 填充背景
  166. back = memDC->GetBkColor();
  167. brush.CreateSolidBrush(darkblue);
  168. memDC->FillRect(&TrafficDrawRectangle, &brush);
  169. // 画网格
  170. int xgridlines, ygridlines;
  171. xgridlines = TGSize.cx / gridxresolution;
  172. ygridlines = TGSize.cy / gridyresolution;
  173. CPen* oldPen = memDC->SelectObject(&GridPen);
  174. // 创建垂直线
  175. for (int x=0; x<= xgridlines; x++)
  176. {
  177. memDC->MoveTo(x*gridxresolution + gridxstartpos, 0 );
  178. memDC->LineTo(x*gridxresolution + gridxstartpos, TGSize.cy );
  179. }
  180. // 添加水平线
  181. for (int y=0; y<= ygridlines; y++)
  182. {
  183. memDC->MoveTo(0 , gridystartpos + TGSize.cy - y*gridyresolution - 2);
  184. memDC->LineTo(TGSize.cx , gridystartpos + TGSize.cy - y*gridyresolution - 2);
  185. }
  186. gridxstartpos += gridscrollxspeed;
  187. gridystartpos += gridscrollyspeed;
  188. if(gridxstartpos < 0 ) gridxstartpos = gridxresolution;
  189. if(gridxstartpos > gridxresolution ) gridxstartpos = 0;
  190. if(gridystartpos < 0 ) gridystartpos = gridyresolution;
  191. if(gridystartpos > gridyresolution ) gridystartpos = 0;
  192. memDC->SelectObject(oldPen );
  193. for(DWORD cnt=0; cnt<TrafficEntries; cnt++)
  194. {
  195. xx = xp + cnt*plotgranularity;
  196. double traffic = (double)TrafficStats[cnt].value; 
  197. yy = yp - (int)((double)TrafficStats[cnt].value * scale);
  198. // 网络处在连接状态才绘制
  199. if(TrafficStats[cnt].connected == TRUE)
  200. {
  201. fillrect.bottom = yp;
  202. fillrect.top = yy;
  203. fillrect.left = xx;
  204. fillrect.right = xx+plotgranularity;
  205. memDC->SetBrushOrg(xx,yp);
  206. if(TrafficStats[cnt].value > 0.0) 
  207. {
  208. memDC->FillRect(&fillrect, &colorbrush);
  209. memDC->SetPixelV(xx, yy, cyan);
  210. }
  211. }
  212. }
  213. tmp.Format("%8.1f",TrafficStats[TrafficEntries-1].value);
  214. COLORREF textcolor = memDC->GetTextColor();
  215. int bkmode = memDC->GetBkMode();
  216. memDC->SetBkMode(TRANSPARENT);
  217. memDC->SetTextColor(darkblue);
  218. memDC->TextOut(6,5,AllTraffic);
  219. memDC->SetTextColor(cyan);
  220. // memDC->SetTextColor(darkblue);
  221. memDC->TextOut(5,5,AllTraffic); 
  222. memDC->SetTextColor(textcolor);
  223. memDC->SetBkMode(bkmode);
  224. memDC->SelectObject(oldFont);
  225. memDC->SetBrushOrg(orgBrushOrigin.x, orgBrushOrigin.y);
  226. delete memDC;
  227. }
  228. pDC -> RestoreDC( nSavedDC );
  229. }
  230. void MFTrafficButton::OnTimer(UINT nIDEvent) 
  231. {
  232. // TODO: Add your message handler code here and/or call default
  233. if(nIDEvent == NETTIMER)
  234. {
  235. #ifdef _I_HAVE_PLATFORM_SDK_INSTALLED_
  236. DWORD flag, reserved;
  237. BOOL erg;
  238. flag = 0;//INTERNET_CONNECTION_OFFLINE ;
  239. reserved = 0;
  240. TCHAR connectionname[1024];
  241. erg =  InternetGetConnectedStateEx(
  242. &flag, //OUT LPDWORD lpdwFlags,
  243. (LPTSTR)&connectionname,//OUT LPTSTR lpszConnectionName,
  244. 1024,//IN DWORD dwNameLen,
  245. 0//IN DWORD dwReserved
  246. );
  247. isOnline = erg;
  248. #else
  249. isOnline = TRUE;
  250. #endif
  251. // 得到当前的流量
  252. double traffic = m_cTrafficClass.GetTraffic(SelectedInterface);
  253. DWORD  totaltraffic = m_cTrafficClass.GetInterfaceTotalTraffic(SelectedInterface);
  254. double delta1;
  255. double divisor = (1000.0/(double)NETUPDATESPEED);
  256. delta1 = (double)(traffic * divisor) / 1024.0;
  257. CurrentTraffic.Format("当前流量: %.1f KB/sec",delta1);
  258. // Should we recalculate the local maximum per session or per display?
  259. if(useAdaptiveScale==TRUE)
  260. {
  261. MaxTrafficAmount = 0.0;
  262. }
  263. // Shift whole array 1 step to left and calculate local maximum
  264. for(DWORD x=0; x<TrafficEntries; x++)
  265. {
  266. TrafficStats[x].connected = TrafficStats[x+1].connected;
  267. TrafficStats[x].value = TrafficStats[x+1].value;
  268. if(TrafficStats[x].value > MaxTrafficAmount)
  269. MaxTrafficAmount = TrafficStats[x].value;
  270. }
  271. if(isOnline == TRUE)
  272. {
  273. TrafficStats[TrafficEntries].connected = TRUE;
  274. TrafficStats[TrafficEntries].value = traffic;
  275. if(TrafficStats[TrafficEntries].value > MaxTrafficAmount)
  276. MaxTrafficAmount = TrafficStats[TrafficEntries].value;
  277. }
  278. else
  279. {
  280. TrafficStats[TrafficEntries].connected = FALSE;
  281. TrafficStats[TrafficEntries].value = traffic;
  282. if(TrafficStats[TrafficEntries].value > MaxTrafficAmount)
  283. MaxTrafficAmount = TrafficStats[TrafficEntries].value;
  284. }
  285. double delta2;
  286. divisor = (1000.0/(double)NETUPDATESPEED);
  287. delta2 = (double)(MaxTrafficAmount * divisor) / 1024.0;
  288. MaximalTraffic.Format("最大流量:%.1f KB/sec",delta2);
  289. AllTraffic.Format("总流量为: %.1f / %.1f KB/sec",delta1, delta2);
  290. }
  291. // 绘制
  292. Invalidate(FALSE);
  293. CButton::OnTimer(nIDEvent);
  294. }
  295. //创建右键按下后产生的一个pop-up菜单
  296. void MFTrafficButton::OnRButtonDown(UINT nFlags, CPoint point) 
  297. {
  298. // TODO: Add your message handler code here and/or call default
  299. #define STARTINDEX 2000
  300. #define ENDINDEX 2999
  301. #define SET_SCALING 3000
  302. HMENU hMenu , hPopup;
  303. long retId = 0;
  304. int InterfaceIndex = STARTINDEX;
  305. POINT pt;
  306. hMenu = CreatePopupMenu( );   
  307. if( hMenu )
  308. {
  309. //  If the menu was created, add the item text
  310. CString tmp, tmp2;
  311. double tottraff = (double)m_cTrafficClass.GetInterfaceTotalTraffic(SelectedInterface) / (1024.0*1024.0);
  312. m_cTrafficClass.GetNetworkInterfaceName(&tmp, SelectedInterface);
  313. tmp2.Format("接口名称:%s : %.1f MB", tmp, tottraff);
  314. AppendMenu( hMenu , MF_STRING , 1001 , tmp2 );
  315. AppendMenu( hMenu , MF_SEPARATOR , 1000 , NULL );
  316. // Submenu
  317. hPopup = CreatePopupMenu( );   //  Create a new popup menu
  318. if( hPopup )
  319. {
  320. CString tmp;
  321. int cnt;
  322. for(cnt=0; cnt<=m_cTrafficClass.GetNetworkInterfacesCount(); cnt++)
  323. {
  324. CString tmp2;
  325. double tottraff = (double)m_cTrafficClass.GetInterfaceTotalTraffic(cnt) / (1024.0*1024.0);
  326. m_cTrafficClass.GetNetworkInterfaceName(&tmp, cnt);
  327. tmp2.Format("%s : %.1f MB", tmp, tottraff);
  328. AppendMenu( hPopup , MF_STRING , InterfaceIndex++ , tmp2 );
  329. }
  330. AppendMenu( hMenu , MF_POPUP , ( UINT )hPopup , "选择一个接口" );
  331. AppendMenu( hMenu , MF_SEPARATOR , 1000 , NULL );
  332. AppendMenu( hMenu , MF_STRING , SET_SCALING , "锁定的缩放比例:" +  ToggleStatusText);
  333. }
  334. }
  335. GetCursorPos( & pt );
  336. //显示菜单
  337. retId = TrackPopupMenu( hMenu , TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD ,  pt.x , pt.y , 0 , this -> m_hWnd , NULL );  //  start the menu
  338. DestroyMenu( hMenu );
  339. if( retId == 0 )
  340. {
  341. return;
  342. }
  343. else
  344. {
  345. if(retId == SET_SCALING)
  346. {
  347. if(useAdaptiveScale == TRUE)
  348. {
  349. useAdaptiveScale = FALSE;
  350. ToggleStatusText = "全局最大值";
  351. }
  352. else
  353. {
  354. useAdaptiveScale = TRUE;
  355. ToggleStatusText = "局部最大值";
  356. }
  357. }
  358. if((retId>=STARTINDEX) && (retId<ENDINDEX) )
  359. {
  360. SelectedInterface = retId-STARTINDEX;
  361. InterfaceHasChanged();
  362. ReInit(SelectedInterface);
  363. }
  364. }
  365. CButton::OnRButtonDown(nFlags, point);
  366. }
  367. void MFTrafficButton::ReInit(RECT newSize)
  368. {
  369. KillTimer(GRIDTIMER);
  370. KillTimer(NETTIMER);
  371. delete [] TrafficStats;
  372. TGSize.cx = newSize.right - newSize.left;
  373. TGSize.cy = newSize.bottom - newSize.top;
  374. TrafficEntries = TGSize.cx / plotgranularity;
  375. TrafficStats = new TRAFFICENTRY[TrafficEntries+1];
  376. MaxTrafficAmount = 0.0;
  377. SelectedInterface = 0;
  378. for(DWORD x=0; x<TrafficEntries; x++)
  379. {
  380. TrafficStats[x].connected = TRUE;
  381. TrafficStats[x].value = 0.0;
  382. }
  383. m_cTrafficClass.GetTraffic(SelectedInterface);
  384. m_cTrafficClass.GetTraffic(SelectedInterface);
  385. MaxTrafficAmount = 0.0;
  386. SetTimer(GRIDTIMER, gridupdatespeed, 0);
  387. SetTimer(NETTIMER, netupdatespeed, 0);
  388. }
  389. //监视其他接口
  390. void MFTrafficButton::ReInit(int newInterface)
  391. {
  392. KillTimer(GRIDTIMER);
  393. KillTimer(NETTIMER);
  394. SelectedInterface = newInterface;
  395. for(DWORD x=0; x<TrafficEntries; x++)
  396. {
  397. TrafficStats[x].connected = TRUE;
  398. TrafficStats[x].value = 0.0;
  399. }
  400. m_cTrafficClass.GetTraffic(SelectedInterface);
  401. m_cTrafficClass.GetTraffic(SelectedInterface);
  402. MaxTrafficAmount = 0.0;
  403. SetTimer(GRIDTIMER, gridupdatespeed, 0);
  404. SetTimer(NETTIMER, netupdatespeed, 0);
  405. }
  406. //设置更新速度
  407. void MFTrafficButton::SetUpdateSpeed(UINT netspeed, UINT gridspeed)
  408. {
  409. gridupdatespeed = gridspeed;
  410. netupdatespeed = netspeed;
  411. KillTimer(GRIDTIMER);
  412. KillTimer(NETTIMER);
  413. SetTimer(GRIDTIMER, gridupdatespeed, 0);
  414. SetTimer(NETTIMER, netupdatespeed, 0);
  415. }
  416. //回调函数
  417. void MFTrafficButton::InterfaceHasChanged()
  418. {
  419. if(interfaceCallBack !=NULL)
  420. {
  421. interfaceCallBack(SelectedInterface);
  422. }
  423. }
  424. void MFTrafficButton::SetInterfaceNumberNotificationFunction(INTERFACECHANCEDPROC callfunct)
  425. {
  426. interfaceCallBack = callfunct;
  427. }
  428. void MFTrafficButton::SelectTrafficType(int trafficType)
  429. {
  430. switch(trafficType)
  431. {
  432. case Traffic_Incoming:  m_cTrafficClass.SetTrafficType(MFNetTraffic::IncomingTraffic); break;
  433. case Traffic_Outgoing: m_cTrafficClass.SetTrafficType(MFNetTraffic::OutGoingTraffic); break;
  434. case Traffic_Total: m_cTrafficClass.SetTrafficType(MFNetTraffic::AllTraffic); break;
  435. default: m_cTrafficClass.SetTrafficType(MFNetTraffic::AllTraffic);
  436. }
  437. }
  438. void MFTrafficButton::SetInterfaceNumber(int interfacenumber)
  439. {
  440. SelectedInterface = interfacenumber;
  441. ReInit(SelectedInterface);
  442. }
  443. void MFTrafficButton::SetAdaptiveScaling(BOOL adaptive)
  444. {
  445. useAdaptiveScale = adaptive;
  446. }