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

📄 netstat.cpp

📁 Displays CPU time usage, the list of processes (can be terminated) and the task which are running (c
💻 CPP
字号:
//+NM Copyright (c) 2003 Qta
#include "stdafx.h"
#include "ITaskMgr.h"

#include "iphlpapi.h"
#include "netiphlp.h"

static BOOL ShowNetStatus(ThreadPack* pTP);
static BOOL DrawGraph(ThreadPack* pTP, HWND hwndDraw);

void Ift_Init (struct _IFTDATA* ift);
int Ift_Get (struct _IFTDATA* ift, int sec);

//-----------------------------------------------------------------------------
// [Net]僞僽
//-----------------------------------------------------------------------------
BOOL CALLBACK DlgProcNet(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	static ThreadPack* pTP = NULL;
	static struct _IFTDATA* ift = NULL;

	switch(Msg)
	{
	case WM_THREADPACK_POINTER:
	{
		pTP = (ThreadPack*)wParam;
		pTP->ift = LocalAlloc (LPTR, sizeof(struct _IFTDATA));
		ift = (struct _IFTDATA*) pTP->ift;
		Ift_Init (ift);
		Ift_Get (ift, 1);
		HWND hwndCb = GetDlgItem(hDlg, IDC_CB_NET);
		SendMessage (hwndCb, CB_RESETCONTENT, 0, 0);
		TCHAR buf[80];
		for(int idx = 0; idx < ift->nEntries && idx < MAX_NETTABLES; idx++) {
				wsprintf (buf, TEXT("%d: %s"), idx, ift->name[idx]);
				SendMessage (hwndCb, CB_ADDSTRING, 0, (LPARAM)buf);
		}
		SendMessage (hwndCb, CB_SETCURSEL, 0, 0);
		HWND hwndDraw = GetDlgItem(hDlg, IDC_NET_DRAW);
		DrawGraph(pTP, hwndDraw);
		ShowNetStatus(pTP);
		return TRUE;
	}	
	
	// ----------------------------------------------------------
	case WM_INITDIALOG:
	{
		return TRUE;
	}
	
	// ----------------------------------------------------------
	case WM_SIZE:
	{ 
		if( pTP == NULL )
			return 0;

		HWND hwndDraw = GetDlgItem(hDlg, IDC_NET_DRAW);
		HWND hwndText = GetDlgItem(hDlg, IDC_NET_TEXT);
		HWND hwndCb   = GetDlgItem(hDlg, IDC_CB_NET);

		HDWP hdwp;
		RECT rcTab;
		RECT rcDraw;
		RECT rcTitle;
		RECT rcCb;

		SetRect(&rcTab, 0, 0, LOWORD(lParam), HIWORD(lParam));
		TabCtrl_AdjustRect(hDlg, FALSE, &rcTab);

		GetClientRect(hwndCb, &rcCb);
		GetClientRect(hwndDraw, &rcDraw);
		GetClientRect(hwndText, &rcTitle);

		hdwp = BeginDeferWindowPos(3);

		DeferWindowPos(hdwp, hwndCb, HWND_TOP
			, 5
			, 5
			, rcTab.right - rcTab.left -5 -5
			, 5 + (rcCb.bottom - rcCb.top) + 100
			, 0);
	
		DeferWindowPos(hdwp, hwndText, HWND_TOP
			, 5
			, 5 + 5 + (rcCb.bottom - rcCb.top)
			, rcTab.right - rcTab.left -5 -5
			, 5 + 5 + (rcCb.bottom - rcCb.top) + (rcTitle.bottom - rcTitle.top)
			, 0);

		DeferWindowPos(hdwp, hwndDraw, HWND_TOP
			, 5
			, 5 + 5 + 5 + (rcCb.bottom - rcCb.top) + (rcTitle.bottom - rcTitle.top)
			, rcTab.right - rcTab.left -5 -5
			, (rcDraw.bottom - rcDraw.top)
			, 0);

		EndDeferWindowPos(hdwp);
		ShowNetStatus(pTP);
		return 0;
	} 

	case WM_COMMAND:
	{
		int id = LOWORD (wParam);
		int cmd = HIWORD (wParam);
		switch (id) {
		case IDC_CB_NET:
			switch (cmd) {
			case CBN_SELENDOK:
				if (ift) {
					HWND hwndCb   = GetDlgItem(hDlg, IDC_CB_NET);
					int idx = SendMessage (hwndCb, CB_GETCURSEL, 0 ,0);
					if (idx >= 0) ift->nComNo = idx;
				}
				break;
			}
			break;
		}
	}
	
	// ----------------------------------------------------------
	case WM_DRAWITEM:
	if (pTP) {
		LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT)lParam;
		if (lpdis) DrawGraph(pTP, lpdis->hwndItem);
		return TRUE;
	}

	case WM_TIMER:
	if (pTP && ift) {
		int sec = pTP->dwInterval / 1000;
		if (sec < 1) sec = 1;
		int nEntries = ift->nEntries;
		Ift_Get (ift, sec);
		ShowNetStatus(pTP);
		if (ift->nEntries && pTP->nMode == MODE_NETSTATUS) {
			InvalidateRect(GetDlgItem(hDlg, IDC_NET_DRAW), NULL, FALSE);
		}
		if (nEntries != ift->nEntries) {
			HWND hwndCb = GetDlgItem(hDlg, IDC_CB_NET);
			int sel = SendMessage (hwndCb, CB_GETCURSEL, 0, 0);
			if (sel <= ift->nEntries) sel = 0;
			SendMessage (hwndCb, CB_RESETCONTENT, 0, 0);
			TCHAR buf[80];
			for(int idx = 0; idx < ift->nEntries && idx < MAX_NETTABLES; idx++) {
				wsprintf (buf, TEXT("%d: %s"), idx, ift->name[idx]);
				SendMessage (hwndCb, CB_ADDSTRING, 0, (LPARAM)buf);
			}
			SendMessage (hwndCb, CB_SETCURSEL, sel, 0);
		}
		break;
	}

	default:
		break;

	}
	return FALSE;

}

