From 73a99426467783298a0a2cea8eeb5d12a324d881 Mon Sep 17 00:00:00 2001 From: datadiode Date: Tue, 22 Nov 2022 08:33:42 +0100 Subject: [PATCH] Add a Threads page / Make window resizable / offer a subtle maximize/restore like function through double-click on "Stay on top" (because WS_EX_CONTEXTHELP cannot coexist with WS_MAXIMIZEBOX) / Consolidate excess ListView item removal in DeleteExcessItemsLParam() / Some other rework --- itaskmgr_src/ITaskMgr.cpp | 141 +++++++++++------ itaskmgr_src/ITaskMgr.h | 16 +- itaskmgr_src/ITaskMgr.rc | 13 +- itaskmgr_src/ITaskMgr.vcxproj | 1 + itaskmgr_src/ITaskMgr_vc9.vcproj | 4 + itaskmgr_src/StdAfx.h | 4 + itaskmgr_src/cpu.cpp | 17 +-- itaskmgr_src/process.cpp | 238 +++++++++++------------------ itaskmgr_src/resource.h | 2 + itaskmgr_src/tasklist.cpp | 249 +++++++++--------------------- itaskmgr_src/thread.cpp | 254 +++++++++++++++++++++++++++++++ 11 files changed, 553 insertions(+), 386 deletions(-) create mode 100644 itaskmgr_src/thread.cpp diff --git a/itaskmgr_src/ITaskMgr.cpp b/itaskmgr_src/ITaskMgr.cpp index 02ae4e1..b17f3d0 100644 --- a/itaskmgr_src/ITaskMgr.cpp +++ b/itaskmgr_src/ITaskMgr.cpp @@ -6,6 +6,7 @@ INT_PTR CALLBACK DlgProcCpu(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam); INT_PTR CALLBACK DlgProcProcess(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam); +INT_PTR CALLBACK DlgProcThread(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam); INT_PTR CALLBACK DlgProcTask(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam); INT_PTR CALLBACK DlgProcInfo(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam); @@ -26,7 +27,6 @@ static void Measure(ThreadPack* pTP); static DWORD CALLBACK thIdle(LPVOID pvParams); static DWORD GetThreadTick(FILETIME* a, FILETIME* b); -HINSTANCE g_hInst; BOOL volatile g_bThreadEnd; //----------------------------------------------------------------------------- @@ -44,8 +44,6 @@ int WINAPI _tWinMain( HINSTANCE hInstance, icex.dwICC = ICC_LISTVIEW_CLASSES|ICC_TAB_CLASSES|ICC_LISTVIEW_CLASSES; InitCommonControlsEx(&icex); - g_hInst = hInstance; - //------------ Prevent multiple instance ------------------ HANDLE hMutex = CreateMutex(NULL,FALSE,APPNAME); @@ -59,10 +57,8 @@ int WINAPI _tWinMain( HINSTANCE hInstance, return 0; } - - // g_hInst = hInstance; - DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAINDLG), NULL, DlgProc); - return 0; + DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_MAINDLG), NULL, DlgProc, (LPARAM)hInstance); + return 0; } static struct shellapi { HMODULE h; @@ -83,7 +79,9 @@ static INT_PTR CALLBACK DlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lPara { static ThreadPack* pTP = NULL; LPNMHDR lpnmhdr; - + RECT rcWorkArea; + static UINT topmost = 0; + switch(Msg) { // ---------------------------------------------------------- @@ -99,9 +97,9 @@ static INT_PTR CALLBACK DlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lPara memset(pTP, 0, sizeof(ThreadPack)); pTP->hDlg = hDlg; - g_bThreadEnd/*pTP->bEnd*/ = FALSE; + pTP->dwSelectedProcessID = GetCurrentProcessId(); pTP->dwInterval = 2000; //sec - pTP->g_hInst = g_hInst; + pTP->g_hInst = (HINSTANCE)lParam; memset(pTP->chPowHistory, -1, sizeof pTP->chPowHistory); memset(pTP->chPowHistory, 0, sizeof *pTP->chPowHistory); @@ -119,7 +117,6 @@ static INT_PTR CALLBACK DlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lPara pTP->nMode = MODE_CPUPOWER; - // make tasktray icons pTP->nidTrayIcon.cbSize = sizeof(NOTIFYICONDATA); pTP->nidTrayIcon.hIcon = pTP->hIcon[0]; @@ -133,7 +130,6 @@ static INT_PTR CALLBACK DlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lPara SetTimer(hDlg, 1, pTP->dwInterval, NULL); - RECT rcWorkArea; if (SystemParametersInfo(SPI_GETWORKAREA, 0, &rcWorkArea, FALSE)) { SetWindowPos(hDlg, NULL @@ -151,9 +147,23 @@ static INT_PTR CALLBACK DlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lPara 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); + case MAKEWPARAM(IDC_STAY_ON_TOP, BN_DBLCLK): + if (SystemParametersInfo(SPI_GETWORKAREA, 0, &rcWorkArea, FALSE)) + { + RECT rc; + static RECT rcRestored; + GetWindowRect(hDlg, &rc); + OffsetRect(&rc, rcWorkArea.left - rc.left, rcWorkArea.top - rc.top); + if (EqualRect(&rc, &rcWorkArea) && !IsRectEmpty(&rcRestored)) + rc = rcRestored; + else if (GetWindowRect(hDlg, &rcRestored)) + rc = rcWorkArea; + SetWindowPos(hDlg, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER); + } + // fall through + case MAKEWPARAM(IDC_STAY_ON_TOP, BN_CLICKED): + CheckDlgButton(hDlg, IDC_STAY_ON_TOP, topmost ^= BST_CHECKED); + SetWindowPos(hDlg, topmost ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); break; } return TRUE; @@ -163,14 +173,17 @@ static INT_PTR CALLBACK DlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lPara lpnmhdr = (LPNMHDR)lParam; - if( (lpnmhdr->hwndFrom == pTP->hwndTab) - && (lpnmhdr->idFrom == IDC_TAB) - && (lpnmhdr->code == TCN_SELCHANGE)) + if (lpnmhdr->idFrom == IDC_TAB) { - pTP->nMode = TabCtrl_GetCurSel(pTP->hwndTab); - SetWindowPos(hDlg, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); + switch (lpnmhdr->code) + { + case TCN_SELCHANGE: + pTP->nMode = TabCtrl_GetCurSel(pTP->hwndTab); + SetWindowPos(hDlg, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); + break; + } } - break; + break; // ---------------------------------------------------------- case WM_WINDOWPOSCHANGED: @@ -181,7 +194,7 @@ static INT_PTR CALLBACK DlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lPara GetClientRect(hDlg, &rc); // Size the tab control to fit the client area. - HDWP hdwp = BeginDeferWindowPos(6); + HDWP hdwp = BeginDeferWindowPos(7); UINT flags = lpwndpos->flags & SWP_NOSIZE ? SWP_NOMOVE | SWP_NOSIZE : 0; if (flags == 0) @@ -204,6 +217,10 @@ static INT_PTR CALLBACK DlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lPara rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, flags | (pTP->nMode == MODE_PROCESS ? SWP_SHOWWINDOW : SWP_HIDEWINDOW)); + DeferWindowPos(hdwp, pTP->hwndThreadList, HWND_TOP, + rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, + flags | (pTP->nMode == MODE_THREAD ? SWP_SHOWWINDOW : SWP_HIDEWINDOW)); + DeferWindowPos(hdwp, pTP->hwndTaskList, HWND_TOP, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, flags | (pTP->nMode == MODE_TASKLIST ? SWP_SHOWWINDOW : SWP_HIDEWINDOW)); @@ -238,6 +255,7 @@ static INT_PTR CALLBACK DlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lPara break; } break; + // ---------------------------------------------------------- #ifdef _WIN32_WCE case WM_HELP: @@ -245,7 +263,7 @@ static INT_PTR CALLBACK DlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lPara break; #else case WM_SYSCOMMAND: - switch (wParam) + switch (GET_SC_WPARAM(wParam)) { case SC_CONTEXTHELP: DialogBox(pTP->g_hInst, MAKEINTRESOURCE(IDD_HELP), hDlg, DlgProcHelp); @@ -266,13 +284,16 @@ static INT_PTR CALLBACK DlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lPara case MODE_ICON: break; case MODE_CPUPOWER: - PostMessage(pTP->hwndCpupower, WM_TIMER, wParam, lParam); + SetWindowPos(pTP->hwndCpupower, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); break; case MODE_PROCESS: - PostMessage(pTP->hwndProcessList, WM_TIMER, wParam, lParam); + SetWindowPos(pTP->hwndProcessList, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); + break; + case MODE_THREAD: + SetWindowPos(pTP->hwndThreadList, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); break; case MODE_TASKLIST: - PostMessage(pTP->hwndTaskList, WM_TIMER, wParam, lParam); + SetWindowPos(pTP->hwndTaskList, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); break; default: break; @@ -330,7 +351,7 @@ static BOOL CreateTab(ThreadPack* pTP) if(TabCtrl_InsertItem(hwndTab, MODE_CPUPOWER, &tie) == -1) return FALSE; - HWND const hwndCpupower = CreateDialogParam( pTP->g_hInst, MAKEINTRESOURCE(IDD_CPU), pTP->hDlg, DlgProcCpu, (LPARAM)pTP ); + HWND const hwndCpupower = CreateDialogParam(pTP->g_hInst, MAKEINTRESOURCE(IDD_CPU), pTP->hDlg, DlgProcCpu, (LPARAM)pTP); if( hwndCpupower == NULL ) return FALSE; @@ -341,17 +362,28 @@ static BOOL CreateTab(ThreadPack* pTP) if(TabCtrl_InsertItem(hwndTab, MODE_PROCESS, &tie) == -1) return FALSE; - HWND const hwndProcessList = CreateDialogParam( pTP->g_hInst, MAKEINTRESOURCE(IDD_PROCESS_LIST), pTP->hDlg, DlgProcProcess, (LPARAM)pTP ); + HWND const hwndProcessList = CreateDialogParam(pTP->g_hInst, MAKEINTRESOURCE(IDD_PROCESS_LIST), pTP->hDlg, DlgProcProcess, (LPARAM)pTP); if( hwndProcessList == NULL ) return FALSE; + // ---------------------------------------------------- THREADLIST + tie.pszText = _T("Thread"); + + if(TabCtrl_InsertItem(hwndTab, MODE_THREAD, &tie) == -1) + return FALSE; + + HWND const hwndThreadList = CreateDialogParam(pTP->g_hInst, MAKEINTRESOURCE(IDD_THREAD_LIST), pTP->hDlg, DlgProcThread, (LPARAM)pTP); + + if (hwndThreadList == NULL) + return FALSE; + // ---------------------------------------------------- TASKLIST tie.pszText = _T("Task"); if(TabCtrl_InsertItem(hwndTab, MODE_TASKLIST, &tie) == -1) return FALSE; - HWND const hwndTaskList = CreateDialogParam( pTP->g_hInst, MAKEINTRESOURCE(IDD_TASK_LIST), pTP->hDlg, DlgProcTask, (LPARAM)pTP ); + HWND const hwndTaskList = CreateDialogParam(pTP->g_hInst, MAKEINTRESOURCE(IDD_TASK_LIST), pTP->hDlg, DlgProcTask, (LPARAM)pTP); if( hwndTaskList == NULL ) return FALSE; @@ -368,8 +400,9 @@ static BOOL CreateTab(ThreadPack* pTP) // ---------------------------------------------------- ADD - pTP->hwndProcessList = hwndProcessList; pTP->hwndCpupower = hwndCpupower; + pTP->hwndProcessList = hwndProcessList; + pTP->hwndThreadList = hwndThreadList; pTP->hwndTaskList = hwndTaskList; pTP->hwndInfo = hwndInfo; @@ -460,7 +493,7 @@ static DWORD CALLBACK thIdle(LPVOID pvParams) { ThreadPack* pTP = (ThreadPack*)pvParams; - while(!g_bThreadEnd/*pTP->bEnd*/); + while(!g_bThreadEnd); return 0; } @@ -513,24 +546,15 @@ static INT_PTR CALLBACK DlgProcHelp(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM l //----------------------------------------------------------------------------- LPARAM GetSelectedItemLParam(HWND hwndLView) { - int nIndex = -1; LVITEM lvItem; memset(&lvItem, 0, sizeof(LVITEM)); lvItem.mask = LVIF_PARAM; - - nIndex = ListView_GetNextItem(hwndLView, nIndex, 0); + lvItem.iItem = ListView_GetNextItem(hwndLView, -1, LVNI_SELECTED); - while( nIndex != -1 ) - { - lvItem.iItem = nIndex; - if( ListView_GetItemState(hwndLView, nIndex, LVIS_SELECTED) ) - { - ListView_GetItem(hwndLView, &lvItem); - return lvItem.lParam; - } - nIndex = ListView_GetNextItem(hwndLView, nIndex, 0); - } - return 0; + if (lvItem.iItem != -1) + ListView_GetItem(hwndLView, &lvItem); + + return lvItem.lParam; } //----------------------------------------------------------------------------- @@ -549,11 +573,36 @@ void SelectItemLParam(HWND hwndLView, LPARAM lParam) if (nIndex != -1) { - ListView_SetItemState(hwndLView, nIndex, LVIS_SELECTED, LVIS_SELECTED); + ListView_SetItemState(hwndLView, nIndex, LVIS_FOCUSED | LVIS_SELECTED, LVIS_FOCUSED | LVIS_SELECTED); ListView_EnsureVisible(hwndLView, nIndex, FALSE); } } +//----------------------------------------------------------------------------- +// delete excess items by lParam +//----------------------------------------------------------------------------- +void DeleteExcessItemsLParam(HWND hwndLView, LPARAM* plParam, int n) +{ + LVITEM lvItem; + memset(&lvItem, 0, sizeof lvItem); + lvItem.mask = LVIF_PARAM; + lvItem.iItem = ListView_GetItemCount(hwndLView); + while (lvItem.iItem-- && ListView_GetItem(hwndLView, &lvItem)) + { + int i; + for (i = 0; i < n; ++i) + if (plParam[i] == lvItem.lParam) + break; + if (i == n) + ListView_DeleteItem(hwndLView, lvItem.iItem); + } + + if (GetSelectedItemLParam(hwndLView) == 0) + { + ListView_SetItemState(hwndLView, 0, LVIS_FOCUSED | LVIS_SELECTED, LVIS_FOCUSED | LVIS_SELECTED); + } +} + //----------------------------------------------------------------------------- // create thread(s) //----------------------------------------------------------------------------- diff --git a/itaskmgr_src/ITaskMgr.h b/itaskmgr_src/ITaskMgr.h index 80e9cf8..d253ea0 100644 --- a/itaskmgr_src/ITaskMgr.h +++ b/itaskmgr_src/ITaskMgr.h @@ -4,12 +4,18 @@ #define APPNAME _T("ITaskMgr") #define HISTORY_MAX 512 #define CPUCORE_MAX 8 +#ifdef _WIN32_WCE +#define PROCESS_MAX 256 +#else +#define PROCESS_MAX 16384 +#endif #define MODE_ICON -1 #define MODE_CPUPOWER 0 #define MODE_PROCESS 1 -#define MODE_TASKLIST 2 -#define MODE_INFO 3 +#define MODE_THREAD 2 +#define MODE_TASKLIST 3 +#define MODE_INFO 4 #define MY_NOTIFYICON (WM_APP + 1000) @@ -25,11 +31,14 @@ typedef struct _ThreadPack HWND hwndTab; HWND hwndStayOnTop; - HWND hwndProcessList; HWND hwndCpupower; + HWND hwndProcessList; + HWND hwndThreadList; HWND hwndTaskList; HWND hwndInfo; + DWORD dwSelectedProcessID; + DWORD dwInterval; HICON hIcon[12]; NOTIFYICONDATA nidTrayIcon; @@ -40,5 +49,6 @@ typedef struct _ThreadPack LPARAM GetSelectedItemLParam(HWND hwndLView); void SelectItemLParam(HWND hwndLView, LPARAM lParam); +void DeleteExcessItemsLParam(HWND hwndLView, LPARAM* plParam, int n); #endif // !defined(__ITASKMGR_H__INCLUDED_) diff --git a/itaskmgr_src/ITaskMgr.rc b/itaskmgr_src/ITaskMgr.rc index 9ba0d6c..6b5c691 100644 --- a/itaskmgr_src/ITaskMgr.rc +++ b/itaskmgr_src/ITaskMgr.rc @@ -39,12 +39,12 @@ IDI_DEFAULT_SMALLICON ICON DISCARDABLE "default.ico" // IDD_MAINDLG DIALOG DISCARDABLE 0, 0, 143, 103 -STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +STYLE WS_THICKFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU EXSTYLE WS_EX_CONTEXTHELP CAPTION "ITaskMgr" FONT 9, "Tahoma" BEGIN - AUTOCHECKBOX "Stay on top",IDC_STAY_ON_TOP,48,0,95,12,WS_GROUP + CHECKBOX "Stay on top",IDC_STAY_ON_TOP,48,0,95,12,BS_NOTIFY | WS_GROUP CONTROL "Tab1",IDC_TAB,"SysTabControl32",WS_GROUP | WS_TABSTOP,0,0,100,60 END @@ -59,6 +59,15 @@ BEGIN LTEXT "",IDC_HEAP,5,95,85,15 END +IDD_THREAD_LIST DIALOG DISCARDABLE 0, 0, 400, 300 +STYLE WS_CHILD | DS_CONTROL +FONT 9, "Tahoma" +BEGIN + CONTROL "List1",IDC_LV_THREAD,"SysListView32",LVS_REPORT | + LVS_SINGLESEL | LVS_SHOWSELALWAYS | LVS_NOSORTHEADER | + WS_BORDER | WS_TABSTOP,0,0,150,105 +END + IDD_CPU DIALOG DISCARDABLE 0, 0, 400, 300 STYLE WS_CHILD | DS_CONTROL FONT 9, "Tahoma" diff --git a/itaskmgr_src/ITaskMgr.vcxproj b/itaskmgr_src/ITaskMgr.vcxproj index 6041821..fa96842 100644 --- a/itaskmgr_src/ITaskMgr.vcxproj +++ b/itaskmgr_src/ITaskMgr.vcxproj @@ -342,6 +342,7 @@ + diff --git a/itaskmgr_src/ITaskMgr_vc9.vcproj b/itaskmgr_src/ITaskMgr_vc9.vcproj index 84f8c29..e84c415 100644 --- a/itaskmgr_src/ITaskMgr_vc9.vcproj +++ b/itaskmgr_src/ITaskMgr_vc9.vcproj @@ -402,6 +402,10 @@ RelativePath=".\tasklist.cpp" > + + diff --git a/itaskmgr_src/StdAfx.h b/itaskmgr_src/StdAfx.h index 2ea0a7c..51afd19 100644 --- a/itaskmgr_src/StdAfx.h +++ b/itaskmgr_src/StdAfx.h @@ -24,10 +24,14 @@ #define CloseToolhelp32Snapshot CloseHandle #define Heap32First(hSnapshot, lphe, th32ProcessID, th32HeapID) Heap32First(lphe, th32ProcessID, th32HeapID) #define Heap32Next(hSnapshot, lphe) Heap32Next(lphe) +#define CeGetProcessAffinity(hThread, lpProcessAffinity) (FALSE) +#define CeGetThreadAffinity(hThread, lpProcessAffinity) (FALSE) #define CeSetThreadAffinity(hThread, dwProcessor) SetThreadAffinityMask(hThread, dwProcessor) #else #define WC_DIALOG L"Dialog" #if _WIN32_WCE <= 0x600 +#define CeGetProcessAffinity(hThread, lpProcessAffinity) (FALSE) +#define CeGetThreadAffinity(hThread, lpProcessAffinity) (FALSE) #define CeSetThreadAffinity(hThread, dwProcessor) (FALSE) #endif #endif diff --git a/itaskmgr_src/cpu.cpp b/itaskmgr_src/cpu.cpp index a04617c..b26b83e 100644 --- a/itaskmgr_src/cpu.cpp +++ b/itaskmgr_src/cpu.cpp @@ -29,13 +29,9 @@ INT_PTR CALLBACK DlgProcCpu(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam) hpenMagenta = CreatePen(PS_SOLID, 1, RGB(255, 0, 255)); hpenYellow = CreatePen(PS_SOLID, 1, RGB(255, 255, 0)); - HWND hwndText = GetDlgItem(hDlg, IDC_CPU_TEXT); static UINT const tabstop = 20; - SendMessage(hwndText, EM_SETTABSTOPS, 1, (LPARAM)&tabstop); + SendDlgItemMessage(hDlg, IDC_CPU_TEXT, EM_SETTABSTOPS, 1, (LPARAM)&tabstop); - HWND hwndDraw = GetDlgItem(hDlg, IDC_CPU_DRAW); - DrawGraph(pTP, hwndDraw); - ShowCpuStatus(pTP); return TRUE; } @@ -94,12 +90,13 @@ INT_PTR CALLBACK DlgProcCpu(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam) return TRUE; } - case WM_TIMER: - { - ShowCpuStatus(pTP); - InvalidateRect(GetDlgItem(hDlg, IDC_CPU_DRAW), NULL, FALSE); + case WM_WINDOWPOSCHANGED: + if (((LPWINDOWPOS)lParam)->flags & (SWP_SHOWWINDOW | SWP_FRAMECHANGED)) + { + ShowCpuStatus(pTP); + InvalidateRect(GetDlgItem(hDlg, IDC_CPU_DRAW), NULL, FALSE); + } break; - } default: break; diff --git a/itaskmgr_src/process.cpp b/itaskmgr_src/process.cpp index 8367fdc..95cc57a 100644 --- a/itaskmgr_src/process.cpp +++ b/itaskmgr_src/process.cpp @@ -2,8 +2,7 @@ #include "ITaskMgr.h" static SIZE_T CalcHeapOfProcess(DWORD dwProcessID); -static BOOL DeleteProcessItem(HWND hwndLView, DWORD* pdwProcessIDs); -static BOOL InitProcessListViewColumns(HWND hwndLView); +static void InitProcessListViewColumns(HWND hwndLView); static BOOL InsertProcessItem(HWND hwndLView, PROCESSENTRY32* ppe32); static void KillSelectedProcess(HWND hwndLView); static BOOL DrawProcessView(HWND hwndLView); @@ -21,75 +20,73 @@ 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); - + if (HWND hwndLView = GetDlgItem(hDlg, IDC_LV_PROCESS)) + { + ListView_SetExtendedListViewStyle(hwndLView, LVS_EX_FULLROWSELECT); + InitProcessListViewColumns(hwndLView); + } return TRUE; - } // ---------------------------------------------------------- case WM_NOTIFY: - LPNMHDR lpnmhdr; - lpnmhdr = (LPNMHDR)lParam; - - if( (lpnmhdr->hwndFrom == hDlg) - && (lpnmhdr->idFrom == IDC_LV_PROCESS) ) + if (((LPNMHDR)lParam)->idFrom == IDC_LV_PROCESS) { - switch( lpnmhdr->code ) + LPNMLISTVIEW lpnmlv = (LPNMLISTVIEW)lParam; + switch (lpnmlv->hdr.code) { - case 0: - default: + case LVN_ITEMCHANGED: + if ((lpnmlv->uChanged & LVIF_STATE) && + (lpnmlv->uNewState & LVIS_SELECTED) > (lpnmlv->uOldState & LVIS_SELECTED)) + { + pTP->dwSelectedProcessID = (DWORD)lpnmlv->lParam; + } + break; + case NM_DBLCLK: + TabCtrl_SetCurSel(pTP->hwndTab, MODE_THREAD); + NMHDR nmh = { pTP->hwndTab, IDC_TAB, TCN_SELCHANGE }; + SendMessage(pTP->hDlg, WM_NOTIFY, 0, (LPARAM)&nmh); + SetFocus(GetNextDlgTabItem(pTP->hwndThreadList, NULL, FALSE)); break; } } - break; + break; // ---------------------------------------------------------- case WM_COMMAND: - { switch( LOWORD(wParam) ) { case IDC_TERMINATE: HWND hwndLView = GetDlgItem(hDlg, IDC_LV_PROCESS); KillSelectedProcess(hwndLView); } - return 0; - } + break; + // ---------------------------------------------------------- case WM_SIZE: - { ResizeWindow(hDlg, lParam); - return 0; - } + break; // ---------------------------------------------------------- - case WM_TIMER: - { - HWND hwndLView = GetDlgItem(hDlg, IDC_LV_PROCESS); - HWND hwndHeap = GetDlgItem(hDlg, IDC_HEAP); - TCHAR szFmt[128]; - - DrawProcessView(hwndLView); - - DWORD dwProcessID = (DWORD)GetSelectedItemLParam(hwndLView); - - SIZE_T dwUsedHeap = CalcHeapOfProcess(dwProcessID); - - wsprintf(szFmt, _T("Memory used %uKB"), (DWORD)(dwUsedHeap>>10)); - SetWindowText(hwndHeap, szFmt); - - return 0; - } + case WM_WINDOWPOSCHANGED: + if (((LPWINDOWPOS)lParam)->flags & (SWP_SHOWWINDOW | SWP_FRAMECHANGED)) + { + if (HWND hwndLView = GetDlgItem(hDlg, IDC_LV_PROCESS)) + { + DrawProcessView(hwndLView); + } + if (HWND hwndHeap = GetDlgItem(hDlg, IDC_HEAP)) + { + TCHAR szFmt[128]; + SIZE_T dwUsedHeap = CalcHeapOfProcess(pTP->dwSelectedProcessID); + wsprintf(szFmt, _T("Memory used %uKB"), (DWORD)(dwUsedHeap >> 10)); + SetWindowText(hwndHeap, szFmt); + } + } + break; } - - return FALSE; + return FALSE; } //----------------------------------------------------------------------------- @@ -97,17 +94,9 @@ INT_PTR CALLBACK DlgProcProcess(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lPara //----------------------------------------------------------------------------- static BOOL DrawProcessView(HWND hwndLView) { - if(hwndLView == NULL) - { - return FALSE; - } - - BOOL bRet; - HANDLE hSS; - - hSS = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); + HANDLE const hSS = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); - if( hSS == (void*)-1 ) + if (hSS == INVALID_HANDLE_VALUE) { return FALSE; } @@ -115,55 +104,30 @@ static BOOL DrawProcessView(HWND hwndLView) // -- process PROCESSENTRY32 pe32; - memset(&pe32, 0, sizeof(PROCESSENTRY32)); - pe32.dwSize = sizeof(PROCESSENTRY32); + pe32.dwSize = sizeof pe32; - bRet = Process32First(hSS, &pe32); - - if( pe32.dwSize != sizeof(PROCESSENTRY32) ) - { - CloseToolhelp32Snapshot(hSS); - return FALSE; - } + LPARAM lParam[PROCESS_MAX]; - int nIndex[256]; - memset(&nIndex, 0, sizeof(int)); + int n = 0; - DWORD dwProcessIDs[256]; - memset(&dwProcessIDs, 0, sizeof(DWORD)*256); - - int ii = 0; - - do + if (Process32First(hSS, &pe32)) do { InsertProcessItem(hwndLView, &pe32); - dwProcessIDs[ii++] = pe32.th32ProcessID; - }while( Process32Next(hSS, &pe32) && ii < 255 ); + lParam[n++] = pe32.th32ProcessID; + } while (Process32Next(hSS, &pe32) && (n < PROCESS_MAX)); - DeleteProcessItem(hwndLView, &dwProcessIDs[0]); + DeleteExcessItemsLParam(hwndLView, lParam, n); CloseToolhelp32Snapshot(hSS); - if( 0 == GetSelectedItemLParam(hwndLView) ) - { - ListView_SetItemState(hwndLView, 0, LVIS_SELECTED, LVIS_SELECTED); - } - return TRUE; } //----------------------------------------------------------------------------- // make columns header //----------------------------------------------------------------------------- -static BOOL InitProcessListViewColumns(HWND hwndLView) +static void InitProcessListViewColumns(HWND hwndLView) { - if( hwndLView == NULL ) - return FALSE; - - RECT rcLView; - GetClientRect(hwndLView, &rcLView); - - LVCOLUMN lvc; lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM; @@ -171,33 +135,37 @@ static BOOL InitProcessListViewColumns(HWND hwndLView) // Process Image lvc.iSubItem = 0; lvc.pszText = _T("image name"); + lvc.cx = 100; lvc.fmt = LVCFMT_LEFT; - if (ListView_InsertColumn(hwndLView, 0, &lvc) == -1) - return FALSE; + ListView_InsertColumn(hwndLView, 0, &lvc); // Process ID lvc.iSubItem = 1; lvc.pszText = _T("id"); + lvc.cx = ListView_GetStringWidth(hwndLView, _T("0000000000")); + lvc.fmt = LVCFMT_LEFT; + ListView_InsertColumn(hwndLView, 1, &lvc); + + // Process Priority + lvc.iSubItem = 3; + lvc.pszText = _T("prio"); + lvc.cx = ListView_GetStringWidth(hwndLView, _T("00000")); lvc.fmt = LVCFMT_RIGHT; - if (ListView_InsertColumn(hwndLView, 1, &lvc) == -1) - return FALSE; + ListView_InsertColumn(hwndLView, 2, &lvc); + + // Process Affinity + lvc.iSubItem = 4; + lvc.pszText = _T("affin"); + lvc.cx = ListView_GetStringWidth(hwndLView, _T("00000")); + lvc.fmt = LVCFMT_RIGHT; + ListView_InsertColumn(hwndLView, 3, &lvc); // Process Threads lvc.iSubItem = 2; - lvc.pszText = _T("Threads"); + lvc.pszText = _T("thrds"); + lvc.cx = ListView_GetStringWidth(hwndLView, _T("000000")); lvc.fmt = LVCFMT_RIGHT; - if (ListView_InsertColumn(hwndLView, 2, &lvc) == -1) - return FALSE; - - HDC hDC = GetDC(hwndLView); - SIZE sz; - GetTextExtentPoint(hDC, _T("0000000000"), 10, &sz); - ReleaseDC(hwndLView, hDC); - - ListView_SetColumnWidth(hwndLView, 1, sz.cx); - ListView_SetColumnWidth(hwndLView, 2, sz.cx/2); - - return TRUE; + ListView_InsertColumn(hwndLView, 4, &lvc); } //----------------------------------------------------------------------------- @@ -205,10 +173,7 @@ static BOOL InitProcessListViewColumns(HWND hwndLView) //----------------------------------------------------------------------------- static BOOL InsertProcessItem(HWND hwndLView, PROCESSENTRY32* ppe32) { - if(hwndLView == NULL) - return FALSE; - - // serch item + // search item LVFINDINFO finditem; memset(&finditem, 0, sizeof finditem); @@ -246,51 +211,22 @@ static BOOL InsertProcessItem(HWND hwndLView, PROCESSENTRY32* ppe32) // Add volatile subitems - // threads - wsprintf( szFmt, _T("%d"), ppe32->cntThreads ); - ListView_SetItemText( hwndLView, dwIndex, 2, szFmt ); - - return TRUE; -} - -//----------------------------------------------------------------------------- -// clean up process which is not now on memory -//----------------------------------------------------------------------------- -static BOOL DeleteProcessItem(HWND hwndLView, DWORD* pdwProcessIDs) -{ - int nIndex = -1; - int ii = 0; - nIndex = ListView_GetNextItem(hwndLView, nIndex, 0); - - LVITEM lvItem; - memset(&lvItem, 0, sizeof(LVITEM)); + // priority + wsprintf(szFmt, _T("%d"), ppe32->pcPriClassBase); + ListView_SetItemText(hwndLView, dwIndex, 2, szFmt); - lvItem.mask = LVIF_PARAM; - - while( nIndex != -1 ) + // affinity + DWORD dwAffinity; + if (CeGetProcessAffinity((HANDLE)ppe32->th32ProcessID, &dwAffinity)) { - lvItem.iItem = nIndex; - ListView_GetItem(hwndLView, &lvItem); - - ii = 0; - BOOL fDelete = TRUE; - do - { - if( pdwProcessIDs[ii++] == (DWORD)lvItem.lParam ) - { - fDelete = FALSE; - break; - } - }while( pdwProcessIDs[ii] ); + wsprintf(szFmt, _T("%02X"), dwAffinity); + ListView_SetItemText(hwndLView, dwIndex, 3, szFmt); + } - if( fDelete ) - { - ListView_DeleteItem(hwndLView, nIndex); - nIndex = -1; - } + // threads + wsprintf(szFmt, _T("%d"), ppe32->cntThreads); + ListView_SetItemText(hwndLView, dwIndex, 4, szFmt); - nIndex = ListView_GetNextItem(hwndLView, nIndex, 0); - } return TRUE; } @@ -445,5 +381,7 @@ static void ResizeWindow(HWND hDlg, LPARAM lParam) cx -= GetSystemMetrics(SM_CXVSCROLL); cx -= ListView_GetColumnWidth(hwndLView, 1) + 1; cx -= ListView_GetColumnWidth(hwndLView, 2) + 1; + cx -= ListView_GetColumnWidth(hwndLView, 3) + 1; + cx -= ListView_GetColumnWidth(hwndLView, 4) + 1; ListView_SetColumnWidth(hwndLView, 0, cx); } diff --git a/itaskmgr_src/resource.h b/itaskmgr_src/resource.h index 716a2c7..5f6a7c8 100644 --- a/itaskmgr_src/resource.h +++ b/itaskmgr_src/resource.h @@ -9,6 +9,7 @@ #define IDD_TASK_LIST 104 #define IDD_SYSTEM_INFO 105 #define IDI_MAIN 106 +#define IDD_THREAD_LIST 107 #define IDD_HELP 109 #define IDI_DEFAULT_SMALLICON 110 #define IDI_CPU_00 200 @@ -24,6 +25,7 @@ #define IDI_CPU_10 210 #define IDI_CPU_11 211 #define IDC_LV_PROCESS 1001 +#define IDC_LV_THREAD 1002 #define IDC_GRAPH 1003 #define IDC_TAB 1007 #define IDC_STAY_ON_TOP 1008 diff --git a/itaskmgr_src/tasklist.cpp b/itaskmgr_src/tasklist.cpp index 5ab9292..8b5b6da 100644 --- a/itaskmgr_src/tasklist.cpp +++ b/itaskmgr_src/tasklist.cpp @@ -2,17 +2,15 @@ #include "ITaskMgr.h" #include "resource.h" -static BOOL InitTaskListViewColumns(HWND hwndLView); -static BOOL DrawTaskView(HWND hwndLView); +static void InitTaskListViewColumns(HWND hwndLView); +static void DrawTaskView(HWND hwndLView); static BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam); static BOOL InsertTaskItem(HWND hwndLView, HWND hwndEnum); -static BOOL DeleteTaskItem(HWND hwndLView, HWND* phwndEnumWins); static void ResizeWindow(HWND hDlg, LPARAM lParam); -extern HINSTANCE g_hInst; HIMAGELIST g_himlIcons; HICON g_hDefaultIcon; -DWORD g_nDefaultIconIndex; +int g_nDefaultIconIndex; //----------------------------------------------------------------------------- // process listview dialog @@ -26,14 +24,13 @@ 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; - g_hDefaultIcon = (HICON)LoadImage(g_hInst, MAKEINTRESOURCE(IDI_DEFAULT_SMALLICON), + g_hDefaultIcon = (HICON)LoadImage(pTP->g_hInst, MAKEINTRESOURCE(IDI_DEFAULT_SMALLICON), IMAGE_ICON, SM_CYSMICON, SM_CYSMICON, 0); if( !g_hDefaultIcon ) @@ -44,131 +41,112 @@ INT_PTR CALLBACK DlgProcTask(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam) if( g_nDefaultIconIndex == -1 ) return FALSE; - HWND hwndLView = GetDlgItem(hDlg, IDC_LV_TASKLIST); - ListView_SetExtendedListViewStyle(hwndLView, LVS_EX_FULLROWSELECT); - InitTaskListViewColumns(hwndLView); - DrawTaskView(hwndLView); + if (HWND hwndLView = GetDlgItem(hDlg, IDC_LV_TASKLIST)) + { + ListView_SetExtendedListViewStyle(hwndLView, LVS_EX_FULLROWSELECT); + ListView_SetImageList(hwndLView, g_himlIcons, LVSIL_SMALL); + InitTaskListViewColumns(hwndLView); + } return TRUE; - } // ---------------------------------------------------------- case WM_NOTIFY: - { - LPNMHDR lpnmhdr; - lpnmhdr = (LPNMHDR)lParam; - HWND hwndTaskList = GetDlgItem(hDlg, IDC_LV_TASKLIST); - - if( (lpnmhdr->hwndFrom == hwndTaskList) - && (lpnmhdr->idFrom == IDC_LV_TASKLIST) ) + if (((LPNMHDR)lParam)->idFrom == IDC_LV_TASKLIST) { - - switch( lpnmhdr->code ) + LPNMLISTVIEW lpnmlv = (LPNMLISTVIEW)lParam; + LVITEM lvItem; + switch (lpnmlv->hdr.code) { case NM_DBLCLK: - { - LPARAM lParam; - HWND hwndProcessList; - DWORD dwProcessID; - NMHDR nmh; - - lParam = GetSelectedItemLParam(hwndTaskList); - - GetWindowThreadProcessId((HWND)lParam, &dwProcessID); - hwndProcessList = GetDlgItem(pTP->hwndProcessList, IDC_LV_PROCESS); - - SelectItemLParam(hwndProcessList, dwProcessID); - TabCtrl_SetCurSel(pTP->hwndTab, MODE_PROCESS); - - nmh.hwndFrom = pTP->hwndTab; - nmh.idFrom = IDC_TAB; - nmh.code = TCN_SELCHANGE; - SendMessage(pTP->hDlg, WM_NOTIFY, 0, (LPARAM)&nmh); - SetFocus(hwndProcessList); - - return 0; - } - - case NM_CLICK: + if (HWND hwndTarget = (HWND)GetSelectedItemLParam(lpnmlv->hdr.hwndFrom)) + { + DWORD dwProcessID; + if (GetWindowThreadProcessId(hwndTarget, &dwProcessID)) + { + TabCtrl_SetCurSel(pTP->hwndTab, MODE_PROCESS); + NMHDR nmh = { pTP->hwndTab, IDC_TAB, TCN_SELCHANGE }; + SendMessage(pTP->hDlg, WM_NOTIFY, 0, (LPARAM)&nmh); + HWND hwndProcessList = GetDlgItem(pTP->hwndProcessList, IDC_LV_PROCESS); + SelectItemLParam(hwndProcessList, dwProcessID); + SetFocus(hwndProcessList); + } + } break; - default: + case LVN_DELETEITEM: + lvItem.mask = LVIF_IMAGE; + lvItem.iItem = lpnmlv->iItem; + lvItem.iSubItem = 0; + ListView_GetItem(lpnmlv->hdr.hwndFrom, &lvItem); + int nDeleteIconIndex = lvItem.iImage; + if (nDeleteIconIndex != g_nDefaultIconIndex) + { + ImageList_Remove(g_himlIcons, nDeleteIconIndex); + // refresh icon + int nIndex = -1; + while ((nIndex = ListView_GetNextItem(lpnmlv->hdr.hwndFrom, nIndex, 0)) != -1) + { + lvItem.iItem = nIndex; + ListView_GetItem(lpnmlv->hdr.hwndFrom, &lvItem); + if (lvItem.iImage >= nDeleteIconIndex) + { + lvItem.iImage--; + ListView_SetItem(lpnmlv->hdr.hwndFrom, &lvItem); + } + } + } break; } } break; - } // ---------------------------------------------------------- case WM_COMMAND: - { - HWND hwndLView, hwndTarget; - hwndLView = GetDlgItem(hDlg, IDC_LV_TASKLIST); - hwndTarget = (HWND)GetSelectedItemLParam(hwndLView); - - switch( LOWORD(wParam) ) + if (HWND hwndTarget = (HWND)GetSelectedItemLParam(GetDlgItem(hDlg, IDC_LV_TASKLIST))) { - case IDC_TASK_CLOSE: - if( hwndTarget ) + switch (LOWORD(wParam)) + { + case IDC_TASK_CLOSE: PostMessage(hwndTarget, WM_CLOSE, 0, 0); - break; + break; - case IDC_TASK_SWITCH: - if( hwndTarget ) - { + case IDC_TASK_SWITCH: ShowWindow(pTP->hDlg, SW_MINIMIZE); SetForegroundWindow(hwndTarget); + break; } - break; - - default: - break; } - return 0; - } + break; // ---------------------------------------------------------- case WM_SIZE: - { ResizeWindow(hDlg, lParam); - return 0; - } + break; // ---------------------------------------------------------- case WM_DESTROY: ImageList_Destroy(g_himlIcons); - return 0; + break; // ---------------------------------------------------------- - case WM_TIMER: - { - HWND hwndLView = GetDlgItem(hDlg, IDC_LV_TASKLIST); - DrawTaskView(hwndLView); - return 0; + case WM_WINDOWPOSCHANGED: + if (((LPWINDOWPOS)lParam)->flags & (SWP_SHOWWINDOW | SWP_FRAMECHANGED)) + { + HWND hwndLView = GetDlgItem(hDlg, IDC_LV_TASKLIST); + DrawTaskView(hwndLView); + } + break; } - // ---------------------------------------------------------- - } return FALSE; - } //----------------------------------------------------------------------------- // make columns header //----------------------------------------------------------------------------- -static BOOL InitTaskListViewColumns(HWND hwndLView) +static void InitTaskListViewColumns(HWND hwndLView) { - if( hwndLView == NULL ) - return FALSE; - - if(g_himlIcons == NULL) - return FALSE; - - ListView_SetImageList(hwndLView, g_himlIcons, LVSIL_SMALL); - - RECT rcLView; - GetClientRect(hwndLView, &rcLView); - LVCOLUMN lvc; lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM; @@ -178,38 +156,25 @@ static BOOL InitTaskListViewColumns(HWND hwndLView) lvc.pszText = _T("Task"); lvc.fmt = LVCFMT_LEFT; - if (ListView_InsertColumn(hwndLView, 0, &lvc) == -1) - return FALSE; - - ListView_SetColumnWidth(hwndLView, 0, rcLView.right-20); - - return TRUE; + ListView_InsertColumn(hwndLView, 0, &lvc); } //----------------------------------------------------------------------------- // //----------------------------------------------------------------------------- -static BOOL DrawTaskView(HWND hwndLView) +static void DrawTaskView(HWND hwndLView) { HWND hwndEnum[256]; - memset(hwndEnum, 0, sizeof(HWND)*256); + memset(hwndEnum, 0, sizeof hwndEnum); EnumWindows(EnumWindowsProc, (LPARAM)hwndEnum); - HWND* pWnd = hwndEnum; - - for( int ii = 0; ii < 256 && *pWnd != 0; ii++, pWnd++) + int n = 0; + while (n < _countof(hwndEnum) && hwndEnum[n]) { - InsertTaskItem(hwndLView, *pWnd); + InsertTaskItem(hwndLView, hwndEnum[n++]); } - DeleteTaskItem(hwndLView, hwndEnum); - - if( 0 == GetSelectedItemLParam(hwndLView) ) - { - ListView_SetItemState(hwndLView, 0, LVIS_SELECTED, LVIS_SELECTED); - } - - return TRUE; + DeleteExcessItemsLParam(hwndLView, (LPARAM*)hwndEnum, n); } //----------------------------------------------------------------------------- @@ -307,72 +272,6 @@ static BOOL InsertTaskItem(HWND hwndLView, HWND hwndEnum) return TRUE; } -//----------------------------------------------------------------------------- -// -//----------------------------------------------------------------------------- -static BOOL DeleteTaskItem(HWND hwndLView, HWND* phwndEnumWins) -{ - int nIndex = -1; - int ii = 0; - nIndex = ListView_GetNextItem(hwndLView, nIndex, 0); - - LVITEM lvItem; - memset(&lvItem, 0, sizeof(LVITEM)); - - lvItem.mask = LVIF_PARAM|LVIF_IMAGE; - - while( nIndex != -1 ) - { - lvItem.iItem = nIndex; - ListView_GetItem(hwndLView, &lvItem); - - ii = 0; - BOOL fDelete = TRUE; - do - { - if( phwndEnumWins[ii++] == (HWND)lvItem.lParam ) - { - fDelete = FALSE; - break; - } - }while( phwndEnumWins[ii] ); - - if( fDelete ) - { - DWORD dwDeleteIndex = nIndex; - DWORD dwDelteIconIndex = lvItem.iImage; - - ListView_DeleteItem(hwndLView, dwDeleteIndex); - - if( dwDelteIconIndex != g_nDefaultIconIndex ) - { - ImageList_Remove(g_himlIcons, dwDelteIconIndex); - - // reflesh icon - nIndex = ListView_GetNextItem(hwndLView, -1, 0); - - while( nIndex != -1 ) - { - lvItem.iItem = nIndex; - ListView_GetItem(hwndLView, &lvItem); - - if( lvItem.iImage >= (LPARAM)dwDelteIconIndex ) - { - lvItem.iImage--; - ListView_SetItem(hwndLView, &lvItem); - } - nIndex = ListView_GetNextItem(hwndLView, nIndex, 0); - } - } - - nIndex = -1; - } - - nIndex = ListView_GetNextItem(hwndLView, nIndex, 0); - } - return TRUE; -} - //----------------------------------------------------------------------------- // Resize all window //----------------------------------------------------------------------------- diff --git a/itaskmgr_src/thread.cpp b/itaskmgr_src/thread.cpp new file mode 100644 index 0000000..2d9f69f --- /dev/null +++ b/itaskmgr_src/thread.cpp @@ -0,0 +1,254 @@ +#include "stdafx.h" +#include "ITaskMgr.h" + +#ifndef _WIN32_WCE +class ID2TH +{ + HANDLE const h; +public: + ID2TH(DWORD id) : h(OpenThread(THREAD_QUERY_INFORMATION, FALSE, id)) { } + ~ID2TH() { CloseHandle(h); } + operator HANDLE() const { return h; } +}; +#else +typedef HANDLE ID2TH; +#endif + +static BOOL DeleteThreadItem(HWND hwndLView, DWORD* pdwThreadIDs); +static void InitThreadListViewColumns(HWND hwndLView); +static BOOL InsertThreadItem(HWND hwndLView, THREADENTRY32* pte32); +static BOOL DrawThreadView(HWND hwndLView, ThreadPack* pTP); +static void ResizeWindow(HWND hDlg, LPARAM lParam); + +//----------------------------------------------------------------------------- +// process listview dialog +//----------------------------------------------------------------------------- +INT_PTR CALLBACK DlgProcThread(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam) +{ + static ThreadPack* pTP = NULL; + + switch(Msg) + { + + // ---------------------------------------------------------- + case WM_INITDIALOG: + pTP = (ThreadPack*)lParam; + if (HWND hwndLView = GetDlgItem(hDlg, IDC_LV_THREAD)) + { + ListView_SetExtendedListViewStyle(hwndLView, LVS_EX_FULLROWSELECT); + InitThreadListViewColumns(hwndLView); + } + return TRUE; + + // ---------------------------------------------------------- + case WM_NOTIFY: + if (((LPNMHDR)lParam)->idFrom == IDC_LV_THREAD) + { + LPNMLISTVIEW lpnmlv = (LPNMLISTVIEW)lParam; + switch (lpnmlv->hdr.code) + { + case NM_DBLCLK: + TabCtrl_SetCurSel(pTP->hwndTab, MODE_PROCESS); + NMHDR nmh = { pTP->hwndTab, IDC_TAB, TCN_SELCHANGE }; + SendMessage(pTP->hDlg, WM_NOTIFY, 0, (LPARAM)&nmh); + SetFocus(GetNextDlgTabItem(pTP->hwndProcessList, NULL, FALSE)); + break; + } + } + break; + + // ---------------------------------------------------------- + case WM_SIZE: + ResizeWindow(hDlg, lParam); + break; + + // ---------------------------------------------------------- + case WM_WINDOWPOSCHANGED: + if (((LPWINDOWPOS)lParam)->flags & (SWP_SHOWWINDOW | SWP_FRAMECHANGED)) + { + if (HWND hwndLView = GetDlgItem(hDlg, IDC_LV_THREAD)) + { + DrawThreadView(hwndLView, pTP); + } + } + break; + } + + return FALSE; +} + +//----------------------------------------------------------------------------- +// draw graph of thread +//----------------------------------------------------------------------------- +static BOOL DrawThreadView(HWND hwndLView, ThreadPack* pTP) +{ + HANDLE const hSS = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, pTP->dwSelectedProcessID); + + if (hSS == INVALID_HANDLE_VALUE) + { + return FALSE; + } + + // -- thread + + THREADENTRY32 te32; + te32.dwSize = sizeof te32; + + LPARAM lParam[PROCESS_MAX]; + + int n = 0; + + if (Thread32First(hSS, &te32)) do + { + if (te32.th32OwnerProcessID == pTP->dwSelectedProcessID) + { + InsertThreadItem(hwndLView, &te32); + lParam[n++] = te32.th32ThreadID; + } + } while (Thread32Next(hSS, &te32) && (n < PROCESS_MAX)); + + DeleteExcessItemsLParam(hwndLView, lParam, n); + + CloseToolhelp32Snapshot(hSS); + + return TRUE; +} + +//----------------------------------------------------------------------------- +// make columns header +//----------------------------------------------------------------------------- +static void InitThreadListViewColumns(HWND hwndLView) +{ + LVCOLUMN lvc; + + lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM; + + // Thread ID/Owner + lvc.cx = ListView_GetStringWidth(hwndLView, _T("0000000000")); + lvc.fmt = LVCFMT_LEFT; + lvc.pszText = _T("id"); + ListView_InsertColumn(hwndLView, lvc.iSubItem = 0, &lvc); + lvc.pszText = _T("owner"); + ListView_InsertColumn(hwndLView, lvc.iSubItem = 1, &lvc); + + // Thread Priority/Affinity + lvc.cx = ListView_GetStringWidth(hwndLView, _T("00000")); + lvc.fmt = LVCFMT_RIGHT; + lvc.pszText = _T("prio"); + ListView_InsertColumn(hwndLView, lvc.iSubItem = 2, &lvc); + lvc.pszText = _T("affin"); + ListView_InsertColumn(hwndLView, lvc.iSubItem = 3, &lvc); + + // Thread Kernel/User Time + lvc.cx = ListView_GetStringWidth(hwndLView, _T("000000000000000000000")); + lvc.fmt = LVCFMT_RIGHT; + lvc.pszText = _T("ktime"); + ListView_InsertColumn(hwndLView, lvc.iSubItem = 4, &lvc); + lvc.pszText = _T("utime"); + ListView_InsertColumn(hwndLView, lvc.iSubItem = 5, &lvc); +} + +//----------------------------------------------------------------------------- +// fill process view +//----------------------------------------------------------------------------- +static BOOL InsertThreadItem(HWND hwndLView, THREADENTRY32* pte32) +{ + // serch item + LVFINDINFO finditem; + memset(&finditem, 0, sizeof finditem); + + finditem.flags = LVFI_PARAM; + finditem.lParam = pte32->th32ThreadID; + + DWORD dwIndex = ListView_FindItem(hwndLView, -1, &finditem); + + TCHAR szFmt[256]; + + if (dwIndex == -1) + { + LVITEM lvItem; + memset(&lvItem, 0, sizeof(LVITEM)); + + lvItem.mask = LVIF_TEXT | LVIF_PARAM; + lvItem.iItem = 0; + lvItem.iSubItem = 0; + lvItem.pszText = szFmt; + lvItem.lParam = pte32->th32ThreadID; + + wsprintf(szFmt, _T("%08X"), pte32->th32ThreadID); + dwIndex = ListView_InsertItem(hwndLView, &lvItem); + + if( dwIndex == -1 ) + { + return FALSE; + } + + // Add nonvolatile subitems + + // owner + wsprintf(szFmt, _T("%08X"), pte32->th32OwnerProcessID); + ListView_SetItemText(hwndLView, dwIndex, 1, szFmt); + } + + // Add volatile subitems + + // priority + wsprintf(szFmt, _T("%d"), pte32->tpBasePri); + ListView_SetItemText(hwndLView, dwIndex, 2, szFmt); + + // affinity + DWORD dwAffinity; + if (CeGetThreadAffinity((HANDLE)pte32->th32ThreadID, &dwAffinity)) + { + wsprintf(szFmt, _T("%02X"), dwAffinity); + ListView_SetItemText(hwndLView, dwIndex, 3, szFmt); + } + + FILETIME ctime, etime, ktime, utime; + if (GetThreadTimes((ID2TH)pte32->th32ThreadID, &ctime, &etime, &ktime, &utime)) + { + // format to millisecond precision + wsprintf(szFmt, _T("%u,%03u,%03u,%03u,%03u,%03u"), + static_cast(reinterpret_cast(ktime) / static_cast(1E19) % 1000), + static_cast(reinterpret_cast(ktime) / static_cast(1E16) % 1000), + static_cast(reinterpret_cast(ktime) / static_cast(1E13) % 1000), + static_cast(reinterpret_cast(ktime) / static_cast(1E10) % 1000), + static_cast(reinterpret_cast(ktime) / static_cast(1E07) % 1000), + static_cast(reinterpret_cast(ktime) / static_cast(1E04) % 1000)); + ListView_SetItemText(hwndLView, dwIndex, 4, szFmt); + wsprintf(szFmt, _T("%u,%03u,%03u,%03u,%03u,%03u"), + static_cast(reinterpret_cast(utime) / static_cast(1E19) % 1000), + static_cast(reinterpret_cast(utime) / static_cast(1E16) % 1000), + static_cast(reinterpret_cast(utime) / static_cast(1E13) % 1000), + static_cast(reinterpret_cast(utime) / static_cast(1E10) % 1000), + static_cast(reinterpret_cast(utime) / static_cast(1E07) % 1000), + static_cast(reinterpret_cast(utime) / static_cast(1E04) % 1000)); + ListView_SetItemText(hwndLView, dwIndex, 5, szFmt); + } + + return TRUE; +} + +//----------------------------------------------------------------------------- +// Resize all window +//----------------------------------------------------------------------------- +static void ResizeWindow(HWND hDlg, LPARAM lParam) +{ + HWND hwndLView = GetDlgItem(hDlg, IDC_LV_THREAD); + + RECT rcTab; + + SetRect(&rcTab, 0, 0, LOWORD(lParam), HIWORD(lParam)); + TabCtrl_AdjustRect(hDlg, FALSE, &rcTab); + + HDWP hdwp = BeginDeferWindowPos(1); + + hdwp = DeferWindowPos(hdwp, hwndLView, NULL + , rcTab.left + , rcTab.top + , rcTab.right - rcTab.left + , rcTab.bottom - rcTab.top + , SWP_NOZORDER); + + EndDeferWindowPos(hdwp); +}