Skip to content

Commit

Permalink
Make topmost positioning optional / Format memory usage figures with …
Browse files Browse the repository at this point in the history
…digits grouped per thousand / Consolidate child dialog visibility and position control in WM_WINDOWPOSCHANGED / Set ThreadPack pointer early in WM_INITDIALOG to save some nullptr checks / g_bThreadEnd deserves a volatile / Fix idle thread joining upon WM_DESTROY
  • Loading branch information
datadiode committed Nov 20, 2022
1 parent 7297567 commit 83a5ba4
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 64 deletions.
102 changes: 65 additions & 37 deletions itaskmgr_src/ITaskMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ INT_PTR CALLBACK DlgProcTask(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam);
INT_PTR CALLBACK DlgProcInfo(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam);

#ifdef _WIN32_WCE
#define MAIN_DLG_CX 240
#define MAIN_DLG_CX 315
#else
#define MAIN_DLG_CX 420
#endif
Expand All @@ -27,7 +27,7 @@ static DWORD CALLBACK thIdle(LPVOID pvParams);
static DWORD GetThreadTick(FILETIME* a, FILETIME* b);

HINSTANCE g_hInst;
BOOL g_bThreadEnd;
BOOL volatile g_bThreadEnd;

//-----------------------------------------------------------------------------
// WinMain entry point
Expand All @@ -51,8 +51,7 @@ int WINAPI _tWinMain( HINSTANCE hInstance,

if( GetLastError() == ERROR_ALREADY_EXISTS )
{
HWND hwndPrev = FindWindow( L"Dialog" , APPNAME );
if( hwndPrev )
if (HWND hwndPrev = FindWindow(WC_DIALOG, APPNAME))
{
ShowWindow(hwndPrev, SW_SHOWNORMAL);
SetForegroundWindow(hwndPrev);
Expand Down Expand Up @@ -134,21 +133,31 @@ static INT_PTR CALLBACK DlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lPara

SetTimer(hDlg, 1, pTP->dwInterval, NULL);

ShowWindow(pTP->hwndCpupower, SW_SHOWNORMAL);

RECT rcWorkArea;
UINT uFlags = SystemParametersInfo(SPI_GETWORKAREA, 0, &rcWorkArea, FALSE) ? 0 : SWP_NOMOVE | SWP_NOSIZE;

SetWindowPos(hDlg, HWND_TOPMOST
, (rcWorkArea.right / 2) - (MAIN_DLG_CX / 2)
, 0
, MAIN_DLG_CX
, (rcWorkArea.bottom > 240) ? 240 : rcWorkArea.bottom
, 0);
if (SystemParametersInfo(SPI_GETWORKAREA, 0, &rcWorkArea, FALSE))
{
SetWindowPos(hDlg, NULL
, (rcWorkArea.right / 2) - (MAIN_DLG_CX / 2)
, 0
, MAIN_DLG_CX
, (rcWorkArea.bottom > 240) ? 240 : rcWorkArea.bottom
, SWP_NOZORDER);
}

return TRUE;
}

// ----------------------------------------------------------
case WM_COMMAND:
switch (wParam)
{
case IDC_STAY_ON_TOP:
SetWindowPos(hDlg, IsDlgButtonChecked(hDlg, IDC_STAY_ON_TOP) ?
HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
break;
}
return TRUE;

// ----------------------------------------------------------
case WM_NOTIFY:

Expand All @@ -159,35 +168,49 @@ static INT_PTR CALLBACK DlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lPara
&& (lpnmhdr->code == TCN_SELCHANGE))
{
pTP->nMode = TabCtrl_GetCurSel(pTP->hwndTab);
ShowWindow(pTP->hwndCpupower, pTP->nMode == MODE_CPUPOWER ? SW_SHOWNA : SW_HIDE);
ShowWindow(pTP->hwndProcessList, pTP->nMode == MODE_PROCESS ? SW_SHOWNA : SW_HIDE);
ShowWindow(pTP->hwndTaskList, pTP->nMode == MODE_TASKLIST ? SW_SHOWNA : SW_HIDE);
ShowWindow(pTP->hwndInfo, pTP->nMode == MODE_INFO ? SW_SHOWNA : SW_HIDE);
SetWindowPos(hDlg, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
}
break;

// ----------------------------------------------------------
case WM_SIZE:
{
RECT rc = { 0, 0, LOWORD(lParam), HIWORD(lParam) };
TabCtrl_AdjustRect(pTP->hwndTab, FALSE, &rc);
case WM_WINDOWPOSCHANGED:
{
LPWINDOWPOS lpwndpos = (LPWINDOWPOS)lParam;

RECT rc;
GetClientRect(hDlg, &rc);

// Size the tab control to fit the client area.
HDWP hdwp = BeginDeferWindowPos(5);

DeferWindowPos(hdwp, pTP->hwndTab, NULL, 0, 0, LOWORD(lParam), HIWORD(lParam), SWP_NOMOVE | SWP_NOZORDER);
HDWP hdwp = BeginDeferWindowPos(6);

DeferWindowPos(hdwp, pTP->hwndProcessList, HWND_TOP,
rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, 0);
UINT flags = lpwndpos->flags & SWP_NOSIZE ? SWP_NOMOVE | SWP_NOSIZE : 0;
if (flags == 0)
{
RECT rcTab;
GetWindowRect(pTP->hwndTab, &rcTab);
MapWindowPoints(pTP->hwndStayOnTop, hDlg, (LPPOINT)&rcTab, 1);
DeferWindowPos(hdwp, pTP->hwndStayOnTop, NULL,
rcTab.left + rc.right - rcTab.right, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
DeferWindowPos(hdwp, pTP->hwndTab, NULL,
rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_NOMOVE | SWP_NOZORDER);
TabCtrl_AdjustRect(pTP->hwndTab, FALSE, &rc);
}

DeferWindowPos(hdwp, pTP->hwndCpupower, HWND_TOP,
rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, 0);
rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
flags | (pTP->nMode == MODE_CPUPOWER ? SWP_SHOWWINDOW : SWP_HIDEWINDOW));

DeferWindowPos(hdwp, pTP->hwndProcessList, HWND_TOP,
rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
flags | (pTP->nMode == MODE_PROCESS ? SWP_SHOWWINDOW : SWP_HIDEWINDOW));

DeferWindowPos(hdwp, pTP->hwndTaskList, HWND_TOP,
rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, 0);
rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
flags | (pTP->nMode == MODE_TASKLIST ? SWP_SHOWWINDOW : SWP_HIDEWINDOW));

DeferWindowPos(hdwp, pTP->hwndInfo, HWND_TOP,
rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, 0);
rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
flags | (pTP->nMode == MODE_INFO ? SWP_SHOWWINDOW : SWP_HIDEWINDOW));

EndDeferWindowPos(hdwp);
}
Expand Down Expand Up @@ -254,7 +277,7 @@ static INT_PTR CALLBACK DlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lPara
default:
break;
}
return 0;
break;

// ----------------------------------------------------------
case WM_CLOSE:
Expand All @@ -266,15 +289,18 @@ static INT_PTR CALLBACK DlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lPara
KillTimer(hDlg, 1);
if (*shellapi.Shell_NotifyIcon)
(*shellapi.Shell_NotifyIcon)(NIM_DELETE, &pTP->nidTrayIcon);
g_bThreadEnd/*pTP->bEnd*/ = TRUE;
ResumeThread(pTP->hIdleThread);
WaitForSingleObject(pTP->hIdleThread, 3000);

if(pTP)

if (pTP)
{
g_bThreadEnd = TRUE;
for (DWORD i = 0; i < pTP->si.dwNumberOfProcessors; ++i)
{
ResumeThread(pTP->hIdleThread[i]);
WaitForSingleObject(pTP->hIdleThread[i], 3000);
}
LocalFree(pTP);
}
return 0;
break;
}