//-----------------------------------------------------------------------------
// 僱僢僩儚乕僋忣曬傪僥僉僗僩昞帵
//-----------------------------------------------------------------------------
static BOOL ShowNetStatus(ThreadPack* pTP)
{
	if( pTP == NULL )
		return FALSE;

	struct _IFTDATA* ift = (struct _IFTDATA*) pTP->ift;
	if (ift == NULL)
		return FALSE;

	if (ift->Table == NULL)
		return FALSE;

	TCHAR szFmt[256];

	HWND hDlg;
	HWND hwndStatus;

	if( (hDlg = pTP->hwndNetStatus ) == NULL )
		return FALSE;
	if( (hwndStatus = GetDlgItem(hDlg, IDC_NET_TEXT) ) == NULL )
		return FALSE;

	if (ift->fSupport == FALSE) {
		wsprintf(szFmt, TEXT("Not support."));
		SetWindowText(hwndStatus, szFmt);
		return TRUE;
	}

	if (ift->nEntries == 0) {
		wsprintf(szFmt, TEXT("Not found device."));
		SetWindowText(hwndStatus, szFmt);
		return TRUE;
	}

	int idx = ift->nComNo;

	TCHAR spd[32];
	if (ift->Table->table[idx].dwSpeed >= 1000000)
		wsprintf (spd, TEXT("%3dMbps"), ift->Table->table[idx].dwSpeed / 1000000);
	else
		wsprintf (spd, TEXT("%3dbps"), ift->Table->table[idx].dwSpeed);

	wsprintf(szFmt
		,	_T("Name\t%s\r\n")
			_T("Recv\t%5dKB/%5dbyte/s (%5d)\r\n")
			_T("Send\t%5dKB/%5dbyte/s (%5d)\r\n")
			_T("MTU %d Speed %s Packet %d/%d")

			,ift->name[idx]
			,ift->r[idx].nByte, ift->r[idx].nRate, ift->r[idx].nMax
			,ift->s[idx].nByte, ift->s[idx].nRate, ift->s[idx].nMax
			,ift->Table->table[idx].dwMtu
			,spd
			,ift->Table->table[idx].dwInUcastPkts 
			,ift->Table->table[idx].dwInErrors
		);

	SetWindowText(hwndStatus, szFmt);
	return TRUE;
}

