#include "windows.h"
#include "math.h"
#include "winbase.h"
#include <tlhelp32.h>LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);MEMORYSTATUS mem;
char data[128]="imposible is nothing";
SYSTEMTIME time;int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
char szClassName[] = "memory";
WNDCLASSEX wndclass;
wndclass.cbSize    = sizeof(wndclass);
wndclass.style    = CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc   = MainWndProc;
wndclass.cbClsExtra    = 0;
wndclass.cbWndExtra    = 0;
wndclass.hInstance     = hInstance;
wndclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);
wndclass.lpszMenuName  = NULL;
wndclass.lpszClassName = szClassName ;
wndclass.hIconSm       = NULL;
RegisterClassEx(&wndclass); 
HWND hwnd = ::CreateWindowEx( 
0,
szClassName,
"内存使用情况查看工具",
WS_CAPTION|WS_SYSMENU,
100,
100,
300,
180,
NULL,
NULL,
hInstance,
NULL); if(hwnd == NULL)
{
return -1;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}void paint_1(HDC hdc, RECT rt)
{
HPEN pen;
pen = CreatePen(PS_SOLID, 1, RGB(99,99,99));
HPEN oldpen;
oldpen = (HPEN)SelectObject(hdc, pen);
RoundRect(hdc , rt.left + 20, rt.top + 12, rt.right - 20, rt.bottom - 12, 5, 5);
SelectObject(hdc, oldpen);
DeleteObject(pen);
}void paint_2(HDC hdc, int i, DWORD th, WORD h, WORD m, WORD s, WORD y, WORD mon, WORD d)
{
HFONT font = CreateFont(12 , 0, 0, 0, NULL, 0, 0, 0,ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, VARIABLE_PITCH|FF_SWISS, "MS Sans Serif");
HFONT oldfont = (HFONT)SelectObject(hdc, font); wsprintf(data, "物理内存总量         %5d MB", mem.dwTotalPhys/(1024*1024));
TextOut(hdc, 50, 20, data, strlen(data));
wsprintf(data, "可使用物理内存     %5d MB", (mem.dwAvailPhys/(1024*1024)));
TextOut(hdc, 50, 38, data, strlen(data));
wsprintf(data, "已使用物理内存     %5d MB", ((mem.dwTotalPhys - mem.dwAvailPhys)/(1024*1024)));
TextOut(hdc, 50, 56, data, strlen(data));
wsprintf(data, "内存使用率 %2d %%", mem.dwMemoryLoad);
TextOut(hdc, 50, 80, data, strlen(data));
wsprintf(data, "进程总数 %4d", i);
TextOut(hdc, 150, 80, data, strlen(data));
wsprintf(data, "线程总数 %6d", th);
TextOut(hdc, 50, 98, data, strlen(data));
wsprintf(data, "SYSTEMTIME %4dy %2dm %2dd  %2d:%2d:%2d ", y, mon, d, h, m, s);
TextOut(hdc, 50, 118, data, strlen(data));
SelectObject(hdc, oldfont);
DeleteObject(font);
}LRESULT CALLBACK MainWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int i(0), s(0), n(0), l(0);
DWORD th(0);
PROCESSENTRY32 p;
p.dwSize = sizeof(p);
HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
bool b;
switch (message)
{
case WM_CREATE:
{
b = Process32First(h, &p);
for(i=0;b;i++)
{
b = Process32Next(h, &p);
th = th + (int)p.cntThreads;
}
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
GlobalMemoryStatus(&mem);
SetTimer(hwnd, 101, 300, NULL);
// SetBkMode(hdc , TRANSPAENT);
return 0;
}
case WM_PAINT:
{
HDC hdc;
PAINTSTRUCT ps;
hdc = BeginPaint(hwnd, &ps);
RECT rt;
GetLocalTime(&time);
GetClientRect(hwnd, &rt);
paint_1(hdc, rt);
paint_2(hdc, i, th, time.wHour, time.wMinute, time.wSecond, time.wYear, time.wMonth, time.wDay);
EndPaint(hwnd, &ps);
return 0;
}
case WM_TIMER:
{
b = Process32First(h, &p);
GetLocalTime(&time);
for(i=0;b;i++)
{
b = Process32Next(h, &p);
th = th + (int)p.cntThreads;
}
HDC hdc = GetDC(hwnd);
GlobalMemoryStatus(&mem);
paint_2(hdc, i, th, time.wHour, time.wMinute, time.wSecond, time.wYear, time.wMonth, time.wDay);
ReleaseDC(hwnd ,hdc);
return 0;
}
case WM_DESTROY:
PostQuitMessage(0) ;
return 0 ;
}
CloseHandle(h);
return DefWindowProc(hwnd, message, wParam, lParam);
}