return FALSE;
Expand All @@ -291,6 +317,8 @@ static BOOL CreateTab(ThreadPack* pTP)
if( hwndTab == NULL )
return FALSE;

pTP->hwndStayOnTop = GetDlgItem(pTP->hDlg, IDC_STAY_ON_TOP);

TCITEM tie;

tie.mask = TCIF_TEXT | TCIF_IMAGE;
Expand Down
1 change: 1 addition & 0 deletions itaskmgr_src/ITaskMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ typedef struct _ThreadPack
HANDLE hIdleThread[CPUCORE_MAX];

HWND hwndTab;
HWND hwndStayOnTop;
HWND hwndProcessList;
HWND hwndCpupower;
HWND hwndTaskList;
Expand Down
13 changes: 7 additions & 6 deletions itaskmgr_src/ITaskMgr.rc
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ EXSTYLE WS_EX_CONTEXTHELP
CAPTION "ITaskMgr"
FONT 9, "Tahoma"
BEGIN
CONTROL "Tab1",IDC_TAB,"SysTabControl32",WS_TABSTOP,0,0,100,60
AUTOCHECKBOX "Stay on top",IDC_STAY_ON_TOP,48,0,95,12,WS_GROUP
CONTROL "Tab1",IDC_TAB,"SysTabControl32",WS_GROUP | WS_TABSTOP,0,0,100,60
END

IDD_PROCESS_LIST DIALOG DISCARDABLE 0, 0, 400, 300
STYLE WS_CHILD
STYLE WS_CHILD | DS_CONTROL
FONT 9, "Tahoma"
BEGIN
CONTROL "List1",IDC_LV_PROCESS,"SysListView32",LVS_REPORT |
Expand All @@ -59,7 +60,7 @@ BEGIN
END

IDD_CPU DIALOG DISCARDABLE 0, 0, 400, 300
STYLE WS_CHILD
STYLE WS_CHILD | DS_CONTROL
FONT 9, "Tahoma"
BEGIN
CONTROL "",IDC_CPU_DRAW,"Button",BS_OWNERDRAW | WS_DISABLED,5,5,
Expand All @@ -82,7 +83,7 @@ BEGIN
END

IDD_TASK_LIST DIALOG DISCARDABLE 0, 0, 400, 300
STYLE WS_CHILD
STYLE WS_CHILD | DS_CONTROL
FONT 9, "Tahoma"
BEGIN
CONTROL "List1",IDC_LV_TASKLIST,"SysListView32",LVS_REPORT |
Expand All @@ -93,10 +94,10 @@ BEGIN
END

IDD_SYSTEM_INFO DIALOG DISCARDABLE 0, 0, 400, 300
STYLE WS_CHILD
STYLE WS_CHILD | DS_CONTROL
FONT 9, "Tahoma"
BEGIN
EDITTEXT IDC_INFO_TEXT, 0, 0, 150, 105, ES_MULTILINE | ES_READONLY | NOT WS_TABSTOP
EDITTEXT IDC_INFO_TEXT, 0, 0, 150, 105, ES_MULTILINE | ES_READONLY | NOT WS_TABSTOP
END

/////////////////////////////////////////////////////////////////////////////
Expand Down
5 changes: 4 additions & 1 deletion itaskmgr_src/StdAfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@
#define Heap32First(hSnapshot, lphe, th32ProcessID, th32HeapID) Heap32First(lphe, th32ProcessID, th32HeapID)
#define Heap32Next(hSnapshot, lphe) Heap32Next(lphe)
#define CeSetThreadAffinity(hThread, dwProcessor) SetThreadAffinityMask(hThread, dwProcessor)
#elif _WIN32_WCE <= 0x600
#else
#define WC_DIALOG L"Dialog"
#if _WIN32_WCE <= 0x600
#define CeSetThreadAffinity(hThread, dwProcessor) (FALSE)
#endif
#endif