//-----------------------------------------------------------------------------
// 僱僢僩儚乕僋忣曬傪僌儔僼昞帵
//-----------------------------------------------------------------------------
static BOOL DrawGraph(ThreadPack* pTP, HWND hwndDraw)
{
	int ii;

	HDC hDC;
	RECT rc;

	HPEN hpenDarkGreen;
	HPEN hpenLightGreen;
	HPEN hpenYellow;
	HPEN hOldPen;

	POINT pntBuf[MAX_NTIMES];
	memset( pntBuf, 0, sizeof(POINT)*MAX_NTIMES );

	GetClientRect(hwndDraw, &rc);

	if( pTP->nMode != MODE_NETSTATUS )
		return TRUE;

	struct _IFTDATA* ift = (struct _IFTDATA*) pTP->ift;
	if (ift == NULL)
		return FALSE;

	if (ift->Table == NULL)
		return FALSE;

	static int nXLine = 0;
	nXLine = (nXLine+=2) %12;

	if( !(hpenDarkGreen = CreatePen(PS_SOLID, 1, RGB(0, 127, 0))))
	{
		return FALSE;
	}

	if( !(hpenLightGreen = CreatePen(PS_SOLID, 1, RGB(0, 255, 0))))
	{
		DeleteObject(hpenDarkGreen);
		return FALSE;
	}

	if( !(hpenYellow = CreatePen(PS_SOLID, 1, RGB(255, 255, 0))))
	{
		DeleteObject(hpenDarkGreen);
		DeleteObject(hpenLightGreen);
		return FALSE;
	}

	if(!(hDC = GetDC(hwndDraw)))
	{
		DeleteObject(hpenDarkGreen);
		DeleteObject(hpenLightGreen);
		DeleteObject(hpenYellow);
		return FALSE;
	}

	// paint background
	SelectObject(hDC, GetStockObject(BLACK_BRUSH));

	Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);

	hOldPen = (HPEN)SelectObject(hDC, hpenDarkGreen);

	POINT pnt[2];
	for( ii = 0; ii < rc.bottom - rc.top; ii+=12 )
	{
		pnt[0].x = rc.left;
		pnt[1].x = rc.right;
		pnt[0].y = pnt[1].y = ii + rc.top;
		Polyline(hDC, &pnt[0], 2);
	}

	for( ii = 12; ii < (rc.right - rc.left) + nXLine; ii+=12 )
	{
		pnt[0].x = pnt[1].x = ii + rc.left - nXLine;
		pnt[0].y = rc.top;
		pnt[1].y = rc.bottom;
		Polyline(hDC, &pnt[0], 2);
	}

	SelectObject(hDC, hOldPen);
	POINT* pPoint;

	int idx = ift->nComNo;
	int max = 4000;	// mini 32kbps
	if (ift->r[idx].nMax > max) max = ift->r[idx].nMax;
	if (ift->s[idx].nMax > max) max = ift->s[idx].nMax;
	if (max < 1) max = 1;

	// Draw Recv
	pPoint = &pntBuf[0];
	int xx;

	int n = ift->nTime;

	xx = rc.right;
	for( ii = 0; (ii < MAX_NTIMES) && (xx > rc.left); ii++, xx-=2, pPoint++ )
	{
		n--;
		if (n < 0) n = MAX_NTIMES - 1;
		int per = (ift->r[idx].nPar[n]) * 100 / max;
		if (per > 100) per = 100;

		LONG lHeight = (100 - per);
		pPoint->x = xx;
		pPoint->y = rc.top + lHeight * (rc.bottom - rc.top)/100 ;
	}

	hOldPen = (HPEN)SelectObject(hDC, hpenLightGreen);
	Polyline(hDC, &pntBuf[0], ii);
	SelectObject(hDC, hOldPen);

	// Draw Send
	pPoint = &pntBuf[0];

	n = ift->nTime;

	xx = rc.right;
	for( ii = 0; (ii < MAX_NTIMES) && (xx > rc.left); ii++, xx-=2, pPoint++ )
	{
		n--;
		if (n < 0) n = MAX_NTIMES - 1;
		int per = (ift->s[idx].nPar[n]) * 100 / max;
		if (per > 100) per = 100;

		LONG lHeight = (100 - per);
		pPoint->x = xx;
		pPoint->y = rc.top + lHeight * (rc.bottom - rc.top)/100 ;
	}

	hOldPen = (HPEN)SelectObject(hDC, hpenYellow);
	Polyline(hDC, &pntBuf[0], ii);
	SelectObject(hDC, hOldPen);



	// finish
	SelectObject(hDC, hOldPen);
	DeleteObject(hpenDarkGreen);
	DeleteObject(hpenLightGreen);
	DeleteObject(hpenYellow);
	ReleaseDC(hwndDraw, hDC);

	return TRUE;
}

