#include "stdafx.h"
#include "MFTrafficButton.h"
#include "MemDC.h"
#include <math.h>
#ifndef _WIN32_IE
#define _WIN32_IE 0x0500
#endif
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// MFTrafficButton
MFTrafficButton::MFTrafficButton()
{
brushInitalized = FALSE;
interfaceCallBack = NULL;
useAdaptiveScale = FALSE;
gridxstartpos = 0;
gridystartpos = 0;
gridxresolution = GRIDXRESOLUTION;
gridyresolution = GRIDYRESOLUTION;
gridscrollxspeed = GRIDSCROLLXSPEED;
gridscrollyspeed = GRIDSCROLLYSPEED;
plotgranularity = PLOTGRANULATRITY;
netupdatespeed = NETUPDATESPEED;
gridupdatespeed = GRIDUPDATESPEED;
ToggleStatusText = "局部最大值";
}
MFTrafficButton::~MFTrafficButton()
{
}
BEGIN_MESSAGE_MAP(MFTrafficButton, CButton)
//{{AFX_MSG_MAP(MFTrafficButton)
ON_WM_TIMER()
ON_WM_RBUTTONDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// MFTrafficButton message handlers
void MFTrafficButton::PreSubclassWindow()
{
CButton::PreSubclassWindow();
// 这个函数确保这个button是用来绘制的而不是接收按下的消息的
ModifyStyle( 0 , BS_OWNERDRAW );
this->GetWindowRect(&TrafficDrawRectangle);
this->GetWindowRect(&TrafficDrawUpdateRectangle);
ScreenToClient(&TrafficDrawUpdateRectangle);
ScreenToClient(&TrafficDrawRectangle);
TGSize.cx = TrafficDrawRectangle.right - TrafficDrawRectangle.left;
TGSize.cy = TrafficDrawRectangle.bottom - TrafficDrawRectangle.top;
initalized = FALSE;
MaxTrafficAmount = 0.0;
SelectedInterface = 0;
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");
red = RGB(255,125,125);
green = RGB(125,255,125);
cyan = RGB(0,255,255);
darkblue= RGB(0,0,75);
// darkblue= RGB(255,255,255);
darkgray= RGB(50,50,50);
white = RGB(255,255,255);
black = RGB(0,0,0);
lightgreen = RGB(156,255,156);
darkgreen = RGB(32,64,32);
// darkgreen =0;
greenbrush.CreateSolidBrush(green);
redbrush.CreateSolidBrush(red);
GridPen.CreatePen(PS_SOLID ,1 , darkgreen);
GetWindowRect(&TrafficDrawRectangle);
ScreenToClient(&TrafficDrawRectangle);
TGSize.cx = TrafficDrawRectangle.right - TrafficDrawRectangle.left;
TGSize.cy = TrafficDrawRectangle.bottom - TrafficDrawRectangle.top;
TrafficEntries = TGSize.cx / plotgranularity;
TrafficStats = new TRAFFICENTRY[TrafficEntries+1];
for(DWORD x=0; x<TrafficEntries; x++)
{
TrafficStats[x].connected = TRUE;//FALSE;
TrafficStats[x].value = 0.0;//(float)x * (float)TrafficEntries*0.05;//;10.0 + 10.0*(sin(5.0*(float)x*3.14/180.0));
}
MaxTrafficAmount = 0.0;
SelectedInterface = 0;
CRgn rectRgn, ellRgn, finalRgn;
rectRgn.CreateRectRgn(0,0,TGSize.cx, TGSize.cy);
ShapeWNDRegion.CreateRectRgn(0,0,TGSize.cx, TGSize.cy);;
ShapeDCRegion.CreateRectRgn(0,0,TGSize.cx, TGSize.cy);;
int x1,x2,y1,y2,xe,ye,xs,ys;
int xof, yof;
int r;
xs = TGSize.cx;
ys = TGSize.cy;
x1 = 0;
y1 = 0;
x2 = xs;
y2 = ys;
xe = 5;//Radius of edge
ye = 5;//Radius of edge
xof = (int)( (float)xs*0.0);
yof = (int)( (float)ys*0.0);
r = ellRgn.CreateRoundRectRgn(x1,y1,x2,y2,xe,ye);
r = ellRgn.OffsetRgn(-xof, -yof);
r = ShapeWNDRegion.CombineRgn(&rectRgn, &ellRgn,RGN_AND );
r = ShapeDCRegion.CombineRgn(&rectRgn, &ellRgn,RGN_AND );
this->SetWindowRgn((HRGN)ShapeWNDRegion, TRUE);
initalized = TRUE;
SetTimer(GRIDTIMER, gridupdatespeed, 0);
SetTimer(NETTIMER, netupdatespeed, 0);
}
void MFTrafficButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your message handler code here and/or call default
// CButton::OnDrawItem(nIDCtl, lpDrawItemStruct);
ASSERT( lpDrawItemStruct != NULL );
CDC * pDC = CDC::FromHandle( lpDrawItemStruct -> hDC );
int erg = pDC->SelectClipRgn(&ShapeDCRegion);
CRect rect = lpDrawItemStruct -> rcItem;
UINT state = lpDrawItemStruct -> itemState;
UINT nStyle = GetStyle( );
int nSavedDC = pDC -> SaveDC( );
// Create the brush for the color bar
if(brushInitalized == FALSE)
{
CBitmap bmp;
CMemDC *memDC = new CMemDC(pDC);
RECT clipRect;
memDC->GetClipBox(&clipRect);
if(clipRect.right - clipRect.left > 1)
{
bmp.CreateCompatibleBitmap(memDC,plotgranularity, TGSize.cy);
CBitmap *pOld = memDC->SelectObject(&bmp);
CSize bmps = bmp.GetBitmapDimension();
double factor = 255.0 / (float)TGSize.cy;
BYTE r,g,b;
for(int x = 0; x<TGSize.cy; x++)
{
g = (BYTE)(255-factor*x);
r = (BYTE)(factor*x);
b = (BYTE)64;
memDC->SetPixelV(0,x,RGB(r,g,b));
memDC->SetPixelV(1,x,RGB(r,g,b));
}
memDC->SelectObject(pOld);
colorbrush.CreatePatternBrush(&bmp);
brushInitalized = TRUE;
}
}
if(initalized == TRUE)
{
COLORREF backcolor = GetSysColor(COLOR_BTNFACE);
CBrush brush;
CMemDC *memDC = new CMemDC(pDC);
RECT clipRect;
memDC->GetClipBox(&clipRect);
memDC->FillSolidRect(&clipRect,backcolor);
CFont *oldFont;
int xp, yp, xx, yy;
orgBrushOrigin = memDC->GetBrushOrg();
oldFont = memDC->SelectObject(&smallFont);
double scale = (double)TGSize.cy / (double)MaxTrafficAmount;
yp = TrafficDrawRectangle.bottom;
xp = TrafficDrawRectangle.left;
RECT fillrect;
CString tmp;
// 填充背景
back = memDC->GetBkColor();
brush.CreateSolidBrush(darkblue);
memDC->FillRect(&TrafficDrawRectangle, &brush);
// 画网格
int xgridlines, ygridlines;
xgridlines = TGSize.cx / gridxresolution;
ygridlines = TGSize.cy / gridyresolution;
CPen* oldPen = memDC->SelectObject(&GridPen);
// 创建垂直线
for (int x=0; x<= xgridlines; x++)
{
memDC->MoveTo(x*gridxresolution + gridxstartpos, 0 );
memDC->LineTo(x*gridxresolution + gridxstartpos, TGSize.cy );
}
// 添加水平线
for (int y=0; y<= ygridlines; y++)
{
memDC->MoveTo(0 , gridystartpos + TGSize.cy - y*gridyresolution - 2);
memDC->LineTo(TGSize.cx , gridystartpos + TGSize.cy - y*gridyresolution - 2);
}
gridxstartpos += gridscrollxspeed;
gridystartpos += gridscrollyspeed;
if(gridxstartpos < 0 ) gridxstartpos = gridxresolution;
if(gridxstartpos > gridxresolution ) gridxstartpos = 0;
if(gridystartpos < 0 ) gridystartpos = gridyresolution;
if(gridystartpos > gridyresolution ) gridystartpos = 0;
memDC->SelectObject(oldPen );
for(DWORD cnt=0; cnt<TrafficEntries; cnt++)
{
xx = xp + cnt*plotgranularity;
double traffic = (double)TrafficStats[cnt].value;
yy = yp - (int)((double)TrafficStats[cnt].value * scale);
// 网络处在连接状态才绘制
if(TrafficStats[cnt].connected == TRUE)
{
fillrect.bottom = yp;
fillrect.top = yy;
fillrect.left = xx;
fillrect.right = xx+plotgranularity;
memDC->SetBrushOrg(xx,yp);
if(TrafficStats[cnt].value > 0.0)
{
memDC->FillRect(&fillrect, &colorbrush);
memDC->SetPixelV(xx, yy, cyan);
}
}
}
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->SetTextColor(darkblue);
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 );
}
void MFTrafficButton::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(nIDEvent ==
- 1
- 2
- 3
- 4
- 5
- 6
前往页