Advertisement
Old_But_Gold

Untitled

Oct 24th, 2024
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 43.00 KB | None | 0 0
  1. #ifndef UNICODE
  2. #define UNICODE
  3. #endif
  4.  
  5. #pragma comment(lib, "Comctl32.lib")
  6. #pragma comment(lib, "Psapi.lib")
  7.  
  8. #define MainName L"Task Manager"
  9. #define SCREEN_WIDTH GetSystemMetrics(SM_CXSCREEN)
  10. #define SCREEN_HEIGHT GetSystemMetrics(SM_CYSCREEN)
  11.  
  12. #define ID_MENU_RUN_TASK 0x2010
  13. #define ID_MENU_EXIT 0x2011
  14. #define ID_MENU_ABOUT 0x2012
  15. #define ID_MENU_RARE 0x3010
  16. #define ID_MENU_NORMAL 0x3011
  17. #define ID_MENU_OFTEN 0x3012
  18. #define ID_MENU_NONE 0x3013
  19. #define ID_MENU_UPDATE 0x4010
  20.  
  21. #define IDC_TABCONTROL 0x5010
  22. #define IDC_LISTVIEW_PROCESSES 0x6010
  23. #define IDC_PROGRESSBAR_CPU 0x7010
  24. #define IDC_PROGRESSBAR_RAM 0x8010
  25.  
  26.  
  27. #define IDM_CLOSE_PROCESS 0x5011
  28. #define IDM_OPEN_LOCATION 0x5012
  29.  
  30. #define TIMER_PROCESSES 0x1
  31. #define TIMER_GRAPH 0x2
  32.  
  33. #include <Windows.h>
  34. #include <iomanip>
  35. #include <CommCtrl.h>
  36. #include <wingdi.h>
  37. #include <string>
  38. #include "resource.h"
  39. #include <TlHelp32.h> // For process enumeration
  40. #include <Psapi.h> // For process memory information
  41. #include <shellapi.h>
  42. #include <map>
  43. #include <sstream>
  44. #include <set>
  45. #include <fstream>
  46. #include <vector>
  47. #pragma comment(lib, "gdiplus.lib")
  48.  
  49.  
  50. #define ProcessListViewX 0
  51. #define ProcessListViewY 25
  52.  
  53. #define TabControlX 10
  54. #define TabControlY 10
  55.  
  56. HMENU CreateAppMenu() {
  57. HMENU hMenu = CreateMenu();
  58. HMENU hFileMenu = CreatePopupMenu();
  59. HMENU hRefreshMenu = CreatePopupMenu();
  60.  
  61. AppendMenu(hFileMenu, MF_STRING, ID_MENU_RUN_TASK, L"Запустить новую задачу");
  62. AppendMenu(hFileMenu, MF_SEPARATOR, 0, NULL);
  63. AppendMenu(hFileMenu, MF_STRING, ID_MENU_EXIT, L"Выход");
  64.  
  65. AppendMenu(hRefreshMenu, MF_STRING, ID_MENU_RARE, L"Редко (5 сек)");
  66. AppendMenu(hRefreshMenu, MF_STRING, ID_MENU_NORMAL, L"Нормально (3 сек)");
  67. AppendMenu(hRefreshMenu, MF_STRING, ID_MENU_OFTEN, L"Часто (1 сек)");
  68. AppendMenu(hRefreshMenu, MF_STRING, ID_MENU_NONE, L"Отключить");
  69.  
  70. AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hFileMenu, L"Файл");
  71. AppendMenu(hMenu, MF_STRING, ID_MENU_ABOUT, L"О разработчике");
  72. AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hRefreshMenu, L"Частота обновления");
  73. AppendMenu(hMenu, MF_STRING, ID_MENU_UPDATE, L"Обновить сейчас");
  74.  
  75. return hMenu;
  76. }
  77.  
  78. HIMAGELIST CreateTabImageList() {
  79. HIMAGELIST hImageList = ImageList_Create(16, 16, ILC_COLOR32 | ILC_MASK, 1, 1);
  80. ImageList_SetBkColor(hImageList, CLR_NONE);
  81. HICON hIcon1 = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_PROCESSICON));
  82. HICON hIcon2 = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_GRAPHICON));
  83.  
  84. ImageList_AddIcon(hImageList, hIcon1);
  85. ImageList_AddIcon(hImageList, hIcon2);
  86.  
  87. DestroyIcon(hIcon1);
  88. DestroyIcon(hIcon2);
  89. return hImageList;
  90. }
  91.  
  92. void AddTabItems(HWND hTab) {
  93. HIMAGELIST hImageList = CreateTabImageList();
  94. TabCtrl_SetImageList(hTab, hImageList);
  95.  
  96. TCITEM tie = { };
  97. tie.mask = TCIF_TEXT | TCIF_IMAGE;
  98.  
  99. tie.pszText = (LPWSTR)L"Процессы";
  100. tie.iImage = 0;
  101. TabCtrl_InsertItem(hTab, 0, &tie);
  102.  
  103. tie.pszText = (LPWSTR)L"Производительность";
  104. tie.iImage = 1;
  105. TabCtrl_InsertItem(hTab, 1, &tie);
  106. }
  107.  
  108. HMENU CreateContextMenu() {
  109. HMENU hMenu = CreatePopupMenu();
  110. AppendMenu(hMenu, MF_STRING, IDM_CLOSE_PROCESS, L"Закрыть процесс");
  111. AppendMenu(hMenu, MF_STRING, IDM_OPEN_LOCATION, L"Открыть расположение файла");
  112. return hMenu;
  113. }
  114.  
  115. void HandleContextMenu(HWND hwnd, WPARAM wParam, LPARAM lParam) {
  116. POINT pt;
  117. GetCursorPos(&pt);
  118. HMENU hMenu = CreateContextMenu();
  119. TrackPopupMenu(hMenu, TPM_RIGHTBUTTON, pt.x, pt.y, 0, hwnd, NULL);
  120. DestroyMenu(hMenu);
  121. }
  122.  
  123. void CloseProcessById(DWORD processId) {
  124. HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, processId);
  125. if (hProcess) {
  126. TerminateProcess(hProcess, 0) ? MessageBox(NULL, L"Процесс успешно завершён.", L"Успех", MB_OK | MB_ICONINFORMATION) : MessageBox(NULL, L"Не удалось завершить процесс.", L"Ошибка", MB_OK | MB_ICONERROR);
  127. CloseHandle(hProcess);
  128. } else {
  129. MessageBox(NULL, L"Не удалось открыть процесс для завершения.", L"Ошибка", MB_OK | MB_ICONERROR);
  130. }
  131. }
  132.  
  133. void OpenProcessLocation(DWORD processId) {
  134. HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processId);
  135. if (hProcess) {
  136. TCHAR fullPath[MAX_PATH] = {0};
  137. DWORD size = MAX_PATH;
  138.  
  139. if (QueryFullProcessImageName(hProcess, 0, fullPath, &size)) {
  140. std::wstring folderPath(fullPath);
  141. folderPath = folderPath.substr(0, folderPath.find_last_of(L"\\/"));
  142.  
  143. ShellExecute(NULL, L"open", folderPath.c_str(), NULL, NULL, SW_SHOWNORMAL);
  144. } else {
  145. MessageBox(NULL, L"Не удалось получить путь процесса.", L"Ошибка", MB_OK | MB_ICONERROR);
  146. }
  147.  
  148. CloseHandle(hProcess);
  149. } else {
  150. MessageBox(NULL, L"Не удалось открыть процесс для получения пути.", L"Ошибка", MB_OK | MB_ICONERROR);
  151. }
  152. }
  153.  
  154. std::wstring GetPriority(DWORD processId)
  155. {
  156. HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processId);
  157. if (hProcess)
  158. {
  159. DWORD priorityClass = GetPriorityClass(hProcess);
  160. if (priorityClass == 0) {
  161. CloseHandle(hProcess);
  162. return L"Неизвестно";
  163. }
  164.  
  165. std::wstring priorityString;
  166. switch (priorityClass) {
  167. case IDLE_PRIORITY_CLASS: priorityString = L"Низкий"; break;
  168. case BELOW_NORMAL_PRIORITY_CLASS: priorityString = L"Ниже нормального"; break;
  169. case NORMAL_PRIORITY_CLASS: priorityString = L"Нормальный"; break;
  170. case ABOVE_NORMAL_PRIORITY_CLASS: priorityString = L"Выше нормального"; break;
  171. case HIGH_PRIORITY_CLASS: priorityString = L"Высокий"; break;
  172. case REALTIME_PRIORITY_CLASS: priorityString = L"Реального времени"; break;
  173. default: priorityString = L"Неизвестно"; break;
  174. }
  175.  
  176. CloseHandle(hProcess);
  177. return priorityString;
  178. }
  179. return L"Нет доступа";
  180. }
  181.  
  182. void ChangeIcon(PROCESSENTRY32 pe32, HWND hListView, LVITEM& lvi, std::map<std::wstring, int>& iconMap)
  183. {
  184. TCHAR fullPath[MAX_PATH];
  185. DWORD processNameLength = sizeof(fullPath) / sizeof(TCHAR);
  186. HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pe32.th32ProcessID);
  187. if (hProcess) {
  188. if (QueryFullProcessImageName(hProcess, 0, fullPath, &processNameLength)) {
  189. std::wstring processPath(fullPath);
  190. auto it = iconMap.find(processPath);
  191. if (it != iconMap.end())
  192. {
  193. lvi.iImage = it->second;
  194. } else
  195. {
  196. HICON hIcon = ExtractIcon(NULL, fullPath, 0);
  197. if (hIcon) {
  198. int imageIndex = ImageList_AddIcon(ListView_GetImageList(hListView, LVSIL_SMALL), hIcon);
  199. if (imageIndex != -1) {
  200. lvi.iImage = imageIndex;
  201. iconMap[processPath] = imageIndex;
  202. }
  203. DestroyIcon(hIcon);
  204. }
  205. }
  206. }
  207. CloseHandle(hProcess);
  208. }
  209. }
  210.  
  211. bool Is64BitProcess(DWORD processID) {
  212. HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, processID);
  213. if (hProcess) {
  214. BOOL is64Bit = FALSE;
  215. IsWow64Process(hProcess, &is64Bit);
  216. CloseHandle(hProcess);
  217. return is64Bit == FALSE;
  218. }
  219. return false;
  220. }
  221.  
  222. std::wstring GetProcessTimeCreation(DWORD processId)
  223. {
  224. HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processId);
  225. if (hProcess) {
  226. FILETIME creationTime, exitTime, kernelTime, userTime;
  227. if (GetProcessTimes(hProcess, &creationTime, &exitTime, &kernelTime, &userTime)) {
  228. SYSTEMTIME st;
  229. FileTimeToSystemTime(&creationTime, &st);
  230. st.wHour += 3; //For UTC
  231. wchar_t timeString[100];
  232. (void)swprintf_s(timeString, 100, L"%02d/%02d/%04d %02d:%02d:%02d",
  233. st.wDay, st.wMonth, st.wYear, st.wHour, st.wMinute, st.wSecond);
  234. std::wstring result(timeString);
  235. return result;
  236. }
  237. CloseHandle(hProcess);
  238. }
  239. return L"";
  240. }
  241.  
  242. std::wstring FormatFloat(int precision, double value) {
  243. std::wstringstream stream;
  244. stream.precision(precision);
  245. stream << std::fixed << value << L" MB";
  246. return stream.str();
  247. }
  248.  
  249. std::wstring GetMemoryUsage(HANDLE hProcess) {
  250. PROCESS_MEMORY_COUNTERS_EX pmc;
  251. if (GetProcessMemoryInfo(hProcess, (PROCESS_MEMORY_COUNTERS*)&pmc, sizeof(pmc))) {
  252.  
  253. double memoryUsageMB = static_cast<double>(pmc.WorkingSetSize) / (1024.0 * 1024.0);
  254. return FormatFloat(3, memoryUsageMB);
  255. }
  256. return L"N/A";
  257. }
  258.  
  259. HWND CreateProcessListView(HWND hParent) {
  260. HWND hListView = CreateWindowEx(0, WC_LISTVIEW, NULL,
  261. WS_CHILD | WS_VISIBLE | LVS_REPORT | LVS_SINGLESEL,
  262. ProcessListViewX, ProcessListViewY, SCREEN_WIDTH - 40, SCREEN_HEIGHT - 70,
  263. hParent, (HMENU)IDC_LISTVIEW_PROCESSES, (HINSTANCE)GetWindowLongPtr(hParent, GWLP_HINSTANCE), NULL);
  264.  
  265. LVCOLUMN lvColumn;
  266.  
  267. HIMAGELIST hImageList = ImageList_Create(16, 16, ILC_COLOR32 | ILC_MASK, 1, 1);
  268. ImageList_SetBkColor(hImageList, CLR_NONE);
  269.  
  270. HICON hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MAINICON));
  271. ImageList_AddIcon(hImageList, LoadIcon(NULL, IDI_WINLOGO));
  272. ImageList_AddIcon(hImageList, hIcon);
  273.  
  274. DestroyIcon(hIcon);
  275. ListView_SetImageList(hListView, hImageList, LVSIL_SMALL);
  276. ListView_SetExtendedListViewStyle(hListView, LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP | LVS_EX_DOUBLEBUFFER);
  277.  
  278. const std::wstring columnHeaders[] = {
  279. L"Имя процесса", L"ID процесса", L"ID родительского процесса", L"Приоритет",
  280. L"Загрузка ЦП", L"Загрузка Памяти", L"Время создания", L"Путь к файлу", L"Битность"
  281. };
  282. int columnWidths[] = { 200, 145, 145, 150, 150, 185, 180, 240, 80 };
  283.  
  284. for (int i = 0; i < 9; i++) {
  285. lvColumn.mask = (i == 0)
  286. ? LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM | LVCF_FMT | LVCF_IMAGE
  287. : LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM | LVCF_FMT;
  288.  
  289. lvColumn.fmt = LVCFMT_CENTER;
  290. lvColumn.cchTextMax = MAX_PATH;
  291. lvColumn.pszText = const_cast<LPWSTR>(columnHeaders[i].c_str());
  292. lvColumn.cx = columnWidths[i];
  293. lvColumn.iImage = 1;
  294. lvColumn.iSubItem = i;
  295.  
  296. ListView_InsertColumn(hListView, i, &lvColumn);
  297. }
  298.  
  299. return hListView;
  300. }
  301.  
  302. int FindProcessInListViewByPID(HWND hListView, DWORD processID) {
  303. LVFINDINFO findInfo = { };
  304. findInfo.flags = LVFI_PARAM;
  305. findInfo.lParam = processID;
  306. return ListView_FindItem(hListView, -1, &findInfo);
  307. }
  308.  
  309. void RemoveStaleProcesses(HWND hListView, const std::set<DWORD>& existingProcesses) {
  310. int itemCount = ListView_GetItemCount(hListView);
  311. for (int i = itemCount - 1; i >= 0; --i) {
  312. LVITEM lvi = { };
  313. lvi.iItem = i;
  314. lvi.mask = LVIF_PARAM;
  315.  
  316. if (ListView_GetItem(hListView, &lvi)) {
  317. DWORD processID = static_cast<DWORD>(lvi.lParam);
  318.  
  319. if (existingProcesses.find(processID) == existingProcesses.end()) {
  320. ListView_DeleteItem(hListView, i);
  321. }
  322. }
  323. }
  324. }
  325.  
  326. void PopulateProcessListView(HWND hListView, std::map<std::wstring, int>& iconMap)
  327. {
  328. SCROLLINFO si = { };
  329. si.cbSize = sizeof(SCROLLINFO);
  330. si.fMask = SIF_POS;
  331. GetScrollInfo(hListView, SB_VERT, &si);
  332. int scrollPos = si.nPos;
  333.  
  334. SendMessage(hListView, WM_SETREDRAW, FALSE, 0);
  335.  
  336. HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
  337. if (hProcessSnap == INVALID_HANDLE_VALUE) {
  338. return;
  339. }
  340.  
  341. PROCESSENTRY32 pe32 = { };
  342. pe32.dwSize = sizeof(PROCESSENTRY32);
  343. std::set<DWORD> existingProcesses;
  344.  
  345. if (Process32First(hProcessSnap, &pe32)) {
  346. do {
  347. std::wstring exeFile = pe32.szExeFile;
  348. DWORD processID = pe32.th32ProcessID;
  349.  
  350. int itemIndex = FindProcessInListViewByPID(hListView, processID);
  351.  
  352. LVITEM lvi = { };
  353. lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
  354. lvi.pszText = const_cast<LPWSTR>(exeFile.c_str());
  355. //lvi.iItem = (itemIndex == -1) ? 0 : ListView_GetItemCount(hListView);
  356. lvi.iItem = (itemIndex == -1) ? ListView_GetItemCount(hListView) : itemIndex;
  357. lvi.iImage = 0;
  358. lvi.cchTextMax = MAX_PATH;
  359. lvi.lParam = processID;
  360.  
  361. ChangeIcon(pe32, hListView, lvi, iconMap);
  362.  
  363. if (itemIndex == -1) {
  364. ListView_InsertItem(hListView, &lvi);
  365. } else {
  366. ListView_SetItem(hListView, &lvi);
  367. }
  368.  
  369. ListView_SetItemText(hListView, lvi.iItem, 1, (LPWSTR)std::to_wstring(pe32.th32ProcessID).c_str());
  370. std::wstring parent = pe32.th32ParentProcessID == 0 ? L"Главный процесс" : std::to_wstring(pe32.th32ParentProcessID);
  371. ListView_SetItemText(hListView, lvi.iItem, 2, (LPWSTR)parent.c_str());
  372. ListView_SetItemText(hListView, lvi.iItem, 3, (LPWSTR)GetPriority(pe32.th32ProcessID).c_str());
  373.  
  374. HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pe32.th32ProcessID);
  375. if (hProcess)
  376. {
  377. std::wstring memUsage = GetMemoryUsage(hProcess);
  378. ListView_SetItemText(hListView, lvi.iItem, 5, (LPWSTR)memUsage.c_str());
  379. CloseHandle(hProcess);
  380. }
  381.  
  382. ListView_SetItemText(hListView, lvi.iItem, 6, (LPWSTR)GetProcessTimeCreation(pe32.th32ProcessID).c_str());
  383.  
  384. TCHAR fullPath[MAX_PATH];
  385. DWORD processNameLength = sizeof(fullPath) / sizeof(TCHAR);
  386. if (QueryFullProcessImageName(OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pe32.th32ProcessID), 0, fullPath, &processNameLength)) {
  387. ListView_SetItemText(hListView, lvi.iItem, 7, (LPWSTR)fullPath);
  388. }
  389. ListView_SetItemText(hListView, lvi.iItem, 8, Is64BitProcess(pe32.th32ProcessID) ? (LPWSTR)L"64-бит" : (LPWSTR)L"32-бит");
  390.  
  391. existingProcesses.insert(processID);
  392. } while (Process32Next(hProcessSnap, &pe32));
  393. }
  394. CloseHandle(hProcessSnap);
  395.  
  396. RemoveStaleProcesses(hListView, existingProcesses);
  397.  
  398. SendMessage(hListView, WM_SETREDRAW, TRUE, 0);
  399.  
  400. si.nPos = scrollPos;
  401. SetScrollInfo(hListView, SB_VERT, &si, TRUE);
  402. }
  403.  
  404. int GetCurrentRAMUsage() {
  405. MEMORYSTATUSEX memInfo;
  406. memInfo.dwLength = sizeof(MEMORYSTATUSEX);
  407.  
  408. if (GlobalMemoryStatusEx(&memInfo)) {
  409. DWORDLONG totalRAM = memInfo.ullTotalPhys;
  410. DWORDLONG freeRAM = memInfo.ullAvailPhys;
  411.  
  412. int ramUsage = static_cast<int>((1.0 - (double)freeRAM / totalRAM) * 100);
  413. return ramUsage;
  414. }
  415.  
  416. return 0;
  417. }
  418.  
  419. typedef struct _SYSTEM_PROCESSOR_TIMES {
  420. ULONGLONG IdleTime;
  421. ULONGLONG KernelTime;
  422. ULONGLONG UserTime;
  423. ULONGLONG DpcTime;
  424. ULONGLONG InterruptTime;
  425. ULONG InterruptCount;
  426. } SYSTEM_PROCESSOR_TIMES;
  427.  
  428. typedef NTSTATUS (NTAPI *ZwQuerySystemInformationFunc)(
  429. ULONG SystemInformationClass,
  430. PVOID SystemInformation,
  431. ULONG SystemInformationLength,
  432. PULONG ReturnLength
  433. );
  434.  
  435. #define SystemProcessorTimesCLASS 8
  436.  
  437. int GetCurrentCPUUsage(SYSTEM_INFO sysInfo, SYSTEM_PROCESSOR_TIMES* CurrentSysProcTimes,
  438. SYSTEM_PROCESSOR_TIMES* PreviousSysProcTimes,
  439. ZwQuerySystemInformationFunc ZwQuerySystemInformation)
  440. {
  441. static DWORD oldTime = GetTickCount();
  442. DWORD nowTime = GetTickCount();
  443. DWORD perTime = nowTime - oldTime;
  444. oldTime = nowTime;
  445.  
  446. ZwQuerySystemInformation(SystemProcessorTimesCLASS, &CurrentSysProcTimes[0], sizeof(SYSTEM_PROCESSOR_TIMES) * sysInfo.dwNumberOfProcessors, nullptr);
  447.  
  448. ULONGLONG totalIdleTime = 0;
  449. for (int j = 0; j < sysInfo.dwNumberOfProcessors; j++) {
  450. totalIdleTime += CurrentSysProcTimes[j].IdleTime - PreviousSysProcTimes[j].IdleTime;
  451. }
  452.  
  453. ULONGLONG averageIdleTime = totalIdleTime / sysInfo.dwNumberOfProcessors;
  454.  
  455. double idleTimeMs = averageIdleTime / 10000.0;
  456.  
  457. double cpuUsage = 100.0 * (1.0 - (idleTimeMs / perTime));
  458.  
  459. if (cpuUsage < 0 || cpuUsage > 100) {
  460. cpuUsage = 0;
  461. }
  462.  
  463. memcpy(&PreviousSysProcTimes[0], &CurrentSysProcTimes[0], sizeof(SYSTEM_PROCESSOR_TIMES) * sysInfo.dwNumberOfProcessors);
  464. return cpuUsage;
  465. }
  466.  
  467. std::wstring GetSystemUptime() {
  468. ULONGLONG systemUptime = GetTickCount64();
  469. ULONGLONG seconds = (systemUptime / 1000) % 60;
  470. ULONGLONG minutes = (systemUptime / (1000 * 60)) % 60;
  471. ULONGLONG hours = (systemUptime / (1000 * 60 * 60)) % 24;
  472. ULONGLONG days = (systemUptime / (1000 * 60 * 60 * 24));
  473.  
  474. return L"Время работы: " + std::to_wstring(days) + L" дн. " +
  475. std::to_wstring(hours) + L" ч. " +
  476. std::to_wstring(minutes) + L" мин. " +
  477. std::to_wstring(seconds) + L" сек.";
  478. }
  479.  
  480. std::wstring GetMemoryInfo()
  481. {
  482. MEMORYSTATUSEX memoryStatus;
  483. memoryStatus.dwLength = sizeof(MEMORYSTATUSEX);
  484.  
  485. if (GlobalMemoryStatusEx(&memoryStatus))
  486. {
  487. ULONGLONG totalMemoryMB = memoryStatus.ullTotalPhys / (1024 * 1024);
  488. ULONGLONG freeMemoryMB = memoryStatus.ullAvailPhys / (1024 * 1024);
  489.  
  490. std::wstring result = L"Общий объем памяти: " + std::to_wstring(totalMemoryMB) + L" МБ\r\n" +
  491. L"Свободно: " + std::to_wstring(freeMemoryMB) + L" МБ";
  492. return result;
  493. }
  494. return L"Ошибка получения информации о памяти";
  495. }
  496.  
  497. void DrawGraph(HWND hPerfomance, std::vector<int>& history, const int maxHistory)
  498. {
  499. HDC hdc = GetDC(hPerfomance);
  500. RECT rect;
  501. GetClientRect(hPerfomance, &rect);
  502.  
  503. int width = rect.right - rect.left;
  504. int height = rect.bottom - rect.top;
  505.  
  506. HDC memDC = CreateCompatibleDC(hdc);
  507.  
  508. HBITMAP memBitmap = CreateCompatibleBitmap(hdc, width, height);
  509.  
  510. HBITMAP oldBitmap = (HBITMAP)SelectObject(memDC, memBitmap);
  511.  
  512. HBRUSH backgroundBrush = CreateSolidBrush(RGB(30, 30, 30));
  513. FillRect(memDC, &rect, backgroundBrush);
  514. DeleteObject(backgroundBrush);
  515.  
  516. SetBkMode(memDC, TRANSPARENT);
  517. SetStretchBltMode(memDC, HALFTONE);
  518.  
  519. HPEN gridPen = CreatePen(PS_DOT, 1, RGB(60, 60, 60));
  520. SelectObject(memDC, gridPen);
  521. for (int i = 1; i <= 10; ++i) {
  522. int y = height - 20 - (height - 40) * i / 10;
  523. MoveToEx(memDC, 0, y, NULL);
  524. LineTo(memDC, width, y);
  525. }
  526.  
  527. if (!history.empty()) {
  528. HPEN graphPen = CreatePen(PS_SOLID, 2, RGB(0, 255, 127));
  529. SelectObject(memDC, graphPen);
  530.  
  531. int spacing = width / maxHistory;
  532. int prevX = 0;
  533. int prevY = height - 20 - (history[0] * (height - 20) / 100);
  534.  
  535. for (size_t i = 1; i < history.size(); ++i) {
  536. int x = i * spacing;
  537. int y = height - 20 - (history[i] * (height - 20) / 100);
  538.  
  539. MoveToEx(memDC, prevX, prevY, NULL);
  540. LineTo(memDC, x, y);
  541.  
  542. prevX = x;
  543. prevY = y;
  544. }
  545. DeleteObject(graphPen);
  546. }
  547.  
  548. DeleteObject(gridPen);
  549.  
  550. BitBlt(hdc, 0, 0, width, height, memDC, 0, 0, SRCCOPY);
  551.  
  552. SelectObject(memDC, oldBitmap);
  553. DeleteObject(memBitmap);
  554. DeleteDC(memDC);
  555. ReleaseDC(hPerfomance, hdc);
  556. }
  557.  
  558. void PushValue(std::vector<int>& history, const int maxHistory, int newValue)
  559. {
  560. history.push_back(newValue);
  561. if (history.size() > maxHistory) {
  562. history.erase(history.begin());
  563. }
  564. }
  565.  
  566. LRESULT CALLBACK ProcessListViewProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  567. static int selectedItemIndex;
  568. switch (uMsg)
  569. {
  570. case WM_COMMAND:
  571. {
  572. int wmId = LOWORD(wParam);
  573. int wmEvent = HIWORD(wParam);
  574.  
  575. switch (wmId)
  576. {
  577. case IDM_CLOSE_PROCESS:
  578. {
  579. wchar_t buffer[32];
  580. ListView_GetItemText(hwnd, selectedItemIndex, 1, buffer, 32);
  581. DWORD processId = _wtoi(buffer);
  582. CloseProcessById(processId);
  583. }
  584. break;
  585.  
  586. case IDM_OPEN_LOCATION:
  587. {
  588. wchar_t buffer[32];
  589. ListView_GetItemText(hwnd, selectedItemIndex, 1, buffer, 32);
  590. DWORD processId = _wtoi(buffer);
  591. OpenProcessLocation(processId);
  592. }
  593. break;
  594. }
  595. }
  596. break;
  597.  
  598. case WM_CONTEXTMENU: {
  599. if ((HWND)wParam == hwnd)
  600. {
  601. LVHITTESTINFO hitTestInfo = { 0 };
  602. POINT pt;
  603. GetCursorPos(&pt);
  604. ScreenToClient(hwnd, &pt);
  605. hitTestInfo.pt = pt;
  606. ListView_HitTest(hwnd, &hitTestInfo);
  607. selectedItemIndex = hitTestInfo.iItem;
  608. if (selectedItemIndex != -1)
  609. {
  610. HandleContextMenu(hwnd, wParam, lParam);
  611. }
  612. }
  613. break;
  614. }
  615.  
  616. case WM_NOTIFY:
  617. {
  618. LPNMHDR nmhdr = (LPNMHDR)lParam;
  619.  
  620. if (nmhdr->code == HDN_ITEMCHANGING) {
  621. LPNMHEADER pnmhHeader = (LPNMHEADER)lParam;
  622. if (pnmhHeader->pitem->cxy < 30) {
  623. pnmhHeader->pitem->cxy = 30;
  624. }
  625. }
  626.  
  627.  
  628. if (nmhdr->hwndFrom == ListView_GetHeader(hwnd) && nmhdr->code == NM_CUSTOMDRAW)
  629. {
  630. LPNMCUSTOMDRAW lpNMCustomDraw = (LPNMCUSTOMDRAW)lParam;
  631.  
  632. switch (lpNMCustomDraw->dwDrawStage)
  633. {
  634. case CDDS_PREPAINT:
  635. return CDRF_NOTIFYITEMDRAW;
  636.  
  637. case CDDS_ITEMPREPAINT:
  638. {
  639. COLORREF rgb;
  640. if (lpNMCustomDraw->uItemState & CDIS_SELECTED)
  641. {
  642. rgb = RGB(26, 188, 156); // Цвет фона для выделенного элемента
  643. }
  644. else
  645. {
  646. rgb = RGB(47, 45, 60); // Цвет фона для обычных элементов
  647. }
  648. SetBkColor(lpNMCustomDraw->hdc, rgb);
  649. SetTextColor(lpNMCustomDraw->hdc, RGB(255, 255, 255));
  650.  
  651. RECT rc = lpNMCustomDraw->rc;
  652. HBRUSH hBrush = CreateSolidBrush(RGB(31, 29, 40));
  653. FillRect(lpNMCustomDraw->hdc, &rc, hBrush);
  654. DeleteObject(hBrush);
  655.  
  656. RECT textRect = rc;
  657. InflateRect(&textRect, -1, -1);
  658.  
  659. hBrush = CreateSolidBrush(rgb);
  660. FillRect(lpNMCustomDraw->hdc, &textRect, hBrush);
  661. DeleteObject(hBrush);
  662.  
  663. wchar_t text[MAX_PATH];
  664. HDITEM hdi = { 0 };
  665. hdi.mask = HDI_TEXT;
  666. hdi.pszText = text;
  667. hdi.cchTextMax = std::size(text);
  668.  
  669. Header_GetItem(nmhdr->hwndFrom, lpNMCustomDraw->dwItemSpec, &hdi);
  670.  
  671. DrawText(lpNMCustomDraw->hdc, text, -1, &rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  672.  
  673. return CDRF_SKIPDEFAULT;
  674. }
  675. }
  676. }
  677.  
  678. }
  679.  
  680. default:
  681. return CallWindowProc((WNDPROC)GetWindowLongPtr(hwnd, GWLP_USERDATA), hwnd, uMsg, wParam, lParam);
  682. }
  683. return CallWindowProc((WNDPROC)GetWindowLongPtr(hwnd, GWLP_USERDATA), hwnd, uMsg, wParam, lParam);
  684. }
  685.  
  686. LRESULT CALLBACK TabControlProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  687. {
  688. switch (uMsg)
  689. {
  690. case WM_NOTIFY:
  691. {
  692. LPNMHDR pnmh = (LPNMHDR)lParam;
  693.  
  694. if (pnmh->code == NM_CUSTOMDRAW)
  695. {
  696. LPNMLVCUSTOMDRAW pLVCD = (LPNMLVCUSTOMDRAW)lParam;
  697. switch (pLVCD->nmcd.dwDrawStage)
  698. {
  699. case CDDS_PREPAINT:
  700. return CDRF_NOTIFYITEMDRAW;
  701.  
  702. case CDDS_ITEMPREPAINT:
  703. pLVCD->clrText = RGB(255, 255, 255);
  704. pLVCD->clrTextBk = RGB(57, 66, 100);
  705. return CDRF_DODEFAULT;
  706.  
  707. default:
  708. break;
  709. }
  710. }
  711. break;
  712. }
  713.  
  714. case WM_CTLCOLORSTATIC:
  715. {
  716. HDC hdcStatic = (HDC)wParam;
  717. HWND hStatic = (HWND)lParam;
  718.  
  719. SetBkColor(hdcStatic, RGB(43, 41, 55));
  720. SetTextColor(hdcStatic, RGB(255, 255, 255));
  721.  
  722. HBRUSH hBrush = CreateSolidBrush(RGB(43, 41, 55));
  723. return (LONG_PTR)hBrush;
  724. }
  725.  
  726.  
  727. default:
  728. return CallWindowProc((WNDPROC)GetWindowLongPtr(hwnd, GWLP_USERDATA), hwnd, uMsg, wParam, lParam);
  729. }
  730. return CallWindowProc((WNDPROC)GetWindowLongPtr(hwnd, GWLP_USERDATA), hwnd, uMsg, wParam, lParam);
  731. }
  732.  
  733. LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  734. {
  735. static HWND hTabControl, hListViewProcesses;
  736. static std::map<std::wstring, int> iconMap;
  737. static HWND hProgressBarCPU;
  738. static HWND hProgressBarRAM;
  739.  
  740. static HWND hPerfomanceCPU, hPerfomanceRAM;
  741.  
  742. static UINT_PTR timerId = 0;
  743. static int updateInterval = 1000;
  744.  
  745. static HMODULE lib = nullptr;
  746. static SYSTEM_PROCESSOR_TIMES* CurrentSysProcTimes;
  747. static SYSTEM_PROCESSOR_TIMES* PreviousSysProcTimes;
  748. static ZwQuerySystemInformationFunc ZwQuerySystemInformation = nullptr;
  749.  
  750. static HWND hLabelCPU, hLabelRAM, hLabelUptime, hLabelMemoryInfo;
  751. static SYSTEM_INFO sysInfo;
  752.  
  753. static std::vector<int> cpuUsageHistory;
  754. static std::vector<int> ramUsageHistory;
  755. static const int cpuUsageHistoryMax = 100;
  756. static const int ramUsageHistoryMax = 100;
  757.  
  758. switch (uMsg)
  759. {
  760. case WM_ERASEBKGND:
  761. return 1;
  762.  
  763. case WM_DRAWITEM:
  764. {
  765. LPDRAWITEMSTRUCT lpDrawItem = (LPDRAWITEMSTRUCT)lParam;
  766. if (lpDrawItem->CtlID == IDC_TABCONTROL)
  767. {
  768. TCHAR szTabText[256];
  769. TCITEM tci;
  770. tci.mask = TCIF_TEXT;
  771. tci.pszText = szTabText;
  772. tci.cchTextMax = sizeof(szTabText) / sizeof(TCHAR);
  773. TabCtrl_GetItem(hTabControl, lpDrawItem->itemID, &tci);
  774.  
  775. RECT rcTab = lpDrawItem->rcItem;
  776.  
  777. HBRUSH hBrush = CreateSolidBrush((lpDrawItem->itemState & ODS_SELECTED) ?
  778. RGB(57, 66, 100) : RGB(43, 41, 55));
  779. FillRect(lpDrawItem->hDC, &rcTab, hBrush);
  780. DeleteObject(hBrush);
  781.  
  782. SetTextColor(lpDrawItem->hDC, RGB(255, 255, 255));
  783. SetBkMode(lpDrawItem->hDC, TRANSPARENT);
  784. DrawText(lpDrawItem->hDC, szTabText, -1, &lpDrawItem->rcItem,
  785. DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  786.  
  787. return TRUE;
  788. }
  789. }
  790.  
  791. case WM_PAINT:
  792. {
  793. PAINTSTRUCT ps;
  794. HDC hdc = BeginPaint(hwnd, &ps);
  795.  
  796. HBRUSH darkBrush = CreateSolidBrush(RGB(43, 41, 55));
  797. FillRect(hdc, &ps.rcPaint, darkBrush);
  798. DeleteObject(darkBrush);
  799.  
  800. EndPaint(hwnd, &ps);
  801. }
  802. return 0;
  803.  
  804. case WM_CREATE:
  805. {
  806. GetSystemInfo(&sysInfo);
  807. CurrentSysProcTimes = new SYSTEM_PROCESSOR_TIMES[sysInfo.dwNumberOfProcessors];
  808. PreviousSysProcTimes = new SYSTEM_PROCESSOR_TIMES[sysInfo.dwNumberOfProcessors];
  809. ZeroMemory(CurrentSysProcTimes, sizeof(SYSTEM_PROCESSOR_TIMES) * sysInfo.dwNumberOfProcessors);
  810. ZeroMemory(PreviousSysProcTimes, sizeof(SYSTEM_PROCESSOR_TIMES) * sysInfo.dwNumberOfProcessors);
  811.  
  812. lib = LoadLibrary(L"ntdll.dll");
  813. if (!lib) {
  814. MessageBox(NULL, L"Не удалось загрузить ntdll.dll", L"Ошибка", MB_OK | MB_ICONERROR);
  815. PostQuitMessage(0);
  816. return -1;
  817. }
  818.  
  819. ZwQuerySystemInformation = (ZwQuerySystemInformationFunc)GetProcAddress(lib, "ZwQuerySystemInformation");
  820. if (!ZwQuerySystemInformation) {
  821. MessageBox(NULL, L"Не удалось найти функцию ZwQuerySystemInformation", L"Ошибка", MB_OK | MB_ICONERROR);
  822. FreeLibrary(lib);
  823. PostQuitMessage(0);
  824. return -1;
  825. }
  826.  
  827. if (ZwQuerySystemInformation(SystemProcessorTimesCLASS, &PreviousSysProcTimes[0], sizeof(SYSTEM_PROCESSOR_TIMES) * sysInfo.dwNumberOfProcessors, nullptr) != 0) {
  828. MessageBox(NULL, L"Не удалось получить данные о процессорах", L"Ошибка", MB_OK | MB_ICONERROR);
  829. FreeLibrary(lib);
  830. PostQuitMessage(0);
  831. return -1;
  832. }
  833.  
  834. hTabControl = CreateWindowEx(WS_EX_TRANSPARENT, WC_TABCONTROL, NULL,
  835. WS_CHILD | WS_VISIBLE | TCS_BUTTONS | TCS_OWNERDRAWFIXED,
  836. TabControlX, TabControlY, SCREEN_WIDTH - 20, SCREEN_HEIGHT - 40,
  837. hwnd, (HMENU)IDC_TABCONTROL, (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE), NULL);
  838.  
  839. WNDPROC oldTabControlProc = (WNDPROC)GetWindowLongPtr(hTabControl, GWLP_WNDPROC);
  840. SetWindowLongPtr(hTabControl, GWLP_WNDPROC, (LONG_PTR)TabControlProc);
  841. SetWindowLongPtr(hTabControl, GWLP_USERDATA, (LONG_PTR)oldTabControlProc);
  842.  
  843. SetClassLongPtr(hTabControl, GCLP_HBRBACKGROUND, (LONG_PTR)CreateSolidBrush(RGB(43, 41, 55)));
  844.  
  845. AddTabItems(hTabControl);
  846. SetMenu(hwnd, CreateAppMenu());
  847.  
  848. hListViewProcesses = CreateProcessListView(hTabControl);
  849. PopulateProcessListView(hListViewProcesses, iconMap);
  850. ListView_SetBkColor(hListViewProcesses, RGB(57, 66, 100));
  851.  
  852. WNDPROC oldListViewProcessesProc = (WNDPROC)SetWindowLongPtr(hListViewProcesses, GWLP_WNDPROC, (LONG_PTR)ProcessListViewProc);
  853. SetWindowLongPtr(hListViewProcesses, GWLP_USERDATA, (LONG_PTR)oldListViewProcessesProc);
  854.  
  855. hProgressBarCPU = CreateWindowEx(0, PROGRESS_CLASS, L"",
  856. WS_CHILD | PBS_SMOOTH | PBS_MARQUEE,
  857. 10, 50, 300, 25, hTabControl, (HMENU)IDC_PROGRESSBAR_CPU,
  858. (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE), NULL);
  859.  
  860. SendMessage(hProgressBarCPU, PBM_SETRANGE, 0, MAKELPARAM(0, 100));
  861. SetClassLongPtr(hProgressBarCPU, GCLP_HBRBACKGROUND, (LONG_PTR)CreateSolidBrush(RGB(43, 41, 55)));
  862.  
  863. hProgressBarRAM = CreateWindowEx(0, PROGRESS_CLASS, L"",
  864. WS_CHILD | PBS_SMOOTH | PBS_MARQUEE,
  865. SCREEN_WIDTH - 340, 50, 300, 25, hTabControl, (HMENU)IDC_PROGRESSBAR_RAM,
  866. (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE), NULL);
  867.  
  868. hLabelCPU = CreateWindowEx(0, WC_STATIC, L"",
  869. WS_CHILD,
  870. 10, 100, 150, 20, hTabControl, NULL,
  871. (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE), NULL);
  872.  
  873. hLabelRAM = CreateWindowEx(0, WC_STATIC, L"",
  874. WS_CHILD,
  875. SCREEN_WIDTH - 340, 100, 150, 20, hTabControl, NULL,
  876. (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE), NULL);
  877.  
  878. hLabelUptime = CreateWindowEx(0, WC_STATIC, L"",
  879. WS_CHILD,
  880. SCREEN_WIDTH / 2 - 150, 50, 300, 20, hTabControl, NULL,
  881. (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE), NULL);
  882.  
  883. hLabelMemoryInfo = CreateWindowEx(0, WC_STATIC, L"",
  884. WS_CHILD,
  885. SCREEN_WIDTH / 2 - 150, 75, 300, 40, hTabControl, NULL,
  886. (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE), NULL);
  887.  
  888. hPerfomanceCPU = CreateWindowEx(0, WC_STATIC, L"", WS_CHILD,
  889. 10, 140, SCREEN_WIDTH / 2 - 50, SCREEN_HEIGHT * 0.65, hTabControl, NULL, (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE), NULL);
  890.  
  891. hPerfomanceRAM = CreateWindowEx(0, WC_STATIC, L"", WS_CHILD,
  892. SCREEN_WIDTH / 2, 140, SCREEN_WIDTH / 2 - 50, SCREEN_HEIGHT * 0.65, hTabControl, NULL, (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE), NULL);
  893.  
  894. SendMessage(hProgressBarRAM, PBM_SETRANGE, 0, MAKELPARAM(0, 100));
  895.  
  896. SendMessage(hProgressBarCPU, PBM_SETBARCOLOR, 0, RGB(26, 188, 156)); // Синий прогрессбар
  897. SendMessage(hProgressBarRAM, PBM_SETBARCOLOR, 0, RGB(26, 188, 156)); // Зелёный прогрессбар
  898.  
  899. SetTimer(hwnd, TIMER_GRAPH, 50, NULL);
  900. timerId = SetTimer(hwnd, TIMER_PROCESSES, updateInterval, NULL);
  901. }
  902. break;
  903.  
  904. case WM_TIMER:
  905. {
  906. if (wParam == TIMER_PROCESSES)
  907. {
  908. PopulateProcessListView(hListViewProcesses, iconMap);
  909. }
  910.  
  911. if (wParam == TIMER_GRAPH)
  912. {
  913. static int counter = 0;
  914. if (counter >= 20)
  915. {
  916. counter = 0;
  917. int cpuUsage = GetCurrentCPUUsage(sysInfo, CurrentSysProcTimes, PreviousSysProcTimes, ZwQuerySystemInformation);
  918. int ramUsage = GetCurrentRAMUsage();
  919.  
  920. SendMessage(hProgressBarCPU, PBM_SETPOS, cpuUsage, 0);
  921. SendMessage(hProgressBarRAM, PBM_SETPOS, ramUsage, 0);
  922.  
  923. std::wstring pcTime = GetSystemUptime();
  924. std::wstring cpuLabel = L"Загрузка ЦП: " + std::to_wstring(cpuUsage) + L"%";
  925. std::wstring ramLabel = L"Загрузка Памяти: " + std::to_wstring(ramUsage) + L"%";
  926.  
  927. SetWindowText(hLabelCPU, cpuLabel.c_str());
  928. SetWindowText(hLabelRAM, ramLabel.c_str());
  929. SetWindowText(hLabelUptime, pcTime.c_str());
  930. SetWindowText(hLabelMemoryInfo, GetMemoryInfo().c_str());
  931.  
  932. PushValue(cpuUsageHistory, cpuUsageHistoryMax, cpuUsage);
  933. PushValue(ramUsageHistory, ramUsageHistoryMax, ramUsage);
  934. } else
  935. counter++;
  936.  
  937. DrawGraph(hPerfomanceCPU, cpuUsageHistory, cpuUsageHistoryMax);
  938. DrawGraph(hPerfomanceRAM, ramUsageHistory, ramUsageHistoryMax);
  939. }
  940. }
  941.  
  942. /*case WM_DRAWITEM:
  943. {
  944. LPDRAWITEMSTRUCT lpDrawItem = (LPDRAWITEMSTRUCT)lParam;
  945. if (lpDrawItem->CtlID == ID_SELECT_BTN)
  946. {
  947.  
  948. }
  949. break;
  950. }*/
  951.  
  952. case WM_KEYDOWN:
  953. {
  954. int wmId = LOWORD(wParam);
  955. int wmEvent = HIWORD(wParam);
  956.  
  957. /*if (wParam == VK_RETURN) {
  958.  
  959. }*/
  960. }
  961. break;
  962.  
  963. case WM_NOTIFY:
  964. {
  965. NMHDR* nmhdr = (NMHDR*)lParam;
  966.  
  967.  
  968. if (nmhdr->hwndFrom == hTabControl && nmhdr->code == TCN_SELCHANGE) {
  969. int tabIndex = TabCtrl_GetCurSel(hTabControl);
  970. ShowWindow(hListViewProcesses, tabIndex == 0 ? SW_SHOW : SW_HIDE);
  971. ShowWindow(hProgressBarCPU, tabIndex == 1 ? SW_SHOW : SW_HIDE);
  972. ShowWindow(hProgressBarRAM, tabIndex == 1 ? SW_SHOW : SW_HIDE);
  973. ShowWindow(hLabelUptime, tabIndex == 1 ? SW_SHOW : SW_HIDE);
  974. ShowWindow(hLabelMemoryInfo, tabIndex == 1 ? SW_SHOW : SW_HIDE);
  975. ShowWindow(hLabelCPU, tabIndex == 1 ? SW_SHOW : SW_HIDE);
  976. ShowWindow(hLabelRAM, tabIndex == 1 ? SW_SHOW : SW_HIDE);
  977. ShowWindow(hPerfomanceCPU, tabIndex == 1 ? SW_SHOW : SW_HIDE);
  978. ShowWindow(hPerfomanceRAM, tabIndex == 1 ? SW_SHOW : SW_HIDE);
  979.  
  980. if (tabIndex == 1)
  981. {
  982. DrawGraph(hPerfomanceCPU, cpuUsageHistory, cpuUsageHistoryMax);
  983. DrawGraph(hPerfomanceRAM, ramUsageHistory, ramUsageHistoryMax);
  984. }
  985. }
  986.  
  987. if (nmhdr->hwndFrom == hListViewProcesses && nmhdr->code == LVN_COLUMNCLICK)
  988. {
  989. NMITEMACTIVATE* pnmItem = (NMITEMACTIVATE*)lParam;
  990. LVCOLUMN lvColumn;
  991. ZeroMemory(&lvColumn, sizeof(lvColumn));
  992.  
  993. static int nSortColumn = 0;
  994. static BOOL bSortAscending = TRUE;
  995.  
  996. if (pnmItem->iSubItem == nSortColumn)
  997. {
  998. bSortAscending = !bSortAscending;
  999. }
  1000. else
  1001. {
  1002. nSortColumn = pnmItem->iSubItem;
  1003. bSortAscending = TRUE;
  1004. }
  1005. //SortListView(hListViewProcesses, nSortColumn, bSortAscending);
  1006. }
  1007. }
  1008. break;
  1009.  
  1010. case WM_COMMAND:
  1011. {
  1012. int wmId = LOWORD(wParam);
  1013. int wmEvent = HIWORD(wParam);
  1014.  
  1015. switch (wmId)
  1016. {
  1017. case ID_MENU_EXIT:
  1018. {
  1019. SendMessage(hwnd, WM_CLOSE, wParam, lParam);
  1020. }
  1021. break;
  1022. case ID_MENU_ABOUT:
  1023. {
  1024. MessageBox(hwnd, L"Task Manager App\nDeveloped by Krutko Andrey\nBSUIR 2024", L"About", MB_OK | MB_ICONINFORMATION);
  1025. }
  1026. break;
  1027. case ID_MENU_UPDATE:
  1028. {
  1029. PopulateProcessListView(hListViewProcesses, iconMap);
  1030. }
  1031. break;
  1032.  
  1033. case ID_MENU_RARE:
  1034. updateInterval = 5000;
  1035. break;
  1036. case ID_MENU_NORMAL:
  1037. updateInterval = 3000;
  1038. break;
  1039. case ID_MENU_OFTEN:
  1040. updateInterval = 1000;
  1041. break;
  1042. case ID_MENU_NONE:
  1043. KillTimer(hwnd, TIMER_PROCESSES);
  1044. timerId = 0;
  1045. return 0;
  1046. }
  1047.  
  1048. if (timerId) {
  1049. KillTimer(hwnd, TIMER_PROCESSES);
  1050. }
  1051. timerId = SetTimer(hwnd, TIMER_PROCESSES, updateInterval, NULL);
  1052. }
  1053. break;
  1054.  
  1055. case WM_CLOSE:
  1056. if (MessageBox(hwnd, L"Вы действительно хотите выйти?", L"Внимание", MB_OKCANCEL) == IDOK)
  1057. {
  1058. DestroyWindow(hwnd);
  1059. }
  1060. break;
  1061.  
  1062. case WM_SIZE:
  1063. {
  1064. int width = LOWORD(lParam);
  1065. int height = HIWORD(lParam);
  1066. MoveWindow(hTabControl, TabControlY, TabControlY, width - 20, height - 40, TRUE);
  1067. MoveWindow(hListViewProcesses, ProcessListViewX, ProcessListViewY, width - 40, height - 70, TRUE);
  1068.  
  1069. return 0;
  1070. }
  1071.  
  1072. case WM_DESTROY:
  1073. {
  1074. if (lib) {
  1075. FreeLibrary(lib);
  1076. }
  1077.  
  1078. delete[] CurrentSysProcTimes;
  1079. delete[] PreviousSysProcTimes;
  1080.  
  1081. KillTimer(hwnd, TIMER_PROCESSES);
  1082. HIMAGELIST hImageList = ListView_GetImageList(hListViewProcesses, LVSIL_SMALL);
  1083. if (hImageList) ImageList_Destroy(hImageList);
  1084. hImageList = TabCtrl_GetImageList(hTabControl);
  1085. if (hImageList) ImageList_Destroy(hImageList);
  1086. PostQuitMessage(0);
  1087. }
  1088. return 0;
  1089.  
  1090. default:
  1091. return DefWindowProc(hwnd, uMsg, wParam, lParam);
  1092. }
  1093. return 0;
  1094. }
  1095.  
  1096. int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
  1097. _In_ PWSTR pCmdLine, _In_ int nCmdShow)
  1098. {
  1099. WNDCLASS wc = { 0 };
  1100. wc.lpfnWndProc = WindowProc;
  1101. wc.hInstance = hInstance;
  1102. wc.lpszClassName = MainName;
  1103. wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
  1104. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  1105. wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MAINICON));
  1106.  
  1107. RegisterClass(&wc);
  1108.  
  1109. HWND hwnd = CreateWindowEx(0, MainName, MainName, WS_OVERLAPPEDWINDOW,
  1110. CW_USEDEFAULT, CW_USEDEFAULT, SCREEN_WIDTH, SCREEN_HEIGHT,
  1111. NULL, NULL, hInstance, NULL
  1112. );
  1113.  
  1114. if (hwnd == NULL)
  1115. {
  1116. return 0;
  1117. }
  1118.  
  1119. /*LONG style = GetWindowLong(hwnd, GWL_STYLE);
  1120. style &= ~(WS_CAPTION | WS_THICKFRAME);
  1121. SetWindowLong(hwnd, GWL_STYLE, style);
  1122.  
  1123. LONG exStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
  1124. exStyle &= ~(WS_EX_DLGMODALFRAME | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE);
  1125. SetWindowLong(hwnd, GWL_EXSTYLE, exStyle);*/
  1126.  
  1127. MONITORINFO mi = { sizeof(mi) };
  1128. if (GetMonitorInfo(MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST), &mi)) {
  1129. SetWindowPos(hwnd, HWND_TOP,
  1130. mi.rcMonitor.left, mi.rcMonitor.top,
  1131. mi.rcMonitor.right - mi.rcMonitor.left,
  1132. mi.rcMonitor.bottom - mi.rcMonitor.top,
  1133. SWP_NOZORDER | SWP_FRAMECHANGED);
  1134. }
  1135.  
  1136. ShowWindow(hwnd, SW_SHOWMAXIMIZED);
  1137.  
  1138. MSG msg;
  1139. while (GetMessage(&msg, NULL, 0, 0))
  1140. {
  1141. TranslateMessage(&msg);
  1142. DispatchMessage(&msg);
  1143. }
  1144.  
  1145. return 0;
  1146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement