本文主要是介绍MFC自绘内存优化代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
// MemoryDlg.cpp : 实现文件//
#include "stdafx.h"
#include "MFCApplication1.h"
#include "MemoryDlg.h"
#include "afxdialogex.h"
#include <windows.h>
#include <TLHELP32.H>
#include <psapi.h>
#pragma comment(lib,"psapi.lib")
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// MemoryDlg 对话框
IMPLEMENT_DYNAMIC(MemoryDlg, CDialog)
MemoryDlg::MemoryDlg(CWnd* pParent /*=NULL*/)
: CDialog(MemoryDlg::IDD, pParent)
{
}
MemoryDlg::~MemoryDlg()
{
}
void MemoryDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(MemoryDlg, CDialog)
ON_COMMAND(ID_FREEMEM,&MemoryDlg::OnFreemem)
ON_COMMAND(ID_32783,&MemoryDlg::On32783)
//ON_BN_CLICKED(IDC_BUTTON1,&MemoryDlg::OnBnClickedButton1)
END_MESSAGE_MAP( )
// MemoryDlg 消息处理程序
BOOL MemoryDlg::OnInitDialog( )
{
CDialog::OnInitDialog( );
// TODO: 在此添加额外的初始化
//m_Menu.LoadMenu(IDR_MENU2); // IDR_MENU1为你加入的菜单的ID,在Resource视图的Menu文件夹下可以找到
//SetMenu(&m_Menu);
CMenu menu;
menu.LoadMenu(IDR_MENU2);
SetMenu(&menu);
menu.Detach( );
HANDLE hThread = CreateThread(NULL,0,OnThread,(LPVOID)this,0,NULL);
if(hThread) CloseHandle(hThread);
SetProcessWorkingSetSize(GetCurrentProcess( ),-1,-1);
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
DWORD WINAPI MemoryDlg::OnThread(LPVOID lpParameter)
{
//监视内存使用状态线程
MemoryDlg* pDlg = (MemoryDlg*)lpParameter;
while(TRUE)
{
CRect rc;
pDlg->GetClientRect(rc); //奔溃报错
CClientDC cDC(pDlg);
CDC dc;
CBitmap bmp;
dc.CreateCompatibleDC((CDC*)&cDC);
bmp.CreateCompatibleBitmap((CDC*)&cDC,rc.Width( ),rc.Height( ));
dc.SelectObject(&bmp);
CBrush* pBrush = CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH));
dc.FillRect(&rc,pBrush);
dc.SetBkMode(TRANSPARENT);
CString strOut;
MEMORYSTATUS mem;
mem.dwLength = sizeof(MEMORYSTATUS);
GlobalMemoryStatus(&mem);
LOGFONT lf;
dc.GetCurrentFont( )->GetLogFont(&lf);
lf.lfHeight = -14;
lf.lfWidth = 0;
StrCpyW(lf.lfFaceName,_T("微软雅黑"));
CFont font;
font.CreateFontIndirect(&lf);
CFont* pOldFont = dc.SelectObject(&font);
dc.SetBkMode(TRANSPARENT);
TEXTMETRIC tm;
dc.GetTextMetrics(&tm);
//画柱形图
CBrush brush;
CRect bkRc(10,10 + tm.tmHeight,110,210);
CRect frRc(11,11 + tm.tmHeight,109,209);
dc.Rectangle(&bkRc);
if(mem.dwMemoryLoad<30)
{
//绿色 RGB(130,193,14)
strOut = L"空闲";
dc.SetTextColor(RGB(130,193,14));
brush.CreateSolidBrush(RGB(130,193,14));
}
else if(mem.dwMemoryLoad<50)
{
//蓝色 RGB(59,169,243)
strOut = L"一般";
dc.SetTextColor(RGB(59,169,243));
brush.CreateSolidBrush(RGB(59,169,243));
}
else if(mem.dwMemoryLoad<70)
{
//黄色 RGB(237,171,0)
strOut = L"繁忙";
dc.SetTextColor(RGB(237,171,0));
brush.CreateSolidBrush(RGB(237,171,0));
}
else if(mem.dwMemoryLoad<80)
{
//橙色 RGB(234,109,12)
strOut = L"非常繁忙";
dc.SetTextColor(RGB(234,109,12));
brush.CreateSolidBrush(RGB(234,109,12));
}
else
{
//红色 RGB(213,10,4)
strOut = L"非常吃力";
dc.SetTextColor(RGB(213,10,4));
brush.CreateSolidBrush(RGB(213,10,4));
}
dc.TextOut(11,10,strOut);
dc.SetTextColor(RGB(0,0,0));
frRc.top = (long)(frRc.top + 209 - 209 * ((float)mem.dwMemoryLoad / 100));
dc.FillRect(&frRc,&brush);
strOut.Format(L"%d%%",mem.dwMemoryLoad);
dc.DrawText(strOut,&bkRc,DT_CENTER | DT_VCENTER | DT_SINGLELINE);
brush.DeleteObject( );
//内存使用率
strOut.Format(L"内存使用率: %d%%",mem.dwMemoryLoad);
dc.TextOut(140,10,strOut);
//物理内存
if(mem.dwTotalPhys >= 1073741824)
strOut.Format(L"物理内存大小: %.2fG",(float)mem.dwTotalPhys / 1073741824);
else if(mem.dwTotalPhys >= 1048576)
strOut.Format(L"物理内存大小: %.2fM",(float)mem.dwTotalPhys / 1048576);
else
strOut.Format(L"物理内存大小: %dKB",mem.dwTotalPhys / 1024);
dc.TextOut(140,10 + tm.tmHeight * 2,strOut);
strOut.Format(L"可用物理内存: %dKB",mem.dwAvailPhys / 1024);
dc.TextOut(140,10 + tm.tmHeight * 3,strOut);
//End 物理内存
//虚拟内存
if(mem.dwTotalVirtual >= 1073741824)
strOut.Format(L"虚拟内存大小: %.2fG",(float)mem.dwTotalVirtual / 1073741824);
else if(mem.dwTotalVirtual >= 1048576)
strOut.Format(L"虚拟内存大小: %.2fM",(float)mem.dwTotalVirtual / 1048576);
else
strOut.Format(L"虚拟内存大小: %dKB",mem.dwTotalVirtual / 1024);
dc.TextOut(140,10 + tm.tmHeight * 5,strOut);
strOut.Format(L"可用虚拟内存: %dKB",mem.dwAvailVirtual / 1024);
dc.TextOut(140,10 + tm.tmHeight * 6,strOut);
//End 虚拟内存
//页面文件
if(mem.dwTotalPageFile >= 1073741824)
strOut.Format(L"页面文件大小: %.2fG",(float)mem.dwTotalPageFile / 1073741824);
else if(mem.dwTotalPageFile >= 1048576)
strOut.Format(L"页面文件大小: %.2fM",(float)mem.dwTotalPageFile / 1048576);
else
strOut.Format(L"页面文件大小: %dKB",mem.dwTotalPageFile / 1024);
dc.TextOut(140,10 + tm.tmHeight * 8,strOut);
strOut.Format(L"可用页面大小: %dKB",mem.dwAvailPageFile / 1024);
dc.TextOut(140,10 + tm.tmHeight * 9,strOut);
//End 页面文件
dc.SelectObject(pOldFont);
font.DeleteObject( );
cDC.BitBlt(0,0,rc.Width( ),rc.Height( ),&dc,0,0,SRCCOPY);
dc.DeleteDC( );
bmp.DeleteObject( );
Sleep(1);
}
return 0;
}
DWORD WINAPI MemoryDlg::OnFreeMemThread(LPVOID lpParameter)
{
//整理内存线程
DWORD dwOld = 0;
DWORD dwNew = 0;
CString strInfo;
MEMORYSTATUS mem;
mem.dwLength = sizeof(MEMORYSTATUS);
GlobalMemoryStatus(&mem);
dwOld = mem.dwAvailPhys;
MemoryDlg* pDlg = (MemoryDlg*)lpParameter;
{
//提权
HANDLE hCurrentProcess;
HANDLE hProcessToken;
TOKEN_PRIVILEGES tp;
LUID luid;
hCurrentProcess = GetCurrentProcess( );
OpenProcessToken(hCurrentProcess,TOKEN_ALL_ACCESS,&hProcessToken);
LookupPrivilegeValue(NULL,TEXT("SeDebugPrivilege"),&luid);
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hProcessToken,FALSE,&tp,sizeof(TOKEN_PRIVILEGES),
(PTOKEN_PRIVILEGES)NULL,(PDWORD)NULL);
}
{
//整理内存
HANDLE SnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if(NULL == SnapShot) goto EndProc;
PROCESSENTRY32 ProcessInfo; //声明进程信息变量
ProcessInfo.dwSize = sizeof(PROCESSENTRY32);
//返回系统中第一个进程的信息
BOOL Status = Process32First(SnapShot,&ProcessInfo);
while(Status)
{
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS,TRUE,
ProcessInfo.th32ProcessID);
if(hProcess)
{
//内存整理
SetProcessWorkingSetSize(hProcess,-1,-1);
EmptyWorkingSet(hProcess);
CloseHandle(hProcess);
}
//获取下一个进程信息
Status = Process32Next(SnapShot,&ProcessInfo);
}
}
EndProc:
//让内存整理菜单恢复可用
CMenu* pMenu = pDlg->GetMenu( )->GetSubMenu(0);
pMenu->EnableMenuItem(ID_FREEMEM,MF_ENABLED | MF_BYCOMMAND);
//计算释放内存的总数
GlobalMemoryStatus(&mem);
dwNew = mem.dwAvailPhys - dwOld;
if(dwNew >= 1073741824)
strInfo.Format(L"释放了%.2fG内存。",(float)dwNew / 1073741824);
else if(dwNew >= 1048576)
strInfo.Format(L"释放了%.2fM内存。",(float)dwNew / 1048576);
else
strInfo.Format(L"释放了%dKB内存。",dwNew / 1024);
pDlg->SetWindowText(L"Memory - " + strInfo);
return 1;
}
void MemoryDlg::OnFreemem( )
{
// TODO: 在此添加命令处理程序代码
CMenu* pMenu = GetMenu( )->GetSubMenu(0);
pMenu->EnableMenuItem(ID_FREEMEM,MF_DISABLED | MF_GRAYED | MF_BYCOMMAND);
HANDLE hThread = CreateThread(NULL,0,OnFreeMemThread,(LPVOID)this,0,NULL);
if(hThread) CloseHandle(hThread);
}
//退出
void MemoryDlg::On32783( )
{
// TODO: 在此添加命令处理程序代码
//CDialog::OnOK( );
CDialog::OnCancel( );
}
这篇关于MFC自绘内存优化代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!