版权声明:本文为YuanChuang文章,未经博主允许转载。 https://blog.csdn.net/zzy1448331580/article/details/86618494
如图所示
全部代码【就个外表,没实现功能】
#include <windows.h>
#include <windowsx.h>
#include <process.h>
#include <TlHelp32.h>
#include "resource.h"
#pragma comment(lib,"msimg32.lib")
#pragma warning(disable:4293)
#define DefaultWindowBitmapPath "C:\\Users\\Administrator\\Desktop\\123.bmp"
#define DefaultForbidColor RGB(255,255,255)
//全局变量
HINSTANCE g_Hinstance = NULL;
HBITMAP g_Bitmap=NULL;
HWND g_Window = NULL;
HANDLE g_DrawThread = NULL;
unsigned int g_DrawThreadID = NULL;
//负责收尾的函数
void ClearWorker()
{
::TerminateThread(g_DrawThread, NULL);
::DeleteObject(g_Bitmap);
::DeleteObject(g_Window);
::CloseHandle(g_DrawThread);
}
//获取当前进程数量
int GetCurrentProcessNum()
{
int nCureentProcessNum = 0;
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (hSnap == NULL)
return nCureentProcessNum;
PROCESSENTRY32 ProcessInfo = {0};
ProcessInfo.dwSize = sizeof(PROCESSENTRY32);
BOOL bOk = Process32First(hSnap, &ProcessInfo);
while (bOk)
{
nCureentProcessNum++;
bOk = Process32Next(hSnap,&ProcessInfo);
}
CloseHandle(hSnap);
return nCureentProcessNum;
}
//CPU使用率辅助函数
int DiffentTime(FILETIME time1, FILETIME time2)
{
int a = time1.dwHighDateTime << 32 | time1.dwLowDateTime;
int b = time2.dwHighDateTime << 32 | time2.dwLowDateTime;
return(b - a);
}
//获取CPU当前使用率
int GetCpuUse()
{
FILETIME idleTime1 = { 0 }, idleTime2 = {0};
FILETIME kernelTime1 = {0}, kernelTime2 = { 0 };
FILETIME userTime1 = {0}, userTime2 = { 0 };
::GetSystemTimes(&idleTime1, &kernelTime1, &userTime1);
Sleep(1000);
::GetSystemTimes(&idleTime2, &kernelTime2, &userTime2);
int idle = (int)DiffentTime(idleTime1, idleTime2);
int kermel = (int)DiffentTime(kernelTime1, kernelTime2);
int user = (int)DiffentTime(userTime1, userTime2);
if (kermel + user == NULL)
return 0;
else
return abs((kermel + user - idle) * 100 / (kermel + user));
}
//显示当前内存情况
void ShowMemoryState(HDC& dc)
{
MEMORYSTATUSEX sysMemory = { 0 };
sysMemory.dwLength = sizeof(MEMORYSTATUSEX);
if (!GlobalMemoryStatusEx(&sysMemory))return;
HPEN hPen = ::CreatePen(PS_SOLID, 6, RGB(0, 255, 0));
if (hPen == NULL)return;
(HPEN)SelectObject(dc, hPen);
int PageSize = 1024;
char szBuf[100] = { 0 };
wsprintfA(szBuf, "叫我小优化者");
TextOutA(dc, 100, 0, szBuf, strlen(szBuf));
wsprintfA(szBuf, "内存使用:%d%%", sysMemory.dwMemoryLoad );
TextOutA(dc, 100, 20, szBuf, strlen(szBuf));
wsprintfA(szBuf, "可用内存:%dGB", sysMemory.ullAvailPhys/PageSize/PageSize/PageSize);
TextOutA(dc,100,40,szBuf,strlen(szBuf));
wsprintfA(szBuf, "CPU使用:%d%", GetCpuUse());
TextOutA(dc, 100, 60, szBuf, strlen(szBuf));
wsprintfA(szBuf, "当前进程:%d个", GetCurrentProcessNum());
TextOutA(dc, 100, 80, szBuf, strlen(szBuf));
::DeletePen(hPen);
}
unsigned int _stdcall WindowDrawThread(PVOID)
{
while (TRUE)
{
HDC hdc = ::GetWindowDC(g_Window);
if (hdc == NULL)return 0;
RECT Rect = { 0 };
if (!::GetWindowRect(g_Window, &Rect))return 0;
HDC dcMem = ::CreateCompatibleDC(hdc);
if (dcMem == NULL)return 0;
::SelectObject(dcMem, g_Bitmap);
ShowMemoryState(dcMem);
::BitBlt(hdc, 0, 0, Rect.right - Rect.left, Rect.bottom - Rect.top,
dcMem, NULL, NULL, SRCCOPY);
::DeleteDC(dcMem);
::DeleteDC(hdc);
}
return 0;
}
LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc=NULL;
PAINTSTRUCT ps = { 0 };
switch (message)
{
case WM_CREATE:
::SetLayeredWindowAttributes(hWnd, DefaultForbidColor, NULL, LWA_COLORKEY);
g_DrawThread = (HANDLE)::_beginthreadex(NULL, NULL, WindowDrawThread, NULL, NULL, &g_DrawThreadID);
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case ID_KillMemory:
break;
case ID_KillProcess:
break;
case ID_KillNullFile:
break;
case ID_Exit:
::PostMessage(hWnd, WM_CLOSE, 0, 0);
break;
}
break;
case WM_CONTEXTMENU:
{
HMENU hMenu = LoadMenuA(g_Hinstance, MAKEINTRESOURCE(IDR_MENU1));
if (hMenu)
{
HMENU hPop = GetSubMenu(hMenu, 0);
int PX = GET_X_LPARAM(lParam);
int PY = GET_Y_LPARAM(lParam);
TrackPopupMenu(hPop, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTALIGN,
PX, PY, NULL, (HWND)wParam,NULL);
DestroyMenu(hMenu);
DestroyMenu(hPop);
}
}
break;
case WM_PAINT:
hdc = ::BeginPaint(hWnd, &ps);
::EndPaint(hWnd, &ps);
break;
case WM_LBUTTONDOWN:
::PostMessage(hWnd, WM_SYSCOMMAND, SC_MOVE | HTCAPTION, NULL);
return TRUE;
case WM_CLOSE:
ClearWorker();
::PostQuitMessage(0);
break;
default:
return ::DefWindowProcA(hWnd,message,wParam,lParam);
}
return 0;
}
int APIENTRY WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE , _In_ LPSTR , _In_ int nShowCmd)
{
LPCSTR pszWinCls = "PrcessKiller";
g_Hinstance = hInstance;
WNDCLASSEX wnd = { 0 };
wnd.cbSize = sizeof(WNDCLASSEX);
wnd.hbrBackground = (HBRUSH)::GetStockObject(NULL_BRUSH);
wnd.hInstance = hInstance;
wnd.hCursor = ::LoadCursor(NULL, IDC_ARROW);
wnd.lpfnWndProc = WinProc;
wnd.lpszClassName = pszWinCls;
wnd.style = CS_HREDRAW | CS_VREDRAW;
if (::RegisterClassExA(&wnd) == NULL)return -1;
g_Bitmap = (HBITMAP)::LoadImageA(NULL, DefaultWindowBitmapPath, IMAGE_BITMAP,
NULL, NULL, LR_LOADFROMFILE | LR_DEFAULTSIZE);
if (g_Bitmap == NULL)return -1;
BITMAP BitmapInfo = { 0 };
::GetObject(g_Bitmap,sizeof(BITMAP),&BitmapInfo);
g_Window = ::CreateWindowExA(WS_EX_TOPMOST|WS_EX_LAYERED,pszWinCls,pszWinCls,WS_POPUP,
300,300,BitmapInfo.bmWidth,BitmapInfo.bmHeight,NULL,NULL,hInstance,NULL);
if (g_Window == NULL)return -1;
::ShowWindow(g_Window, nShowCmd);
::UpdateWindow(g_Window);
MSG msg = { 0 };
while (::GetMessage(&msg, NULL, NULL, NULL))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}