C++圖形化動態顯示內存占用率的工具
#include "math.h"define Pi 3.1415927
void CMfcSdiSampleView::OnDraw(CDC pDC) { CMfcSdiSampleDoc pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return;
// TODO: add draw code for native data here CRect rect; GetClientRect(&rect); int radius=800; pDC->SetMapMode(MM_ISOTROPIC); pDC->SetWindowExt(radius+100,radius+100); pDC->SetViewportExt(rect.right/2,-rect.bottom/2); pDC->SetViewportOrg(rect.right/2,rect.bottom/2); //獲取內容狀態信息 MEMORYSTATUS mem; ::GlobalMemoryStatus(&mem); SIZE_T MemTotal=mem.dwTotalPhys; SIZE_T MemAvail=mem.dwAvailPhys; double dFree=(double)(Pi*2*MemAvail/MemTotal); double dUsed=(double)Pi*2-dFree; int x=-(int)(sin(dUsed)*radius); int y= (int)(cos(dUsed)*radius); CPen pen1,pen2,*oldpen; CBrush brush1,brush2,*oldbrush; //繪制已用 pen1.CreatePen(PS_SOLID,50,RGB(255,0,0)); brush1.CreateSolidBrush(RGB(255,0,0)); oldpen=pDC->SelectObject(&pen1); oldbrush=pDC->SelectObject(&brush1); pDC->Pie(-radius,radius,radius,-radius,0,radius,x,y); pDC->SelectObject(oldpen); pDC->SelectObject(oldbrush); //繪制未用 brush2.CreateSolidBrush(RGB(0,160,255)); pen2.CreatePen(PS_SOLID,50,RGB(0,160,255)); oldpen=pDC->SelectObject(&pen2); oldbrush=pDC->SelectObject(&brush2); pDC->Pie(-radius,radius,radius,-radius,x,y,0,radius); pDC->SelectObject(oldpen); pDC->SelectObject(oldbrush); pDC->SetBkMode(TRANSPARENT); CString str; str.Format(_T("Total: %dKB"),MemTotal/1024); //物理總內存 pDC->TextOut(300,300,str); str.Format(_T("Available: %dKB"),MemAvail/1024); //剩余內存 pDC->TextOut(300,200,str);
}
void CMfcSdiSampleView::OnTimer(UINT_PTR nIDEvent) { // TODO: Add your message handler code here and/or call default Invalidate(); CView::OnTimer(nIDEvent); }
int CMfcSdiSampleView::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CView::OnCreate(lpCreateStruct) == -1) return -1;
// TODO: Add your specialized creation code here SetTimer(1,1000,NULL); return 0;
}</pre>
本文由用戶 gd7g 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!