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

📄 mftrafficbutton.cpp

📁 介绍 网络流量统计源代码 正文 网络流量统计源代码。 很好的网络流量统计源代码。 使用曲线图进行实时统计。 采集数据主要在类 MFNetTraffic 中实现
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		
		// last print the textual statistic
		tmp.Format("%8.1f",TrafficStats[TrafficEntries-1].value);
		COLORREF textcolor = memDC->GetTextColor();
		int bkmode = memDC->GetBkMode();
		memDC->SetBkMode(TRANSPARENT);
		memDC->SetTextColor(darkblue);
		memDC->TextOut(6,5,AllTraffic);
		memDC->SetTextColor(cyan);
		memDC->TextOut(5,5,AllTraffic); 
		memDC->SetTextColor(textcolor);
		memDC->SetBkMode(bkmode);
		
		memDC->SelectObject(oldFont);
		memDC->SetBrushOrg(orgBrushOrigin.x, orgBrushOrigin.y);
	
		delete memDC;
	}	
	pDC -> RestoreDC( nSavedDC );

}

// if you have the Microsoft platform SDK, uncomment the following statement
// and insert Wininet.lib in the linker section of the compiler
// #define _I_HAVE_PLATFORM_SDK_INSTALLED_

void MFTrafficButton::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(nIDEvent == NETTIMER)
	{
		
// If you have installed the Microsoft Platform SDK be happy, because you can use the feature
// added below..
#ifdef _I_HAVE_PLATFORM_SDK_INSTALLED_		

		DWORD flag, reserved;
		BOOL erg;
		flag = 0;//INTERNET_CONNECTION_OFFLINE ;
		reserved = 0;
		TCHAR connectionname[1024];
		//	erg = InternetGetConnectedState(&flag, reserved);
		erg =  InternetGetConnectedStateEx(
			&flag,	//OUT LPDWORD lpdwFlags,
			(LPTSTR)&connectionname,//OUT LPTSTR lpszConnectionName,
			1024,//IN DWORD dwNameLen,
			0//IN DWORD dwReserved
			);
		
		isOnline = erg;
#else
		isOnline = TRUE;
#endif		
		// Get current traffic
		double traffic = m_cTrafficClass.GetTraffic(SelectedInterface);
		DWORD  totaltraffic = m_cTrafficClass.GetInterfaceTotalTraffic(SelectedInterface);
		
		double delta1;
		double divisor = (1000.0/(double)NETUPDATESPEED);
		delta1 = (double)(traffic * divisor) / 1024.0;
		CurrentTraffic.Format("%.1f KB/sec",delta1);
		//TRACE("Current Traffic: %s\n", CurrentTraffic);
		
		// Should we recalculate the local maximum per session or per display?
		if(useAdaptiveScale==TRUE)
		{
			MaxTrafficAmount = 0.0;
		}
		// Shift whole array 1 step to left and calculate local maximum
		for(DWORD x=0; x<TrafficEntries; x++)
		{
			TrafficStats[x].connected = TrafficStats[x+1].connected;
			TrafficStats[x].value	= TrafficStats[x+1].value;
			if(TrafficStats[x].value > MaxTrafficAmount)
				MaxTrafficAmount = TrafficStats[x].value;
		}
		if(isOnline == TRUE)
		{
			TrafficStats[TrafficEntries].connected = TRUE;
			TrafficStats[TrafficEntries].value = traffic;
			if(TrafficStats[TrafficEntries].value > MaxTrafficAmount)
				MaxTrafficAmount = TrafficStats[TrafficEntries].value;
		}
		else
		{
			TrafficStats[TrafficEntries].connected = FALSE;
			TrafficStats[TrafficEntries].value = traffic;
			if(TrafficStats[TrafficEntries].value > MaxTrafficAmount)
				MaxTrafficAmount = TrafficStats[TrafficEntries].value;
		}
		
		double delta2;
		divisor = (1000.0/(double)NETUPDATESPEED);
		delta2 = (double)(MaxTrafficAmount * divisor) / 1024.0;
		MaximalTraffic.Format("%.1f KB/sec",delta2);
		AllTraffic.Format("CUR: %.1f KB/SEC   HHG: %.1f KB/SEC",delta1, delta2);
		
	}

	// Force a redraw
	Invalidate(FALSE);

	CButton::OnTimer(nIDEvent);
}

