-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathProcessView.cpp
404 lines (367 loc) · 12.9 KB
/
ProcessView.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* Copyright (C) 2012-2025 Stefan-Mihai MOGA
This file is part of IntelliTask application developed by Stefan-Mihai MOGA.
IntelliTask is an alternative Windows version to the famous Task Manager!
IntelliTask is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Open
Source Initiative, either version 3 of the License, or any later version.
IntelliTask is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
IntelliTask. If not, see <http://www.opensource.org/licenses/gpl-3.0.html>*/
// ProcessView.cpp : implementation file
//
#include "stdafx.h"
#include "IntelliTask.h"
#include "ProcessView.h"
#include "MainFrame.h"
// CProcessView
IMPLEMENT_DYNCREATE(CProcessView, CMFCListView)
CProcessView::CProcessView()
{
m_bInitialized = false;
m_pMainFrame = nullptr;
m_nRefreshTimerID = 0;
GetListCtrl().m_pSystemSnapshot = &m_pSystemSnapshot;
}
CProcessView::~CProcessView()
{
}
BEGIN_MESSAGE_MAP(CProcessView, CMFCListView)
ON_WM_SIZE()
ON_WM_TIMER()
ON_WM_DESTROY()
ON_NOTIFY(NM_DBLCLK, ID_MFCLISTCTRL, OnDblClickEntry)
END_MESSAGE_MAP()
// CProcessView diagnostics
#ifdef _DEBUG
void CProcessView::AssertValid() const
{
CMFCListView::AssertValid();
}
#ifndef _WIN32_WCE
void CProcessView::Dump(CDumpContext& dc) const
{
CMFCListView::Dump(dc);
}
#endif
#endif //_DEBUG
// CProcessView message handlers
void CProcessView::OnInitialUpdate()
{
CMFCListView::OnInitialUpdate();
if (!m_bInitialized)
{
m_bInitialized = true;
GetListCtrl().SetExtendedStyle(GetListCtrl().GetExtendedStyle()
| LVS_EX_DOUBLEBUFFER | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
CRect rectClient;
GetListCtrl().GetClientRect(&rectClient);
const int nProcessID = theApp.GetInt(_T("ProcessID"), PID_COLUMN_LENGTH);
const int nProcessName = theApp.GetInt(_T("ProcessName"), PROCESS_COLUMN_LENGTH);
const int nCPU_Usage = theApp.GetInt(_T("CPU_Usage"), CPU_USAGE_COLUMN_LENGTH);
const int nMEM_Usage = theApp.GetInt(_T("MEM_Usage"), MEM_USAGE_COLUMN_LENGTH);
const int nCompany = theApp.GetInt(_T("Company"), COMPANY_COLUMN_LENGTH);
const int nVersion = theApp.GetInt(_T("Version"), VERSION_COLUMN_LENGTH);
// theApp.GetInt(_T("Description"), DESCRIPTION_COLUMN_SIZE)
const int nDescription = rectClient.Width() - (nProcessID + nProcessName + nCPU_Usage + nMEM_Usage + nCompany + nVersion);
GetListCtrl().InsertColumn(0, _T("PID"), LVCFMT_LEFT, nProcessID);
GetListCtrl().InsertColumn(1, _T("Process"), LVCFMT_LEFT, nProcessName);
GetListCtrl().InsertColumn(2, _T("CPU Usage"), LVCFMT_CENTER, nCPU_Usage);
GetListCtrl().InsertColumn(3, _T("Mem Usage"), LVCFMT_RIGHT, nMEM_Usage);
GetListCtrl().InsertColumn(4, _T("Description"), LVCFMT_LEFT, nDescription);
GetListCtrl().InsertColumn(5, _T("Company"), LVCFMT_LEFT, nCompany);
GetListCtrl().InsertColumn(6, _T("Version"), LVCFMT_LEFT, nVersion);
VERIFY(Refresh());
m_nRefreshTimerID = (UINT)SetTimer(1, 1000, nullptr);
}
}
void CProcessView::OnDestroy()
{
VERIFY(KillTimer(m_nRefreshTimerID));
CMFCListView::OnDestroy();
}
void CProcessView::OnSize(UINT nType, int cx, int cy)
{
CMFCListView::OnSize(nType, cx, cy);
ResizeListCtrl();
}
void CProcessView::OnTimer(UINT_PTR nIDEvent)
{
CString strListItem;
HANDLE hSnapshot = nullptr;
PROCESSENTRY32 pe32 = { 0 };
CUIntArray arrProcessID;
if (nIDEvent == m_nRefreshTimerID)
{
KillTimer(m_nRefreshTimerID);
const int nOldListItem = GetListCtrl().GetNextItem(-1, LVIS_SELECTED | LVIS_FOCUSED);
GetListCtrl().SetRedraw(FALSE);
if ((hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)) != INVALID_HANDLE_VALUE)
{
DOUBLE nProcessorTotalUsage = 0.0;
pe32.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(hSnapshot, &pe32))
{
do
{
arrProcessID.Add(pe32.th32ProcessID);
CProcessData* pProcessData = m_pSystemSnapshot.UpdateProcess(pe32.th32ProcessID);
if (pProcessData != nullptr)
{
const int nCount = GetListCtrl().GetItemCount();
for (int nListItem = 0; nListItem < nCount; nListItem++)
{
const DWORD nProcessID = (DWORD)GetListCtrl().GetItemData(nListItem);
if ((nProcessID != 0) && (nProcessID == pe32.th32ProcessID))
{
nProcessorTotalUsage += pProcessData->GetProcessorUsage();
strListItem.Format(_T("%.2f%%"), pProcessData->GetProcessorUsage());
GetListCtrl().SetItemText(nListItem, 2, strListItem);
GetListCtrl().SetItemText(nListItem, 3, FormatSize(pProcessData->GetMemoryUsage()));
break;
}
}
}
else // new process
{
pProcessData = m_pSystemSnapshot.InsertProcess(pe32);
if (pProcessData != nullptr)
{
strListItem.Format(_T("%d"), pProcessData->GetProcessID());
const int nNewListItem = GetListCtrl().InsertItem(GetListCtrl().GetItemCount(), strListItem);
GetListCtrl().SetItemText(nNewListItem, 1, pProcessData->GetFileName());
nProcessorTotalUsage += pProcessData->GetProcessorUsage();
strListItem.Format(_T("%.2f%%"), pProcessData->GetProcessorUsage());
GetListCtrl().SetItemText(nNewListItem, 2, strListItem);
GetListCtrl().SetItemText(nNewListItem, 3, FormatSize(pProcessData->GetMemoryUsage()));
GetListCtrl().SetItemText(nNewListItem, 4, pProcessData->GetDescription());
GetListCtrl().SetItemText(nNewListItem, 5, pProcessData->GetCompany());
GetListCtrl().SetItemText(nNewListItem, 6, pProcessData->GetVersion());
GetListCtrl().SetItemData(nNewListItem, pProcessData->GetProcessID());
}
}
} while (Process32Next(hSnapshot, &pe32));
}
VERIFY(CloseHandle(hSnapshot));
// TRACE(_T("nProcessorTotalUsage = %.2f%%\n"), nProcessorTotalUsage);
const int nCount = GetListCtrl().GetItemCount();
for (int nListItem = 0; nListItem < nCount; nListItem++)
{
const DWORD nProcessID = (DWORD)GetListCtrl().GetItemData(nListItem);
if (nProcessID == 0)
{
DOUBLE nIdleUsage = 100.0 - nProcessorTotalUsage;
strListItem.Format(_T("%.2f%%"), nIdleUsage);
GetListCtrl().SetItemText(nListItem, 2, strListItem);
CProcessData* pProcessData = m_pSystemSnapshot.GetProcessID(0);
if (pProcessData != nullptr)
{
pProcessData->SetProcessorUsage(nIdleUsage);
}
}
}
}
for (int nOldIndex = 0; nOldIndex < GetListCtrl().GetItemCount(); nOldIndex++)
{
bool bFound = false;
const DWORD nOldProcessID = (DWORD)GetListCtrl().GetItemData(nOldIndex);
for (int nNewIndex = 0; nNewIndex < arrProcessID.GetCount(); nNewIndex++)
{
const DWORD nNewProcessID = arrProcessID.GetAt(nNewIndex);
if (nOldProcessID == nNewProcessID)
{
bFound = true;
break;
}
}
if (!bFound)
{
GetListCtrl().DeleteItem(nOldIndex); // remove process
VERIFY(m_pSystemSnapshot.RemoveProcess(nOldProcessID));
nOldIndex--;
}
}
arrProcessID.RemoveAll();
const int nSortColumn = GetListCtrl().GetHeaderCtrl().GetSortColumn();
const bool bIsAscending = GetListCtrl().GetHeaderCtrl().IsAscending();
GetListCtrl().Sort(nSortColumn, bIsAscending, FALSE);
GetListCtrl().SetItemState(nOldListItem, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
GetListCtrl().SetRedraw(TRUE);
GetListCtrl().UpdateWindow();
ResizeListCtrl();
m_nRefreshTimerID = (UINT)SetTimer(1, 1000, nullptr);
}
CMFCListView::OnTimer(nIDEvent);
}
void CProcessView::OnDblClickEntry(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMITEMACTIVATE pItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
if (pResult != nullptr) *pResult = 0;
if (pItemActivate->iItem != -1)
{
DoubleClickEntry(pItemActivate->iItem);
}
}
void CProcessView::ResizeListCtrl()
{
HDITEM hdItem = { 0 };
if (GetListCtrl().GetSafeHwnd() != nullptr)
{
CRect rectClient;
GetListCtrl().GetClientRect(&rectClient);
CMFCHeaderCtrl& pHeaderCtrl = GetListCtrl().GetHeaderCtrl();
hdItem.mask = HDI_WIDTH;
if (pHeaderCtrl.GetItem(0, &hdItem))
{
const int nProcessID = hdItem.cxy;
theApp.WriteInt(_T("ProcessID"), nProcessID);
hdItem.mask = HDI_WIDTH;
if (pHeaderCtrl.GetItem(1, &hdItem))
{
const int nProcessName = hdItem.cxy;
theApp.WriteInt(_T("ProcessName"), nProcessName);
hdItem.mask = HDI_WIDTH;
if (pHeaderCtrl.GetItem(2, &hdItem))
{
const int nCPU_Usage = hdItem.cxy;
theApp.WriteInt(_T("CPU_Usage"), nCPU_Usage);
hdItem.mask = HDI_WIDTH;
if (pHeaderCtrl.GetItem(3, &hdItem))
{
const int nMEM_Usage = hdItem.cxy;
theApp.WriteInt(_T("MEM_Usage"), nMEM_Usage);
hdItem.mask = HDI_WIDTH;
if (pHeaderCtrl.GetItem(4, &hdItem))
{
theApp.WriteInt(_T("Description"), hdItem.cxy);
hdItem.mask = HDI_WIDTH;
if (pHeaderCtrl.GetItem(5, &hdItem))
{
const int nCompany = hdItem.cxy;
theApp.WriteInt(_T("Company"), nCompany);
hdItem.mask = HDI_WIDTH;
if (pHeaderCtrl.GetItem(6, &hdItem))
{
const int nVersion = hdItem.cxy;
theApp.WriteInt(_T("Version"), nVersion);
const int nDescription = rectClient.Width() - (nProcessID + nProcessName + nCPU_Usage + nMEM_Usage + nCompany + nVersion);
if (pHeaderCtrl.GetItem(4, &hdItem))
{
hdItem.cxy = nDescription;
if (pHeaderCtrl.SetItem(4, &hdItem))
{
GetListCtrl().Invalidate();
GetListCtrl().UpdateWindow();
}
}
}
}
}
}
}
}
}
}
}
void CProcessView::DoubleClickEntry(int nIndex)
{
ASSERT(GetListCtrl().m_hWnd != nullptr);
CProcessData* pProcessData = m_pSystemSnapshot.GetProcessID((int)GetListCtrl().GetItemData(nIndex));
ASSERT(pProcessData != nullptr);
CString sPathName{ pProcessData->GetFilePath() };
if (!sPathName.IsEmpty())
{
TRACE(sPathName);
SHELLEXECUTEINFO sei;
memset(&sei, 0, sizeof(sei));
sei.cbSize = sizeof(sei);
sei.hwnd = AfxGetMainWnd()->GetSafeHwnd();
sei.nShow = SW_SHOW;
sei.lpFile = sPathName.GetBuffer(sPathName.GetLength());
sei.lpVerb = _T("properties");
sei.fMask = SEE_MASK_INVOKEIDLIST;
#pragma warning(suppress: 26486)
ShellExecuteEx(&sei);
sPathName.ReleaseBuffer();
}
}
CString CProcessView::FormatSize(ULONGLONG nFormatSize)
{
CString strFormatSize;
ULONGLONG nFormatRest = 0;
if (nFormatSize < 1024)
{
strFormatSize.Format(_T("%d"), (int)nFormatSize);
}
else
{
nFormatRest = nFormatSize % 1024;
nFormatSize = nFormatSize / 1024;
if (nFormatSize < 1024)
{
if (nFormatRest != 0) nFormatSize++;
strFormatSize.Format(_T("%d KB"), (int)nFormatSize);
}
else
{
nFormatRest = nFormatSize % 1024;
nFormatSize = nFormatSize / 1024;
if (nFormatSize < 1024)
{
if (nFormatRest != 0) nFormatSize++;
strFormatSize.Format(_T("%d MB"), (int)nFormatSize);
}
else
{
nFormatRest = nFormatSize % 1024;
nFormatSize = nFormatSize / 1024;
if (nFormatSize < 1024)
{
if (nFormatRest != 0) nFormatSize++;
strFormatSize.Format(_T("%d GB"), (int)nFormatSize);
}
else
{
nFormatRest = nFormatSize % 1024;
nFormatSize = nFormatSize / 1024;
if (nFormatRest != 0) nFormatSize++;
strFormatSize.Format(_T("%d TB"), (int)nFormatSize);
}
}
}
}
return strFormatSize;
}
bool CProcessView::Refresh()
{
CString strListItem;
GetListCtrl().SetRedraw(FALSE);
int nOldListItem = GetListCtrl().GetNextItem(-1, LVIS_SELECTED | LVIS_FOCUSED);
VERIFY(GetListCtrl().DeleteAllItems());
bool bRetVal = m_pSystemSnapshot.Refresh();
const int nSize = m_pSystemSnapshot.GetSize();
for (int nIndex = 0; nIndex < nSize; nIndex++)
{
CProcessData* pProcessData = m_pSystemSnapshot.GetAt(nIndex);
ASSERT(pProcessData != nullptr);
strListItem.Format(_T("%d"), pProcessData->GetProcessID());
const int nNewListItem = GetListCtrl().InsertItem(GetListCtrl().GetItemCount(), strListItem);
GetListCtrl().SetItemText(nNewListItem, 1, pProcessData->GetFileName());
strListItem.Format(_T("%.2f%%"), pProcessData->GetProcessorUsage());
GetListCtrl().SetItemText(nNewListItem, 2, strListItem);
GetListCtrl().SetItemText(nNewListItem, 3, FormatSize(pProcessData->GetMemoryUsage()));
GetListCtrl().SetItemText(nNewListItem, 4, pProcessData->GetDescription());
GetListCtrl().SetItemText(nNewListItem, 5, pProcessData->GetCompany());
GetListCtrl().SetItemText(nNewListItem, 6, pProcessData->GetVersion());
GetListCtrl().SetItemData(nNewListItem, pProcessData->GetProcessID());
}
// const int nSortColumn = GetListCtrl().GetHeaderCtrl().GetSortColumn();
// const bool bIsAscending = GetListCtrl().GetHeaderCtrl().IsAscending();
GetListCtrl().Sort(1, true, false);
GetListCtrl().SetItemState(nOldListItem, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
GetListCtrl().SetRedraw(true);
GetListCtrl().UpdateWindow();
ResizeListCtrl();
return bRetVal;
}