| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2799 人关注过本帖
标题:自己做的一个内存使用情况查看器(类似Windows任务管理器)
只看楼主 加入收藏
等待
Rank: 1
等 级:新手上路
帖 子:173
专家分:0
注 册:2005-12-1
结帖率:100%
收藏
 问题点数:0 回复次数:5 
自己做的一个内存使用情况查看器(类似Windows任务管理器)

因为第一次用API做,所以有的程序写的太乱;

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

HPEN pen[3];
HBRUSH hbrush;
POINT RecordCurvePoint[326];
bool RecotrCurveFlag = 0;
int RecotrCurveIndex = 325;
int cxChar=LOWORD (GetDialogBaseUnits ());
int cyChar=HIWORD (GetDialogBaseUnits ());
char *pch[12]=
{
"单位",
"当前内存使用情况",
"内存使用记录",
"兆字节(MB)",
"千字节(KB)",
"字节(Byte)",
"内存总数",
"Static",
"空闲内存",
"Static",
"页面文件",
"Static"
};

void defPen()
{
pen[0] = (HPEN)CreatePen(0,1,RGB(255,0,0));
pen[1] = (HPEN)CreatePen(0,1,RGB(10,200,50));
pen[2] = (HPEN)CreatePen(0,1,RGB(0,0,255));
hbrush = CreateSolidBrush(RGB(50,50,50));
}

LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);


int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // pointer to command line
int nCmdShow // show state of window
)
{
WNDCLASS WndCls;
WndCls.cbClsExtra = 0;
WndCls.cbWndExtra = 0;
WndCls.hbrBackground = (HBRUSH)GetStockObject(COLOR_BTNFACE + 1);//WHITE_BRUSH
WndCls.hCursor = LoadCursor(NULL,IDC_ARROW);
WndCls.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndCls.lpfnWndProc = WindowProc;
WndCls.hInstance = hInstance;
WndCls.lpszClassName = "Memory";
WndCls.lpszMenuName = NULL;
WndCls.style = CS_HREDRAW | CS_VREDRAW;
if(!RegisterClass(&WndCls))
{
::MessageBox(NULL, "注册窗口失败!", "提示框", MB_OK);
return 0;
}

HWND hwnd = CreateWindow("Memory",
"内存状态查看器",
WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & ~WS_OVERLAPPED & ~WS_THICKFRAME,//禁止最大化
CW_USEDEFAULT,
CW_USEDEFAULT,
350,
430,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, SW_SHOWNORMAL);
UpdateWindow(hwnd);

MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return 0;
}

LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
RECT WndRect;
POINT point[2];
HDC hdc;
HPEN hOldPen;
MEMORYSTATUS MemInfo;
unsigned long mem[3];
char ch[30];
char ch_kk[6] = {"(MB)"};
static int flag = 1;
static HWND hBtnWnd[13];
static int m_offset = 1;//竖直网格线的偏移量
int i;
int BtnHeight = 13 * cyChar / 4, BtnWidth = 0, BtnDistance = 10;
switch(uMsg)
{
case WM_CREATE:
SetTimer(hwnd, 1, 500, NULL);//设置定时器
defPen();//定义画笔
//创建按纽
for(i = 1; i < 4; i++)//创建3个组框
{
hBtnWnd[i-1] = CreateWindow(TEXT("button"), "单位",WS_CHILD | WS_VISIBLE | BS_GROUPBOX ,
cxChar-4, BtnDistance,
42 * cxChar, BtnHeight,
hwnd, (HMENU) (i-1),
((LPCREATESTRUCT) lParam)->hInstance, NULL) ;//BS_AUTORADIOBUTTON
BtnDistance=BtnDistance + BtnHeight + 10;
BtnHeight = (13 * cyChar) * 2*i / 4;
}
//创建三个单选按钮
BtnDistance = cxChar;
BtnWidth = 0;
for(i = 0; i < 3; i++)
{
hBtnWnd[3+i] = CreateWindow(TEXT("button"), "兆字节(MB)",WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON | WS_BORDER,
BtnDistance, cyChar+16,
13 * cxChar, 5 * cyChar / 4,
hwnd, (HMENU) (3+i),
((LPCREATESTRUCT) lParam)->hInstance, NULL) ;//BS_AUTORADIOBUTTON
BtnWidth = 12 * cxChar;
BtnDistance=BtnDistance + BtnWidth + 15;
}
//创建6个静态文本框
GetWindowRect(hBtnWnd[1],&WndRect);
point[0].x = WndRect.left;
point[0].y = WndRect.top;
ScreenToClient(hwnd, &point[0]);
BtnHeight = 5 * cyChar / 4;
BtnDistance = 20;
for(i = 0; i < 3; i++)
{
hBtnWnd[6+i*2] = CreateWindow(TEXT("static"), "物理内存总数",WS_CHILD | WS_VISIBLE | WS_BORDER | SS_CENTER,
point[0].x+5,point[0].y+BtnDistance,
13 * cxChar, 5 * cyChar / 4,
hwnd, (HMENU)(6+i*2),
((LPCREATESTRUCT) lParam)->hInstance, NULL) ;
hBtnWnd[6+i*2+1] = CreateWindow(TEXT("static"), "256",WS_CHILD | WS_VISIBLE | WS_BORDER | SS_RIGHT,
point[0].x+115,point[0].y+BtnDistance,
20 * cxChar+55, 5 * cyChar / 4,
hwnd, (HMENU)(6+i*2+1),
((LPCREATESTRUCT) lParam)->hInstance, NULL) ;
BtnDistance = BtnDistance + BtnHeight + 6;
}
//创建内存使用记录绘图框
GetWindowRect(hBtnWnd[2],&WndRect);
point[0].x = WndRect.left;
point[0].y = WndRect.top;
ScreenToClient(hwnd, &point[0]);
hBtnWnd[12] = CreateWindow(TEXT("static"), "",WS_CHILD | WS_VISIBLE | WS_BORDER,
point[0].x+5,point[0].y+20,
325, 180,
hwnd, (HMENU)(12),
((LPCREATESTRUCT) lParam)->hInstance, NULL) ;
//初始化子窗口文本及
for(i = 0; i < 12; i++)
{
SetWindowText(hBtnWnd[i], pch[i]);
}
SendMessage(hBtnWnd[3], BM_SETCHECK, 1, 0) ;//让"MB"初始选中
//设置静态文本框信息
GlobalMemoryStatus(&MemInfo);//获取内存信息
switch(flag)
{
case 1:
mem[0] = MemInfo.dwTotalPhys/(1024*1024);
mem[1] = MemInfo.dwAvailPhys/(1024*1024);
mem[2] = MemInfo.dwTotalPageFile/(1024*1024);
strcpy(ch_kk, "(MB)");
break;
case 2:
mem[0] = MemInfo.dwTotalPhys/1024;
mem[1] = MemInfo.dwAvailPhys/1024;
mem[2] = MemInfo.dwTotalPageFile/1024;
strcpy(ch_kk, "(KB)");
break;
case 3:
mem[0] = MemInfo.dwTotalPhys;
mem[1] = MemInfo.dwAvailPhys;
mem[2] = MemInfo.dwTotalPageFile;
strcpy(ch_kk, "(Byte)");
break;
}
for(i = 0; i < 3; i++)
{
sprintf(ch, "%d%s", mem[i], ch_kk);
SetWindowText(hBtnWnd[7+2*i], ch);
}
//初始化内存使用曲线的坐标点
for(i = 0; i < 325; i++)
{
RecordCurvePoint[i].x = i;
RecordCurvePoint[i].y = 0;
}
RecordCurvePoint[i].x = i;
if(flag == 3)
{
mem[1] /= 1024;
mem[0] /= 1024;
}
RecordCurvePoint[i].y = 180*mem[1]/mem[0];
sprintf(ch, "%s%d", "当前内存使用情况%",100-100*mem[1]/mem[0]);
SetWindowText(hBtnWnd[2], ch);
break;

case WM_TIMER:
m_offset += 1;
GlobalMemoryStatus(&MemInfo);//获取内存信息
switch(flag)
{
case 1:
mem[0] = MemInfo.dwTotalPhys/(1024*1024);
mem[1] = MemInfo.dwAvailPhys/(1024*1024);
mem[2] = MemInfo.dwTotalPageFile/(1024*1024);
strcpy(ch_kk, "(MB)");
break;
case 2:
mem[0] = MemInfo.dwTotalPhys/1024;
mem[1] = MemInfo.dwAvailPhys/1024;
mem[2] = MemInfo.dwTotalPageFile/1024;
strcpy(ch_kk, "(KB)");
break;
case 3:
mem[0] = MemInfo.dwTotalPhys;
mem[1] = MemInfo.dwAvailPhys;
mem[2] = MemInfo.dwTotalPageFile;
strcpy(ch_kk, "(Byte)");
break;
}
for(i = 0; i < 3; i++)
{
sprintf(ch, "%d%s", mem[i], ch_kk);
SetWindowText(hBtnWnd[7+2*i], ch);
}

hdc=GetDC(hwnd);
//设置矩形背景
GetWindowRect(hBtnWnd[12],&WndRect);
point[0].x = WndRect.left;
point[0].y = WndRect.top;
point[1].x = WndRect.right;
point[1].y = WndRect.bottom;
ScreenToClient(hwnd, &point[0]);
ScreenToClient(hwnd, &point[1]);
WndRect.left = point[0].x;
WndRect.top = point[0].y;
WndRect.right = point[1].x;
WndRect.bottom = point[1].y;
FillRect(hdc, &WndRect, hbrush);

//画网格线
hOldPen = (HPEN)SelectObject(hdc, pen[1]);
GetWindowRect(hBtnWnd[12],&WndRect);
point[0].x = WndRect.left;
point[0].y = WndRect.top;
point[1].x = WndRect.right;
point[1].y = WndRect.bottom;
ScreenToClient(hwnd, &point[0]);
ScreenToClient(hwnd, &point[1]);
for(i = 0; i < 10; i++)
{
MoveToEx(hdc, point[0].x, point[0].y +(WndRect.bottom - WndRect.top)*i/10, NULL);
LineTo(hdc, point[1].x, point[0].y + (WndRect.bottom - WndRect.top)*i/10);
}
if(m_offset >= 18)//两条网格线的间距为18
{
m_offset = m_offset % 18;
}
for(i = 1; i <= (WndRect.right-WndRect.left)*10/(WndRect.bottom - WndRect.top); i++)
{
MoveToEx(hdc, point[0].x + (WndRect.bottom - WndRect.top)*i/10 - m_offset, point[0].y ,NULL);
LineTo(hdc, point[0].x + (WndRect.bottom - WndRect.top)*i/10 - m_offset, point[1].y);
}
SelectObject(hdc, hOldPen);

//画记录曲线
RecotrCurveIndex--;
if(RecotrCurveIndex < 1)
{
RecotrCurveFlag = 1;
}
for(i = 0; i < 325; i++)
{
RecordCurvePoint[i].y = RecordCurvePoint[i+1].y;
}
if(flag == 3)
{
mem[1] /= 1024;
mem[0] /= 1024;
}
RecordCurvePoint[i].y = 180*mem[1]/mem[0];
sprintf(ch, "%s%d", "当前内存使用情况%",100-100*mem[1]/mem[0]);
SetWindowText(hBtnWnd[2], ch);
hOldPen = (HPEN)SelectObject(hdc, pen[0]);
if(!RecotrCurveFlag)
{
for(i = RecotrCurveIndex; i < 325; i++)
{
point[0].x = WndRect.left+RecordCurvePoint[i].x-1;
point[0].y = WndRect.top+RecordCurvePoint[i].y+1;
point[1].x = WndRect.left+RecordCurvePoint[i+1].x-1;
point[1].y = WndRect.top+RecordCurvePoint[i+1].y+1;
ScreenToClient(hwnd, &point[0]);
ScreenToClient(hwnd, &point[1]);
SetPixel(hdc, point[0].x, point[0].y, RGB(255,0,0));
MoveToEx(hdc, point[0].x, point[0].y, NULL);
LineTo(hdc, point[1].x, point[1].y);
}
}
else
{
for(i = 1; i < 325; i++)
{
point[0].x = WndRect.left+RecordCurvePoint[i].x-1;
point[0].y = WndRect.top+RecordCurvePoint[i].y+1;
point[1].x = WndRect.left+RecordCurvePoint[i+1].x-1;
point[1].y = WndRect.top+RecordCurvePoint[i+1].y+1;
ScreenToClient(hwnd, &point[0]);
ScreenToClient(hwnd, &point[1]);
SetPixel(hdc, point[0].x, point[0].y, RGB(255,0,0));
MoveToEx(hdc, point[0].x, point[0].y, NULL);
LineTo(hdc, point[1].x, point[1].y);
}
}
SelectObject(hdc, hOldPen);
ReleaseDC(hwnd,hdc);
break;

case WM_PAINT:
PAINTSTRUCT ps;
hdc=BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
break;

case WM_CTLCOLORBTN:
hdc = GetDC(hwnd);
SetBkColor(hdc, RGB(255,0,255));
ReleaseDC(hwnd,hdc);
break ;

case WM_COMMAND:
if(HIWORD(wParam) == BN_CLICKED)
{
flag = LOWORD(wParam) - 2;
//立即更新静态文本框内容
GlobalMemoryStatus(&MemInfo);//获取内存信息
switch(flag)
{
case 1:
mem[0] = MemInfo.dwTotalPhys/(1024*1024);
mem[1] = MemInfo.dwAvailPhys/(1024*1024);
mem[2] = MemInfo.dwTotalPageFile/(1024*1024);
strcpy(ch_kk, "(MB)");
break;
case 2:
mem[0] = MemInfo.dwTotalPhys/1024;
mem[1] = MemInfo.dwAvailPhys/1024;
mem[2] = MemInfo.dwTotalPageFile/1024;
strcpy(ch_kk, "(KB)");
break;
case 3:
mem[0] = MemInfo.dwTotalPhys;
mem[1] = MemInfo.dwAvailPhys;
mem[2] = MemInfo.dwTotalPageFile;
strcpy(ch_kk, "(Byte)");
break;
}
for(i = 0; i < 3; i++)
{
sprintf(ch, "%d%s", mem[i], ch_kk);
SetWindowText(hBtnWnd[7+2*i], ch);
}
}
break;

case WM_CLOSE:
DestroyWindow(hwnd);
break;

case WM_DESTROY:
PostQuitMessage(0);
break;

default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}

rm0Mx8H8.rar (33.32 KB) 自己做的一个内存使用情况查看器(类似Windows任务管理器)


[此贴子已经被作者于2007-10-1 13:35:05编辑过]

搜索更多相关主题的帖子: 内存 管理器 Windows 任务 
2007-10-01 13:21
等待
Rank: 1
等 级:新手上路
帖 子:173
专家分:0
注 册:2005-12-1
收藏
得分:0 

请大家提出宝贵的意见,小弟先谢谢了.
为什么在WM_CREATE消息中不能进行绘图操作呢?


2007-10-01 13:26
余来
Rank: 6Rank: 6
等 级:贵宾
威 望:26
帖 子:956
专家分:18
注 册:2006-8-13
收藏
得分:0 

不错,继续加油
WM_CREATE是创建窗口消息,因此,窗口还未创建,因此不能做图(不能在当前程序中的窗口)

楼住再稍微深入点,作个内存修改器,这样就可以完成游戏修改器了,作弊不错。


2007-10-01 13:54
等待
Rank: 1
等 级:新手上路
帖 子:173
专家分:0
注 册:2005-12-1
收藏
得分:0 
这要用汇编吧?可我没学过

2007-10-01 14:37
快速回复:自己做的一个内存使用情况查看器(类似Windows任务管理器)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026803 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved