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

网络截获/分析

开发平台:

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. // m_cTrafficClass.SetTrafficType(IncomingTraffic);
  253. // m_cTrafficClass.SetTrafficType(OutGoingTraffic);
  254. //m_cTrafficClass.SetTrafficType(0);
  255. double traffic = m_cTrafficClass.GetTraffic(SelectedInterface);
  256. DWORD  totaltraffic = m_cTrafficClass.GetInterfaceTotalTraffic(SelectedInterface);
  257. double delta1;
  258. double divisor = (1000.0/(double)NETUPDATESPEED);
  259. delta1 = (double)(traffic * divisor) / 1024.0;
  260. CurrentTraffic.Format("当前流量: %.1f KB/sec",delta1);
  261. // Should we recalculate the local maximum per session or per display?
  262. if(useAdaptiveScale==TRUE)
  263. {
  264. MaxTrafficAmount = 0.0;
  265. }
  266. // Shift whole array 1 step to left and calculate local maximum
  267. for(DWORD x=0; x<TrafficEntries; x++)
  268. {
  269. TrafficStats[x].connected = TrafficStats[x+1].connected;
  270. TrafficStats[x].value = TrafficStats[x+1].value;
  271. if(TrafficStats[x].value > MaxTrafficAmount)
  272. MaxTrafficAmount = TrafficStats[x].value;
  273. }
  274. if(isOnline == TRUE)
  275. {
  276. TrafficStats[TrafficEntries].connected = TRUE;
  277. TrafficStats[TrafficEntries].value = traffic;
  278. if(TrafficStats[TrafficEntries].value > MaxTrafficAmount)
  279. MaxTrafficAmount = TrafficStats[TrafficEntries].value;
  280. }
  281. else
  282. {
  283. TrafficStats[TrafficEntries].connected = FALSE;
  284. TrafficStats[TrafficEntries].value = traffic;
  285. if(TrafficStats[TrafficEntries].value > MaxTrafficAmount)
  286. MaxTrafficAmount = TrafficStats[TrafficEntries].value;
  287. }
  288. double delta2;
  289. divisor = (1000.0/(double)NETUPDATESPEED);
  290. delta2 = (double)(MaxTrafficAmount * divisor) / 1024.0;
  291. MaximalTraffic.Format("最大流量:%.1f KB/sec",delta2);
  292. AllTraffic.Format("流量为: %.1f / %.1f KB/sec",delta1, delta2);
  293. }
  294. // 绘制
  295. Invalidate(FALSE);
  296. CButton::OnTimer(nIDEvent);
  297. }
  298. //创建右键按下后产生的一个pop-up菜单
  299. void MFTrafficButton::OnRButtonDown(UINT nFlags, CPoint point) 
  300. {
  301. // TODO: Add your message handler code here and/or call default
  302. #define STARTINDEX 2000
  303. #define ENDINDEX 2999
  304. #define SET_SCALING 3000
  305. HMENU hMenu , hPopup;
  306. long retId = 0;
  307. int InterfaceIndex = STARTINDEX;
  308. POINT pt;
  309. hMenu = CreatePopupMenu( );   
  310. if( hMenu )
  311. {
  312. //  If the menu was created, add the item text
  313. CString tmp, tmp2;
  314. double tottraff = (double)m_cTrafficClass.GetInterfaceTotalTraffic(SelectedInterface) / (1024.0*1024.0);
  315. m_cTrafficClass.GetNetworkInterfaceName(&tmp, SelectedInterface);
  316. tmp2.Format("接口名称:%s : %.1f MB", tmp, tottraff);
  317. AppendMenu( hMenu , MF_STRING , 1001 , tmp2 );
  318. AppendMenu( hMenu , MF_SEPARATOR , 1000 , NULL );
  319. // Submenu
  320. hPopup = CreatePopupMenu( );   //  Create a new popup menu
  321. if( hPopup )
  322. {
  323. CString tmp;
  324. int cnt;
  325. for(cnt=0; cnt<=m_cTrafficClass.GetNetworkInterfacesCount(); cnt++)
  326. {
  327. CString tmp2;
  328. double tottraff = (double)m_cTrafficClass.GetInterfaceTotalTraffic(cnt) / (1024.0*1024.0);
  329. m_cTrafficClass.GetNetworkInterfaceName(&tmp, cnt);
  330. tmp2.Format("%s : %.1f MB", tmp, tottraff);
  331. AppendMenu( hPopup , MF_STRING , InterfaceIndex++ , tmp2 );
  332. }
  333. AppendMenu( hMenu , MF_POPUP , ( UINT )hPopup , "选择一个接口" );
  334. AppendMenu( hMenu , MF_SEPARATOR , 1000 , NULL );
  335. AppendMenu( hMenu , MF_STRING , SET_SCALING , "锁定的缩放比例:" +  ToggleStatusText);
  336. }
  337. }
  338. GetCursorPos( & pt );
  339. //显示菜单
  340. retId = TrackPopupMenu( hMenu , TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD ,  pt.x , pt.y , 0 , this -> m_hWnd , NULL );  //  start the menu
  341. DestroyMenu( hMenu );
  342. if( retId == 0 )
  343. {
  344. return;
  345. }
  346. else
  347. {
  348. if(retId == SET_SCALING)
  349. {
  350. if(useAdaptiveScale == TRUE)
  351. {
  352. useAdaptiveScale = FALSE;
  353. ToggleStatusText = "全局最大值";
  354. }
  355. else
  356. {
  357. useAdaptiveScale = TRUE;
  358. ToggleStatusText = "局部最大值";
  359. }
  360. }
  361. if((retId>=STARTINDEX) && (retId<ENDINDEX) )
  362. {
  363. SelectedInterface = retId-STARTINDEX;
  364. InterfaceHasChanged();
  365. ReInit(SelectedInterface);
  366. }
  367. }
  368. CButton::OnRButtonDown(nFlags, point);
  369. }
  370. void MFTrafficButton::ReInit(RECT newSize)
  371. {
  372. KillTimer(GRIDTIMER);
  373. KillTimer(NETTIMER);
  374. delete [] TrafficStats;
  375. TGSize.cx = newSize.right - newSize.left;
  376. TGSize.cy = newSize.bottom - newSize.top;
  377. TrafficEntries = TGSize.cx / plotgranularity;
  378. TrafficStats = new TRAFFICENTRY[TrafficEntries+1];
  379. MaxTrafficAmount = 0.0;
  380. SelectedInterface = 0;
  381. for(DWORD x=0; x<TrafficEntries; x++)
  382. {
  383. TrafficStats[x].connected = TRUE;
  384. TrafficStats[x].value = 0.0;
  385. }
  386. m_cTrafficClass.GetTraffic(SelectedInterface);
  387. m_cTrafficClass.GetTraffic(SelectedInterface);
  388. MaxTrafficAmount = 0.0;
  389. SetTimer(GRIDTIMER, gridupdatespeed, 0);
  390. SetTimer(NETTIMER, netupdatespeed, 0);
  391. }
  392. //监视其他接口
  393. void MFTrafficButton::ReInit(int newInterface)
  394. {
  395. KillTimer(GRIDTIMER);
  396. KillTimer(NETTIMER);
  397. SelectedInterface = newInterface;
  398. for(DWORD x=0; x<TrafficEntries; x++)
  399. {
  400. TrafficStats[x].connected = TRUE;
  401. TrafficStats[x].value = 0.0;
  402. }
  403. m_cTrafficClass.GetTraffic(SelectedInterface);
  404. m_cTrafficClass.GetTraffic(SelectedInterface);
  405. MaxTrafficAmount = 0.0;
  406. SetTimer(GRIDTIMER, gridupdatespeed, 0);
  407. SetTimer(NETTIMER, netupdatespeed, 0);
  408. }
  409. //设置更新速度
  410. void MFTrafficButton::SetUpdateSpeed(UINT netspeed, UINT gridspeed)
  411. {
  412. gridupdatespeed = gridspeed;
  413. netupdatespeed = netspeed;
  414. KillTimer(GRIDTIMER);
  415. KillTimer(NETTIMER);
  416. SetTimer(GRIDTIMER, gridupdatespeed, 0);
  417. SetTimer(NETTIMER, netupdatespeed, 0);
  418. }
  419. //回调函数
  420. void MFTrafficButton::InterfaceHasChanged()
  421. {
  422. if(interfaceCallBack !=NULL)
  423. {
  424. interfaceCallBack(SelectedInterface);
  425. }
  426. }
  427. void MFTrafficButton::SetInterfaceNumberNotificationFunction(INTERFACECHANCEDPROC callfunct)
  428. {
  429. interfaceCallBack = callfunct;
  430. }
  431. void MFTrafficButton::SelectTrafficType(int trafficType)
  432. {
  433. switch(trafficType)
  434. {
  435. case Traffic_Incoming:  m_cTrafficClass.SetTrafficType(MFNetTraffic::IncomingTraffic); break;
  436. case Traffic_Outgoing: m_cTrafficClass.SetTrafficType(MFNetTraffic::OutGoingTraffic); break;
  437. case Traffic_Total: m_cTrafficClass.SetTrafficType(MFNetTraffic::AllTraffic); break;
  438. default: m_cTrafficClass.SetTrafficType(MFNetTraffic::AllTraffic);
  439. }
  440. }
  441. void MFTrafficButton::SetInterfaceNumber(int interfacenumber)
  442. {
  443. SelectedInterface = interfacenumber;
  444. ReInit(SelectedInterface);
  445. }
  446. void MFTrafficButton::SetAdaptiveScaling(BOOL adaptive)
  447. {
  448. useAdaptiveScale = adaptive;
  449. }