MonitorSpeedbase.cpp
资源名称:GGBT.rar [点击查看]
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:5k
源码类别:
P2P编程
开发平台:
Visual C++
- // MonitorSpeedbase.cpp: implementation of the CMonitorSpeedbase class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "testbt.h"
- #include "MonitorSpeedbase.h"
- #include "FileBase.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CMonitorSpeedbase::CMonitorSpeedbase()
- {
- m_pCategoryDownload = 0;
- m_lUpRate = 0, m_lDownRate = 0;
- m_lGridWidth = 1;
- m_lIndex = 0;
- memset(m_lValueUp, 0, eCount*sizeof(long));
- memset(m_lValueDown, 0, eCount*sizeof(long));
- }
- CMonitorSpeedbase::~CMonitorSpeedbase()
- {
- }
- void CMonitorSpeedbase::CreateEx(CCategoryDownload* pCategoryDownload, long lFontSize)
- {
- ASSERT(pCategoryDownload);
- m_pCategoryDownload = pCategoryDownload;
- m_font.CreateFont(-lFontSize, 0, 0, 0, 400, 0, 0, 0, 134, 0, 0, 0, 2, "宋体");
- }
- void CMonitorSpeedbase::OnTimer()
- {
- if (!m_pCategoryDownload)
- {
- assert(false);
- return;
- }
- long lDownRate = 0, lUpRate = 0;
- m_pCategoryDownload->GetRateTotals(lUpRate, lDownRate);
- if (lDownRate != m_lDownRate || lUpRate != m_lUpRate)
- {
- m_lDownRate = lDownRate ;
- m_lUpRate = lUpRate;
- }
- if (--m_lIndex < 0)
- {
- m_lIndex = eCount - 1;
- }
- // m_lUpRate = rand()%(200*1024);
- // m_lDownRate = rand()%(200*1024);
- m_lValueUp[m_lIndex] = m_lUpRate/1024;
- m_lValueDown[m_lIndex] = m_lDownRate/1024;
- }
- void CMonitorSpeedbase::OnPaint(CDC& dc, CRect rc, bool bShowText)
- {
- //
- // Get rect.
- //
- CRect rcText = rc;
- rcText.bottom = rcText .top + 10;
- CRect rcUp = rc;
- rcUp.top += 2;
- if (rc.Width() < 100)
- bShowText = false;
- if (bShowText)
- {
- rcUp.bottom -= 20;
- }
- // rcUp.right = rcUp.left + rcUp.Width()/2;
- CRect rcDown = rcUp;
- // rcUp.OffsetRect(rcUp.Width(), 0);
- CRect rcSpeed = rc;
- rcSpeed.top = rcSpeed.bottom - 20;
- CRect rcSpeedFrame = rcSpeed;
- rcSpeed.right = rcSpeed.left + rcSpeed.Width()/2;
- //
- // Get max value.
- //
- int iVisibleCount = rcUp.Width()/m_lGridWidth;
- long lMax = 5;
- long lRet = GetMaxValue(m_lValueUp, iVisibleCount);
- if (lRet > lMax ) lMax = lRet;
- lRet = GetMaxValue(m_lValueDown, iVisibleCount);
- if (lRet > lMax ) lMax = lRet;
- if (lMax %5)
- lMax = (lMax/5 + 1) * 5;
- assert (lMax > 0);
- //
- // ShowText
- //
- dc.SetBkMode(TRANSPARENT);
- CFont* pOldFont = dc.SelectObject(&m_font);
- // dc.SetROP2(
- CString strText;
- // strText.Format("D:%s/s, U:%s/s", FormatSize(m_lDownRate).data(), FormatSize(m_lUpRate).data());
- if (bShowText)
- {
- dc.SetTextColor(RGB(125, 172, 249));
- strText.Format("D:%s/s ", FormatSize(m_lDownRate).data());
- dc.DrawText(strText, rcSpeed, DT_LEFT|DT_VCENTER|DT_SINGLELINE);
- rcSpeed.OffsetRect(rcSpeed.Width(), 0);
- dc.SetTextColor(RGB(249, 152, 111));
- strText.Format("U:%s/s", FormatSize(m_lUpRate).data());
- dc.DrawText(strText, rcSpeed, DT_LEFT|DT_VCENTER|DT_SINGLELINE);
- }
- strText.Format("%dK/S", lMax);
- dc.SetTextColor(RGB(0, 0, 0));
- dc.DrawText(strText, rcText, DT_RIGHT|DT_VCENTER|DT_SINGLELINE);
- dc.SelectObject(pOldFont);
- //
- // draw graph.
- //
- DrawLine(m_lValueDown, dc, RGB(125, 172, 249), rcDown, lMax , iVisibleCount);
- DrawLine(m_lValueUp, dc, RGB(249, 152, 111), rcUp, lMax, iVisibleCount);
- //
- // draw splitter.
- //
- CPen curPen;
- curPen.CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
- CPen* pOldPen = dc.SelectObject(&curPen);
- if (bShowText)
- {
- dc.MoveTo(rcSpeedFrame.left, rcSpeedFrame.top);
- dc.LineTo(rcSpeedFrame.right, rcSpeedFrame.top);
- }
- dc.MoveTo(rcUp.right, rcUp.bottom);
- dc.LineTo(rcUp.right, rc.top);
- dc.SelectObject(pOldPen);
- }
- long CMonitorSpeedbase::GetMaxValue( long lValue[], long lCount)
- {
- long lMax = 5;
- long lDone = 0;
- for (int i=m_lIndex; i<eCount; i++)
- {
- if (lValue[i] > lMax)
- lMax = lValue[i];
- if (++lDone > lCount)
- return lMax;
- }
- for (i=0; i<m_lIndex; i++)
- {
- if (lValue[i] > lMax)
- lMax = lValue[i];
- if (++lDone > lCount)
- return lMax;
- }
- return lMax;
- }
- void CMonitorSpeedbase::DrawLine(long lValue[], CDC& dc, COLORREF cor, CRect& rc, long lMax, long iVisibleCount)
- {
- if (lMax <= 0)
- {
- assert(false);
- return;
- }
- long lDone = 0;
- long ileft = rc.left;
- int iHeight = rc.Height();
- float fUnit = (float)iHeight / lMax ;
- if (iHeight < 0)
- return;
- CPen curPen;
- curPen.CreatePen(PS_SOLID, 1, cor);
- CPen* pOldPen = dc.SelectObject(&curPen);
- dc.MoveTo(ileft, rc.bottom - (long)(lValue[m_lIndex] * fUnit));
- ileft += m_lGridWidth;
- for (int i=m_lIndex + 1; i<eCount; i++)
- {
- if (lDone++ > iVisibleCount)
- break;
- dc.LineTo(ileft, rc.bottom - (long)(lValue[i] * fUnit));
- dc.MoveTo(ileft, rc.bottom - (long)(lValue[i] * fUnit));
- ileft += m_lGridWidth;
- }
- for (i=0; i<m_lIndex; i++)
- {
- if (lDone++ > iVisibleCount)
- break;
- dc.LineTo(ileft, rc.bottom - (long)(lValue[i] * fUnit));
- dc.MoveTo(ileft, rc.bottom - (long)(lValue[i] * fUnit));
- ileft += m_lGridWidth;
- }
- dc.SelectObject(pOldPen);
- }