//----------------------------------------------------------
// 僞僗僋僩儗僀偵傾僀僐儞傪僙僢僩偡傞
//----------------------------------------------------------
HICON SetNetIcon (HWND hWnd, HBITMAP hbmColor, HBITMAP hbmMask, HICON hIcon, ThreadPack* pTP)
{
	if (pTP == NULL) return hIcon;
	struct _IFTDATA* ift = (struct _IFTDATA*) pTP->ift;
	if (ift == NULL) return hIcon;

	if (ift->nEntries == 0) return hIcon;

	if (ift->nZeroSpeed > 20) return hIcon;

	HICON hIconSave = hIcon;

	ICONINFO ii;
	ii.fIcon    = TRUE;
	ii.xHotspot = 16;
	ii.yHotspot = 16;
	ii.hbmColor = hbmColor;
	ii.hbmMask  = hbmMask;
	hIcon = CreateIconIndirect (&ii);

	if (hIcon) {
		NOTIFYICONDATA	tn;
		memset(&tn, 0, sizeof(tn));
		tn.cbSize           = sizeof(tn);
		tn.hWnd             = hWnd;
		tn.uID              = 2;
		tn.uFlags           = NIF_ICON;
		tn.uCallbackMessage = MY_NOTIFYICON;
		tn.hIcon            = hIcon;
		if (Shell_NotifyIcon(NIM_MODIFY, &tn) == FALSE) {
			Shell_NotifyIcon (NIM_ADD, &tn);
		}
		if (hIconSave) DestroyIcon (hIconSave);
	}
	else {
		hIcon = hIconSave;
	}

	return hIcon;
}

//----------------------------------------------------------
// 僞僗僋僩儗僀偐傜傾僀僐儞傪嶍彍
//----------------------------------------------------------
void DelNetIcon (HWND hWnd)
{
	NOTIFYICONDATA	tn;
	memset(&tn, 0, sizeof(tn));
	tn.cbSize           = sizeof(tn);
	tn.hWnd             = hWnd;
	tn.uID              = 2;
	tn.uFlags           = NIF_MESSAGE;
	tn.uCallbackMessage = MY_NOTIFYICON;
	tn.hIcon            = NULL;
	Shell_NotifyIcon (NIM_DELETE, &tn);
}

//----------------------------------------------------------
// 傾僀僐儞梡偺夋憸傪昤夋偡傞
//----------------------------------------------------------
void DrawNetIcon (HDC hDC, ThreadPack* pTP)
{
	if (pTP == NULL) return;
	struct _IFTDATA* ift = (struct _IFTDATA*) pTP->ift;
	if (ift == NULL) return;

	if (ift->nZeroSpeed > 20) return;

	int idx = ift->nComNo;
	int n = ift->nTime;
	int max = 4000;	// mini 32kbps
	if (ift->r[idx].nMax > max) max = ift->r[idx].nMax;
//	if (ift->s[idx].nMax > max) max = ift->s[idx].nMax;
	if (max < 1) max = 1;

	POINT pntSend[16];
	POINT pntRecv[16];
	int nPts = 0;

	for (int i = 0; i < 16; i+=2) {
		n--;
		if (n < 0) n = MAX_NTIMES - 1;

		int perRecv = (ift->r[idx].nPar[n]) * 16 / max;
		int perSend = (ift->s[idx].nPar[n]) * 16 / max;
		if (perRecv > 16) perRecv = 16;
		if (perSend > 16) perSend = 16;

		pntRecv[nPts].x = 16 - 1 - i;
		pntRecv[nPts].y = (16 - perRecv);

		pntSend[nPts].x = 16 - 1 - i;
		pntSend[nPts].y = (16 - perSend);

		nPts++;
	}

	RECT rect = {0,0,16,16};
	HBRUSH hBrush = CreateSolidBrush (0x000000);
	FillRect (hDC, &rect, (HBRUSH) hBrush);
	DeleteObject (hBrush);

	HPEN hpenLightGreen = CreatePen (PS_SOLID, 1, RGB(0, 255, 0));
	HPEN hpenYellow     = CreatePen (PS_SOLID, 1, RGB(255, 255, 0));
	HPEN hOldPen;

	hOldPen = (HPEN)SelectObject(hDC, hpenYellow);
	Polyline(hDC, pntSend, nPts);
	
	SelectObject(hDC, hpenLightGreen);
	Polyline(hDC, pntRecv, nPts);
	
	if (hOldPen) SelectObject(hDC, hOldPen);
	DeleteObject(hpenLightGreen);
}

⌨️ 快捷键说明

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