/*
	Create little popup
*/
void MFTrafficButton::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
#define STARTINDEX	2000
#define ENDINDEX	2999
#define SET_SCALING	3000

	HMENU hMenu , hPopup;
	long retId = 0;
	int InterfaceIndex = STARTINDEX;
	POINT pt;
	
	hMenu = CreatePopupMenu( );			  //  Create a new popup menu
	if( hMenu )
	{	
		//  If the menu was created, add the item text
		CString tmp, tmp2;
		double tottraff = (double)m_cTrafficClass.GetInterfaceTotalTraffic(SelectedInterface) / (1024.0*1024.0);
		m_cTrafficClass.GetNetworkInterfaceName(&tmp, SelectedInterface);
		tmp2.Format("%s : %.1f MB", tmp, tottraff);
		
		AppendMenu( hMenu , MF_STRING , 1001 , tmp2 );
		AppendMenu( hMenu , MF_SEPARATOR , 1000 , NULL );
		// Submenu
		hPopup = CreatePopupMenu( );			  //  Create a new popup menu
		if( hPopup )
		{
			CString tmp;
			int cnt;
			for(cnt=0; cnt<=m_cTrafficClass.GetNetworkInterfacesCount(); cnt++)
			{
				CString tmp2;
				double tottraff = (double)m_cTrafficClass.GetInterfaceTotalTraffic(cnt) / (1024.0*1024.0);
				m_cTrafficClass.GetNetworkInterfaceName(&tmp, cnt);
				
				tmp2.Format("%s : %.1f MB", tmp, tottraff);
				AppendMenu( hPopup , MF_STRING , InterfaceIndex++ , tmp2 );
			}
			AppendMenu( hMenu , MF_POPUP , ( UINT )hPopup , "Select Interface" );
			AppendMenu( hMenu , MF_SEPARATOR , 1000 , NULL );
			AppendMenu( hMenu , MF_STRING , SET_SCALING , "Toggle scaling to " + 	ToggleStatusText);
		}
	}
	
	GetCursorPos( & pt );			 //  Get the cursor position and
	retId = TrackPopupMenu( hMenu , TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD ,  pt.x , pt.y , 0 , this -> m_hWnd , NULL );  //  start the menu
	DestroyMenu( hMenu );				//  Kill the menu when done
	if( retId == 0 )
	{
		return;
	}
	else
	{
		if(retId == SET_SCALING)
		{
			if(useAdaptiveScale == TRUE)
			{
				useAdaptiveScale = FALSE;
				ToggleStatusText = "global maximum";
			}
			else
			{
				useAdaptiveScale = TRUE;
				ToggleStatusText = "local maximum";
			}
		}
		if((retId>=STARTINDEX) && (retId<ENDINDEX) )
		{
			SelectedInterface = retId-STARTINDEX;
			InterfaceHasChanged();
			ReInit(SelectedInterface);
		}
	}
	
	CButton::OnRButtonDown(nFlags, point);
}

/*
	Button size has changed, we need an update of statistics.
	I am not sure if that works, because never used this...
*/
void MFTrafficButton::ReInit(RECT newSize)
{
	KillTimer(GRIDTIMER);
	KillTimer(NETTIMER);

	delete [] TrafficStats;

	TGSize.cx = newSize.right - newSize.left;
	TGSize.cy = newSize.bottom - newSize.top;

	TrafficEntries = TGSize.cx / plotgranularity;
	TrafficStats = new TRAFFICENTRY[TrafficEntries+1];
	
	MaxTrafficAmount = 0.0;
	SelectedInterface = 0;
	
	for(DWORD x=0; x<TrafficEntries; x++)
	{
		TrafficStats[x].connected = TRUE;
		TrafficStats[x].value	= 0.0;
	}
	m_cTrafficClass.GetTraffic(SelectedInterface);
	m_cTrafficClass.GetTraffic(SelectedInterface);
	MaxTrafficAmount = 0.0;
	SetTimer(GRIDTIMER,	gridupdatespeed,	0);
	SetTimer(NETTIMER,	netupdatespeed,		0);
	
}

/*
	We want to monitor another interface
*/
void MFTrafficButton::ReInit(int newInterface)
{
	KillTimer(GRIDTIMER);
	KillTimer(NETTIMER);

	SelectedInterface = newInterface;

	for(DWORD x=0; x<TrafficEntries; x++)
	{
		TrafficStats[x].connected = TRUE;
		TrafficStats[x].value	= 0.0;
	}
	m_cTrafficClass.GetTraffic(SelectedInterface);
	m_cTrafficClass.GetTraffic(SelectedInterface);
	MaxTrafficAmount = 0.0;
	SetTimer(GRIDTIMER,	gridupdatespeed,	0);
	SetTimer(NETTIMER,	netupdatespeed,		0);
	
}

void MFTrafficButton::SetUpdateSpeed(UINT netspeed, UINT gridspeed)
{
	gridupdatespeed	= gridspeed;
	netupdatespeed	= netspeed;
	KillTimer(GRIDTIMER);
	KillTimer(NETTIMER);
	SetTimer(GRIDTIMER,	gridupdatespeed,	0);
	SetTimer(NETTIMER,	netupdatespeed,		0);
}



/*
	React calling the callback function
*/
void MFTrafficButton::InterfaceHasChanged()
{
	if(interfaceCallBack !=NULL)
	{
		interfaceCallBack(SelectedInterface);
	}
}

/*
	Is someone wants to be informed, he has to tell us so
*/
void MFTrafficButton::SetInterfaceNumberNotificationFunction(INTERFACECHANCEDPROC callfunct)
{
	interfaceCallBack = callfunct;	
}

/*
	Which kind of traffic do you want to monitor?
*/
void MFTrafficButton::SelectTrafficType(int trafficType)
{
	switch(trafficType)
	{
	case Traffic_Incoming: 	m_cTrafficClass.SetTrafficType(MFNetTraffic::IncomingTraffic); break;
	case Traffic_Outgoing:	m_cTrafficClass.SetTrafficType(MFNetTraffic::OutGoingTraffic); break;
	case Traffic_Total:		m_cTrafficClass.SetTrafficType(MFNetTraffic::AllTraffic); break;
	default:				m_cTrafficClass.SetTrafficType(MFNetTraffic::AllTraffic);
	}
}

/*
	Which interface do you want to monitor
*/
void MFTrafficButton::SetInterfaceNumber(int interfacenumber)
{
	SelectedInterface = interfacenumber;
	ReInit(SelectedInterface);
}

/*
	Sound important, isnt it?
	Decides, wether to scale the graph using the total maximum traffic amount or the current maximum
*/
void MFTrafficButton::SetAdaptiveScaling(BOOL adaptive)
{
	useAdaptiveScale = adaptive;
}

⌨️ 快捷键说明

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