void CCpuUsageDlg::OnTcomr(UINT nIDEvent)
{
// TODO: Add yourcomssage handler code here and/or call default
int per;
// 得到当前CPU 的使用率
per = m_Usage.GetCpuUsage();
// 计数
if(tcoms < MAXTIMES)
tcoms ++;
// 更新CPU 使用记录数组
for(int i=0; i
{
m_UsageHistory[i] = m_UsageHistory[i+1];
}
// 将最后一个记录设为当前使用率
m_UsageHistory[i] = per;
// 格式化字符串
m_strCpuUsage.Format("CPU 使用 %3d%%", per);
// 更新显示
UpdateData(FALSE);
Invalidate();
CDialog::OnTcomr(nIDEvent);
}
(3)实现绘制示意图
为CCpuUsageDlg 添加成员函数DrawDiagram,绘制CPU 使用率示意图,代码如下:
void CCpuUsageDlg::DrawDiagram(CPaintDC *pDC)
{
CRect rect;
int W, H, x, y;
float xinterval, yinterval;
CPen curpen, *oldpen;
CBrush newbrush, *oldbrush;
枫叶文学网www.fywxw.com
Visual C++ 6.0 程序设计从入门到精通
·170·
// 得到绘制区域的矩形
m_btnView.GetWindowRect(rect);
ScreenToClient(rect); //
rect.top += 15;
// 得到矩形的长和宽
W = rect.Width();
H = rect.Height();
xinterval = (float)W/(MAXTIMES-1);
yinterval = (float)H/100;
// 创建黑色画刷, 绘制背景
newbrush.CreateSolidBrush(RGB(0,0,0));
oldbrush = pDC->SelectObject(&newbrush);
pDC->Rectangle(rect);
newbrush.DeleteObject();
pDC->SelectObject(oldbrush);
// 绿色画笔, 绘制曲线
curpen.CreatePen(PS_SOLID