template<typename f>
struct DllImport {
Expand Down
14 changes: 7 additions & 7 deletions itaskmgr_src/cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ INT_PTR CALLBACK DlgProcCpu(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
// ----------------------------------------------------------
case WM_SIZE:
{
if( pTP == NULL )
return 0;

HWND hwndDraw = GetDlgItem(hDlg, IDC_CPU_DRAW);
HWND hwndText = GetDlgItem(hDlg, IDC_CPU_TEXT);

Expand Down Expand Up @@ -121,7 +118,8 @@ static BOOL ShowCpuStatus(ThreadPack* pTP)
return FALSE;

MEMORYSTATUS ms;
TCHAR szFmt[128], *pszFmt = szFmt;
TCHAR szTmp[64];
TCHAR szFmt[1024], *pszFmt = szFmt;

HWND hDlg;
HWND hwndStatus;
Expand All @@ -141,9 +139,11 @@ static BOOL ShowCpuStatus(ThreadPack* pTP)
for (DWORD i = 0; i < pTP->si.dwNumberOfProcessors; ++i)
pszFmt += wsprintf(pszFmt, _T("\t%d%%"), pTP->chPowHistory[0][i]);

pszFmt += wsprintf(pszFmt
, _T("\r\nMemory used\t%uKB/%uKB")
, dwUsedMem, dwTotalMem);
pszFmt += wsprintf(pszFmt, _T("\r\nMemory used\t"));
GetNumberFormat(LOCALE_INVARIANT, 0, szTmp, NULL, pszFmt, wsprintf(szTmp, _T("%u"), dwUsedMem) + 10);
pszFmt += wsprintf(pszFmt = _tcschr(pszFmt, '.'), _T(" KB / "));
GetNumberFormat(LOCALE_INVARIANT, 0, szTmp, NULL, pszFmt, wsprintf(szTmp, _T("%u"), dwTotalMem) + 10);
pszFmt += wsprintf(pszFmt = _tcschr(pszFmt, '.'), _T(" KB"));

SetWindowText(hwndStatus, szFmt);
return TRUE;
Expand Down
8 changes: 2 additions & 6 deletions itaskmgr_src/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,19 @@ INT_PTR CALLBACK DlgProcProcess(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lPara
// ----------------------------------------------------------
case WM_INITDIALOG:
{
pTP = (ThreadPack*)lParam;

HWND hwndLView = GetDlgItem(hDlg, IDC_LV_PROCESS);
InitProcessListViewColumns(hwndLView);
DrawProcessView(hwndLView);

ListView_SetExtendedListViewStyle(hwndLView, LVS_EX_FULLROWSELECT);

pTP = (ThreadPack*)lParam;
return TRUE;
}

// ----------------------------------------------------------
case WM_NOTIFY:
if( pTP == NULL )
break;

LPNMHDR lpnmhdr;
lpnmhdr = (LPNMHDR)lParam;

Expand Down Expand Up @@ -66,8 +64,6 @@ INT_PTR CALLBACK DlgProcProcess(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lPara
// ----------------------------------------------------------
case WM_SIZE:
{
if( pTP == NULL )
return 0;
ResizeWindow(hDlg, lParam);
return 0;
}
Expand Down
1 change: 1 addition & 0 deletions itaskmgr_src/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#define IDC_LV_PROCESS 1001
#define IDC_GRAPH 1003
#define IDC_TAB 1007
#define IDC_STAY_ON_TOP 1008
#define IDC_SCROLLBAR 1009
#define IDC_TERMINATE 1010
#define IDC_CPU_DRAW 1011
Expand Down
9 changes: 2 additions & 7 deletions itaskmgr_src/tasklist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ INT_PTR CALLBACK DlgProcTask(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
// ----------------------------------------------------------
case WM_INITDIALOG:
{
pTP = (ThreadPack*)lParam;

g_himlIcons = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), ILC_MASK, 1, 1);
if( !g_himlIcons )
return FALSE;
Expand All @@ -46,16 +48,12 @@ INT_PTR CALLBACK DlgProcTask(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
ListView_SetExtendedListViewStyle(hwndLView, LVS_EX_FULLROWSELECT);
InitTaskListViewColumns(hwndLView);
DrawTaskView(hwndLView);
pTP = (ThreadPack*)lParam;
return TRUE;
}

// ----------------------------------------------------------
case WM_NOTIFY:
{
if( pTP == NULL )
break;

LPNMHDR lpnmhdr;
lpnmhdr = (LPNMHDR)lParam;
HWND hwndTaskList = GetDlgItem(hDlg, IDC_LV_TASKLIST);
Expand Down Expand Up @@ -131,9 +129,6 @@ INT_PTR CALLBACK DlgProcTask(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
// ----------------------------------------------------------
case WM_SIZE:
{
if( pTP == NULL )
return 0;

ResizeWindow(hDlg, lParam);
return 0;
}
Expand Down

0 comments on commit 83a5ba4

Please sign in to comment.