// NetCtl.cpp : implementation file
//
#include "stdafx.h"
#include "DrawMapCtl.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMemUsgesCtl
CDrawMapCtl::CDrawMapCtl()
{
m_iRefreshInterval = 50;
m_ForeFontColor = RGB(255, 0, 0);
m_ForeColor = RGB(0, 150, 0);
m_BackColor = RGB(0, 0, 0);
m_FreeColor = RGB(255, 255, 0);
m_LableColor = RGB(255, 255, 0);
m_UsedColor = RGB(0, 140, 0);
memset(m_Rate,0,sizeof(WORD)*RATECOUNT);
m_RateCount = 0;
}
CDrawMapCtl::~CDrawMapCtl()
{
}
BEGIN_MESSAGE_MAP(CDrawMapCtl, CWnd)
//{{AFX_MSG_MAP(CMemUsgesCtl)
ON_WM_PAINT()
ON_WM_CREATE()
ON_WM_TIMER()
ON_WM_SIZE()
ON_WM_SHOWWINDOW()
ON_WM_ERASEBKGND()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMemUsgesCtl message handlers
void CDrawMapCtl::OnPaint()
{
//DrawMapOfTest();
DrawChart();
//CPaintDC dc(this);
}
int CDrawMapCtl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
return 0;
}
void CDrawMapCtl::SetRefreshInterval(int iMs)
{
m_iRefreshInterval = iMs;
SetTimer(100, m_iRefreshInterval, NULL);
}
void CDrawMapCtl::OnTimer(UINT nIDEvent)
{
Invalidate();
CWnd::OnTimer(nIDEvent);
}
void CDrawMapCtl::OnSize(UINT nType, int cx, int cy)
{
CWnd::OnSize(nType, cx, cy);
}
void CDrawMapCtl::OnShowWindow(BOOL bShow, UINT nStatus)
{
CWnd::OnShowWindow(bShow, nStatus);
}
void CDrawMapCtl::DrawMapOfTest()
{
CDC memDC;
CString strLable;
CBitmap memBitmap;
CBitmap* pOldBmp = NULL;
CString strUsges;
CPen BackFreePen;
CPen BackUsedPen;
CPen *pOldPen = NULL;
CPen *pOldMemPen = NULL;
CRect rectClient;
CPaintDC dc(this);
GetClientRect(&rectClient);
//draw cpu free usges
BackFreePen.CreatePen(PS_SOLID,1, m_FreeColor);
memDC.CreateCompatibleDC(&dc);
pOldMemPen = memDC.SelectObject(&BackFreePen);
memBitmap.CreateCompatibleBitmap(&dc, rectClient.right, rectClient.bottom);
pOldBmp = memDC.SelectObject(&memBitmap);
int m_iGridHeight = rectClient.Height() / 50;
for(int i = 0; i < 50 - (int)(30 / 2); i ++)
{
memDC.MoveTo(rectClient.left + 12, rectClient.top + i * m_iGridHeight + 12);
memDC.LineTo(rectClient.right - 12, rectClient.top + i * m_iGridHeight + 12);
}
//draw line
BackUsedPen.CreatePen(PS_SOLID, 1, m_UsedColor);
memDC.SelectObject(&BackUsedPen);
for(int j = 0; j < (int)(30 / 2); j ++) //fill used percent
{
memDC.MoveTo(rectClient.left + 12, rectClient.bottom - j * m_iGridHeight - 12);
memDC.LineTo(rectClient.right - 12, rectClient.bottom - j * m_iGridHeight - 12);
}
strLable = _T("Memory");
memDC.SetBkMode(TRANSPARENT);
memDC.SetTextAlign(TA_LEFT);
memDC.SetTextColor(m_LableColor);
memDC.TextOut(rectClient.left + 40, rectClient.top + 30, strLable);
strUsges.Format("%u", 30);
strUsges += "%";
memDC.SetTextAlign(TA_LEFT | TA_CENTER);
memDC.SetTextColor(m_ForeFontColor);
memDC.TextOut(rectClient.left + rectClient.Width() / 2 - 10, rectClient.bottom - 60, strUsges);
dc.BitBlt(rectClient.left, rectClient.top, rectClient.right, rectClient.bottom,
&memDC, 0, 0, SRCCOPY);
memDC.SelectObject(pOldBmp);
memDC.SelectObject(pOldMemPen);
memDC.DeleteDC();
memBitmap.DeleteObject();
}
BOOL CDrawMapCtl::OnEraseBkgnd(CDC* pDC)
{
return TRUE;//CWnd::OnEraseBkgnd(pDC);
}
BOOL CDrawMapCtl::Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID)
{
return CWnd::Create(NULL, NULL, dwStyle, rect, pParentWnd, nID, NULL);
}
void CDrawMapCtl::Release()
{
KillTimer(100);
}
void CDrawMapCtl::DrawChart()
{
CDC memDC;
CString strLable;
CBitmap memBitmap;
CBitmap* pOldBmp = NULL;
CString strUsges;
CPen BackFreePen;
CPen BackUsedPen;
CPen *pOldPen = NULL;
CPen *pOldMemPen = NULL;
CRect rectClient;
CPaintDC dc(this);
GetClientRect(&rectClient);
memDC.CreateCompatibleDC(&dc);
memBitmap.CreateCompatibleBitmap(&dc, rectClient.right, rectClient.bottom);
pOldBmp = memDC.SelectObject(&memBitmap);
BackFreePen.CreatePen(PS_SOLID,1, m_FreeColor);
pOldMemPen = memDC.SelectObject(&BackFreePen);
//***
Coordinate(memDC,rectClient);
//***
BackUsedPen.CreatePen(PS_SOLID,1, m_UsedColor);
memDC.SelectObject(&BackUsedPen);
DrawCurve(memDC,rectClient);
/*
int m_iGridHeight = rectClient.Height() / 50;
for(int i = 0; i < 50 - (int)(30 / 2); i ++)
{
memDC.MoveTo(rectClient.left + 12, rectClient.top + i * m_iGridHeight + 12);
memDC.LineTo(rectClient.right - 12, rectClient.top + i * m_iGridHeight + 12);
}
//draw line
BackUsedPen.CreatePen(PS_SOLID, 1, m_UsedColor);
memDC.SelectObject(&BackUsedPen);
for(int j = 0; j < (int)(30 / 2); j ++) //fill used percent
{
memDC.MoveTo(rectClient.left + 12, rectClient.bottom - j * m_iGridHeight - 12);
memDC.LineTo(rectClient.right - 12, rectClient.bottom - j * m_iGridHeight - 12);
}
strLable = _T("Memory");
memDC.SetBkMode(TRANSPARENT);
memDC.SetTextAlign(TA_LEFT);
memDC.SetTextColor(m_LableColor);
memDC.TextOut(rectClient.left + 40, rectClient.top + 30, strLable);
strUsges.Format("%u", 30);
strUsges += "%";
memDC.SetTextAlign(TA_LEFT | TA_CENTER);
memDC.SetTextColor(m_ForeFontColor);
memDC.TextOut(rectClient.left + rectClient.Width() / 2 - 10, rectClient.bottom - 60, strUsges);
*/
dc.BitBlt(rectClient.left, rectClient.top, rectClient.right, rectClient.bottom,
&memDC, 0, 0, SRCCOPY);
memDC.SelectObject(pOldBmp);
memDC.SelectObject(pOldMemPen);
memDC.DeleteDC();
memBitmap.DeleteObject();
}
void CDrawMapCtl::Coordinate(CDC &memDC,const CRect &rectClient)
{
int i;
//*****
int iMapLeft,iMapRight,iMapTop,iMapBottom,iDelta1=10,iDelta2=30;
iMapLeft=rectClient.left+iDelta2;
iMapTop=rectClient.top+iDelta1;
iMapRight=rectClient.right-iDelta1;
iMapBottom=rectClient.bottom-iDelta2;
memDC.MoveTo(iMapLeft,iMapTop);
memDC.LineTo(iMapLeft,iMapBottom); //纵坐标
memDC.LineTo(iMapRight,iMapBottom); //横坐标
//********
//纵坐标的箭头
memDC.MoveTo(iMapLeft,iMapTop);
memDC.LineTo(iMapLeft-5,iMapTop+10);
memDC.MoveTo(iMapLeft,iMapTop);
memDC.LineTo(iMapLeft+5,iMapTop+10);
//横坐标的箭头
memDC.MoveTo(iMapRight,iMapBottom);
memDC.LineTo(iMapRight-10,iMapBottom-5);
memDC.MoveTo(iMapRight,iMapBottom);
memDC.LineTo(iMapRight-10,iMapBottom+5);
//*********
int iSumDelta=0,iDelta;
//横坐标的分度格 iXParts个分度
int iXParts=30;
int iTempX;
iDelta=(iMapRight-iMapLeft)/iXParts;
for(i=1;i<iXParts;i++)
{
iSumDelta+=iDelta;
iTempX=iMapLeft+iSumDelta;
memDC.MoveTo(iTempX,iMapBottom);
if(i%2==0)
{
memDC.LineTo(iTempX,iMapBottom-5);
}
else
{
memDC.LineTo(iTempX,iMapBottom-3);
}
}
iSumDelta+=iDelta;
iTempX=iMapLeft+iSumDelta;
if( (iMapRight-iTempX)>5 )
{
memDC.MoveTo(iTempX,iMapBottom);
if(i%2==0)
{
memDC.LineTo(iTempX,iMapBottom-5);
}
else
{
memDC.LineTo(iTempX,iMapBottom-3);
}
}
//纵坐标的分度格 iYParts个分度
int iYParts=40;
int iTempY;
iSumDelta=0;
iDelta=(iMapBottom-iMapTop)/iYParts;
for(i=1;i<iYParts;i++)
{
iSumDelta+=iDelta;
iTempY=iMapBottom-iSumDelta;
memDC.MoveTo(iMapLeft,iTempY);
if(i%2==0)
{
memDC.LineTo(iMapLeft+5,iTempY);
}
else
{
memDC.LineTo(iMapLeft+3,iTempY);
}
}
iSumDelta+=iDelta;
iTempY=iMapBottom-iSumDelta;
if( (iTempY-iMapTop)>5)
{
memDC.MoveTo(iMapLeft,iTempY);
if(i%2==0)
{
memDC.LineTo(iMapLeft+5,iTempY);
}
else
{
memDC.LineTo(iMapLeft+3,iTempY);
}
}
//*****画中间线
iSumDelta=iDelta*iYParts/2;
iTempY=iMapBottom-iSumDelta;