4 * taskmgr.c : Defines the entry point for the application.
6 * Copyright (C) 1999 - 2001 Brian Palmer <brianp@reactos.org>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #define WIN32_LEAN_AND_MEAN /* Exclude rarely-used stuff from Windows headers */
38 #define STATUS_WINDOW 2001
40 /* Global Variables: */
41 HINSTANCE hInst; /* current instance */
43 HWND hMainWnd; /* Main Window */
44 HWND hStatusWnd; /* Status Bar Window */
45 HWND hTabWnd; /* Tab Control Window */
47 int nMinimumWidth; /* Minimum width of the dialog (OnSize()'s cx) */
48 int nMinimumHeight; /* Minimum height of the dialog (OnSize()'s cy) */
50 int nOldWidth; /* Holds the previous client area width */
51 int nOldHeight; /* Holds the previous client area height */
53 BOOL bInMenuLoop = FALSE; /* Tells us if we are in the menu loop */
55 TASKMANAGER_SETTINGS TaskManagerSettings;
58 int APIENTRY WinMain(HINSTANCE hInstance,
59 HINSTANCE hPrevInstance,
67 /* Initialize global variables */
70 /* Change our priority class to HIGH */
71 hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
72 SetPriorityClass(hProcess, HIGH_PRIORITY_CLASS);
73 CloseHandle(hProcess);
75 /* Now let's get the SE_DEBUG_NAME privilege
76 * so that we can debug processes
79 /* Get a token for this process. */
80 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
81 /* Get the LUID for the debug privilege. */
82 LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &tkp.Privileges[0].Luid);
84 tkp.PrivilegeCount = 1; /* one privilege to set */
85 tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
87 /* Get the debug privilege for this process. */
88 AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
91 /* Load our settings from the registry */
94 /* Initialize perf data */
95 if (!PerfDataInitialize()) {
99 DialogBox(hInst, (LPCTSTR)IDD_TASKMGR_DIALOG, NULL, (DLGPROC)TaskManagerWndProc);
101 /* Save our settings to the registry */
103 PerfDataUninitialize();
107 /* Message handler for dialog box. */
108 LRESULT CALLBACK TaskManagerWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
121 return OnCreate(hDlg);
124 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
125 EndDialog(hDlg, LOWORD(wParam));
128 /* Process menu commands */
129 switch (LOWORD(wParam))
132 TaskManager_OnFileNew();
134 case ID_OPTIONS_ALWAYSONTOP:
135 TaskManager_OnOptionsAlwaysOnTop();
137 case ID_OPTIONS_MINIMIZEONUSE:
138 TaskManager_OnOptionsMinimizeOnUse();
140 case ID_OPTIONS_HIDEWHENMINIMIZED:
141 TaskManager_OnOptionsHideWhenMinimized();
143 case ID_OPTIONS_SHOW16BITTASKS:
144 TaskManager_OnOptionsShow16BitTasks();
147 ApplicationPage_OnViewLargeIcons();
150 ApplicationPage_OnViewSmallIcons();
152 case ID_VIEW_DETAILS:
153 ApplicationPage_OnViewDetails();
155 case ID_VIEW_SHOWKERNELTIMES:
156 PerformancePage_OnViewShowKernelTimes();
158 case ID_VIEW_CPUHISTORY_ONEGRAPHALL:
159 PerformancePage_OnViewCPUHistoryOneGraphAll();
161 case ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU:
162 PerformancePage_OnViewCPUHistoryOneGraphPerCPU();
164 case ID_VIEW_UPDATESPEED_HIGH:
165 TaskManager_OnViewUpdateSpeedHigh();
167 case ID_VIEW_UPDATESPEED_NORMAL:
168 TaskManager_OnViewUpdateSpeedNormal();
170 case ID_VIEW_UPDATESPEED_LOW:
171 TaskManager_OnViewUpdateSpeedLow();
173 case ID_VIEW_UPDATESPEED_PAUSED:
174 TaskManager_OnViewUpdateSpeedPaused();
176 case ID_VIEW_SELECTCOLUMNS:
177 ProcessPage_OnViewSelectColumns();
179 case ID_VIEW_REFRESH:
180 PostMessage(hDlg, WM_TIMER, 0, 0);
182 case ID_WINDOWS_TILEHORIZONTALLY:
183 ApplicationPage_OnWindowsTileHorizontally();
185 case ID_WINDOWS_TILEVERTICALLY:
186 ApplicationPage_OnWindowsTileVertically();
188 case ID_WINDOWS_MINIMIZE:
189 ApplicationPage_OnWindowsMinimize();
191 case ID_WINDOWS_MAXIMIZE:
192 ApplicationPage_OnWindowsMaximize();
194 case ID_WINDOWS_CASCADE:
195 ApplicationPage_OnWindowsCascade();
197 case ID_WINDOWS_BRINGTOFRONT:
198 ApplicationPage_OnWindowsBringToFront();
200 case ID_APPLICATION_PAGE_SWITCHTO:
201 ApplicationPage_OnSwitchTo();
203 case ID_APPLICATION_PAGE_ENDTASK:
204 ApplicationPage_OnEndTask();
206 case ID_APPLICATION_PAGE_GOTOPROCESS:
207 ApplicationPage_OnGotoProcess();
209 case ID_PROCESS_PAGE_ENDPROCESS:
210 ProcessPage_OnEndProcess();
212 case ID_PROCESS_PAGE_ENDPROCESSTREE:
213 ProcessPage_OnEndProcessTree();
215 case ID_PROCESS_PAGE_DEBUG:
216 ProcessPage_OnDebug();
218 case ID_PROCESS_PAGE_SETAFFINITY:
219 ProcessPage_OnSetAffinity();
221 case ID_PROCESS_PAGE_SETPRIORITY_REALTIME:
222 ProcessPage_OnSetPriorityRealTime();
224 case ID_PROCESS_PAGE_SETPRIORITY_HIGH:
225 ProcessPage_OnSetPriorityHigh();
227 case ID_PROCESS_PAGE_SETPRIORITY_ABOVENORMAL:
228 ProcessPage_OnSetPriorityAboveNormal();
230 case ID_PROCESS_PAGE_SETPRIORITY_NORMAL:
231 ProcessPage_OnSetPriorityNormal();
233 case ID_PROCESS_PAGE_SETPRIORITY_BELOWNORMAL:
234 ProcessPage_OnSetPriorityBelowNormal();
236 case ID_PROCESS_PAGE_SETPRIORITY_LOW:
237 ProcessPage_OnSetPriorityLow();
239 case ID_PROCESS_PAGE_DEBUGCHANNELS:
240 ProcessPage_OnDebugChannels();
252 idctrl = (int)wParam;
253 pnmh = (LPNMHDR)lParam;
254 if ((pnmh->hwndFrom == hTabWnd) &&
255 (pnmh->idFrom == IDC_TAB) &&
256 (pnmh->code == TCN_SELCHANGE))
258 TaskManager_OnTabWndSelChange();
264 GetClientRect(hDlg, &rc);
265 Draw3dRect(hdc, rc.left, rc.top, rc.right, rc.top + 2, GetSysColor(COLOR_3DSHADOW), GetSysColor(COLOR_3DHILIGHT));
266 ReleaseDC(hDlg, hdc);
270 hdc = BeginPaint(hDlg, &ps);
271 GetClientRect(hDlg, &rc);
272 Draw3dRect(hdc, rc.left, rc.top, rc.right, rc.top + 2, GetSysColor(COLOR_3DSHADOW), GetSysColor(COLOR_3DHILIGHT));
277 /* Make sure the user is sizing the dialog */
278 /* in an acceptable range */
279 pRC = (LPRECT)lParam;
280 if ((wParam == WMSZ_LEFT) || (wParam == WMSZ_TOPLEFT) || (wParam == WMSZ_BOTTOMLEFT)) {
281 /* If the width is too small enlarge it to the minimum */
282 if (nMinimumWidth > (pRC->right - pRC->left))
283 pRC->left = pRC->right - nMinimumWidth;
285 /* If the width is too small enlarge it to the minimum */
286 if (nMinimumWidth > (pRC->right - pRC->left))
287 pRC->right = pRC->left + nMinimumWidth;
289 if ((wParam == WMSZ_TOP) || (wParam == WMSZ_TOPLEFT) || (wParam == WMSZ_TOPRIGHT)) {
290 /* If the height is too small enlarge it to the minimum */
291 if (nMinimumHeight > (pRC->bottom - pRC->top))
292 pRC->top = pRC->bottom - nMinimumHeight;
294 /* If the height is too small enlarge it to the minimum */
295 if (nMinimumHeight > (pRC->bottom - pRC->top))
296 pRC->bottom = pRC->top + nMinimumHeight;
302 /* Handle the window sizing in it's own function */
303 OnSize(wParam, LOWORD(lParam), HIWORD(lParam));
307 /* Handle the window moving in it's own function */
308 OnMove(wParam, LOWORD(lParam), HIWORD(lParam));
312 ShowWindow(hDlg, SW_HIDE);
313 TrayIcon_ShellRemoveTrayIcon();
314 wp.length = sizeof(WINDOWPLACEMENT);
315 GetWindowPlacement(hDlg, &wp);
316 TaskManagerSettings.Left = wp.rcNormalPosition.left;
317 TaskManagerSettings.Top = wp.rcNormalPosition.top;
318 TaskManagerSettings.Right = wp.rcNormalPosition.right;
319 TaskManagerSettings.Bottom = wp.rcNormalPosition.bottom;
320 if (IsZoomed(hDlg) || (wp.flags & WPF_RESTORETOMAXIMIZED))
321 TaskManagerSettings.Maximized = TRUE;
323 TaskManagerSettings.Maximized = FALSE;
324 return DefWindowProc(hDlg, message, wParam, lParam);
327 /* Refresh the performance data */
329 RefreshApplicationPage();
330 RefreshProcessPage();
331 RefreshPerformancePage();
332 TrayIcon_ShellUpdateTrayIcon();
335 case WM_ENTERMENULOOP:
336 TaskManager_OnEnterMenuLoop(hDlg);
338 case WM_EXITMENULOOP:
339 TaskManager_OnExitMenuLoop(hDlg);
342 TaskManager_OnMenuSelect(hDlg, LOWORD(wParam), HIWORD(wParam), (HMENU)lParam);
349 void FillSolidRect(HDC hDC, LPCRECT lpRect, COLORREF clr)
351 SetBkColor(hDC, clr);
352 ExtTextOut(hDC, 0, 0, ETO_OPAQUE, lpRect, NULL, 0, NULL);
355 void FillSolidRect2(HDC hDC, int x, int y, int cx, int cy, COLORREF clr)
359 SetBkColor(hDC, clr);
363 rect.bottom = y + cy;
364 ExtTextOut(hDC, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
367 void Draw3dRect(HDC hDC, int x, int y, int cx, int cy, COLORREF clrTopLeft, COLORREF clrBottomRight)
369 FillSolidRect2(hDC, x, y, cx - 1, 1, clrTopLeft);
370 FillSolidRect2(hDC, x, y, 1, cy - 1, clrTopLeft);
371 FillSolidRect2(hDC, x + cx, y, -1, cy, clrBottomRight);
372 FillSolidRect2(hDC, x, y + cy, cx, -1, clrBottomRight);
375 void Draw3dRect2(HDC hDC, LPRECT lpRect, COLORREF clrTopLeft, COLORREF clrBottomRight)
377 Draw3dRect(hDC, lpRect->left, lpRect->top, lpRect->right - lpRect->left,
378 lpRect->bottom - lpRect->top, clrTopLeft, clrBottomRight);
381 void Font_DrawText(HDC hDC, LPCTSTR lpszText, int x, int y)
388 hFontDC = CreateCompatibleDC(hDC);
389 hFontBitmap = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_FONT));
390 hOldBitmap = (HBITMAP)SelectObject(hFontDC, hFontBitmap);
392 for (i = 0; i < (int)_tcslen(lpszText); i++) {
393 if ((lpszText[i] >= '0') && (lpszText[i] <= '9')) {
394 BitBlt(hDC, x + (i * 8), y, 8, 11, hFontDC, (lpszText[i] - '0') * 8, 0, SRCCOPY);
396 else if (lpszText[i] == 'K')
398 BitBlt(hDC, x + (i * 8), y, 8, 11, hFontDC, 80, 0, SRCCOPY);
400 else if (lpszText[i] == '%')
402 BitBlt(hDC, x + (i * 8), y, 8, 11, hFontDC, 88, 0, SRCCOPY);
405 SelectObject(hFontDC, hOldBitmap);
406 DeleteObject(hFontBitmap);
410 BOOL OnCreate(HWND hWnd)
415 HMENU hUpdateSpeedMenu;
416 HMENU hCPUHistoryMenu;
423 SendMessage(hMainWnd, WM_SETICON, ICON_BIG, (LPARAM)LoadIcon(hInst, MAKEINTRESOURCE(IDI_TASKMANAGER)));
425 /* Initialize the Windows Common Controls DLL */
426 InitCommonControls();
428 /* Get the minimum window sizes */
429 GetWindowRect(hWnd, &rc);
430 nMinimumWidth = (rc.right - rc.left);
431 nMinimumHeight = (rc.bottom - rc.top);
433 /* Create the status bar */
434 hStatusWnd = CreateStatusWindow(WS_VISIBLE|WS_CHILD|WS_CLIPSIBLINGS|SBT_NOBORDERS, _T(""), hWnd, STATUS_WINDOW);
438 /* Create the status bar panes */
442 SendMessage(hStatusWnd, SB_SETPARTS, 3, (long)nParts);
444 /* Create tab pages */
445 hTabWnd = GetDlgItem(hWnd, IDC_TAB);
447 hApplicationPage = CreateDialog(hInst, MAKEINTRESOURCE(IDD_APPLICATION_PAGE), hWnd, (DLGPROC)ApplicationPageWndProc);
448 hProcessPage = CreateDialog(hInst, MAKEINTRESOURCE(IDD_PROCESS_PAGE), hWnd, (DLGPROC)ProcessPageWndProc);
449 hPerformancePage = CreateDialog(hInst, MAKEINTRESOURCE(IDD_PERFORMANCE_PAGE), hWnd, (DLGPROC)PerformancePageWndProc);
451 hApplicationPage = CreateDialog(hInst, MAKEINTRESOURCE(IDD_APPLICATION_PAGE), hTabWnd, (DLGPROC)ApplicationPageWndProc);
452 hProcessPage = CreateDialog(hInst, MAKEINTRESOURCE(IDD_PROCESS_PAGE), hTabWnd, (DLGPROC)ProcessPageWndProc);
453 hPerformancePage = CreateDialog(hInst, MAKEINTRESOURCE(IDD_PERFORMANCE_PAGE), hTabWnd, (DLGPROC)PerformancePageWndProc);
457 _tcscpy(szTemp, _T("Applications"));
458 memset(&item, 0, sizeof(TCITEM));
459 item.mask = TCIF_TEXT;
460 item.pszText = szTemp;
461 TabCtrl_InsertItem(hTabWnd, 0, &item);
462 _tcscpy(szTemp, _T("Processes"));
463 memset(&item, 0, sizeof(TCITEM));
464 item.mask = TCIF_TEXT;
465 item.pszText = szTemp;
466 TabCtrl_InsertItem(hTabWnd, 1, &item);
467 _tcscpy(szTemp, _T("Performance"));
468 memset(&item, 0, sizeof(TCITEM));
469 item.mask = TCIF_TEXT;
470 item.pszText = szTemp;
471 TabCtrl_InsertItem(hTabWnd, 2, &item);
473 /* Size everything correctly */
474 GetClientRect(hWnd, &rc);
475 nOldWidth = rc.right;
476 nOldHeight = rc.bottom;
477 /* nOldStartX = rc.left; */
478 /*nOldStartY = rc.top; */
480 #define PAGE_OFFSET_LEFT 17
481 #define PAGE_OFFSET_TOP 72
482 #define PAGE_OFFSET_WIDTH (PAGE_OFFSET_LEFT*2)
483 #define PAGE_OFFSET_HEIGHT (PAGE_OFFSET_TOP+32)
485 if ((TaskManagerSettings.Left != 0) ||
486 (TaskManagerSettings.Top != 0) ||
487 (TaskManagerSettings.Right != 0) ||
488 (TaskManagerSettings.Bottom != 0))
490 MoveWindow(hWnd, TaskManagerSettings.Left, TaskManagerSettings.Top, TaskManagerSettings.Right - TaskManagerSettings.Left, TaskManagerSettings.Bottom - TaskManagerSettings.Top, TRUE);
491 #ifdef __GNUC__TEST__
492 MoveWindow(hApplicationPage, TaskManagerSettings.Left + PAGE_OFFSET_LEFT, TaskManagerSettings.Top + PAGE_OFFSET_TOP, TaskManagerSettings.Right - TaskManagerSettings.Left - PAGE_OFFSET_WIDTH, TaskManagerSettings.Bottom - TaskManagerSettings.Top - PAGE_OFFSET_HEIGHT, FALSE);
493 MoveWindow(hProcessPage, TaskManagerSettings.Left + PAGE_OFFSET_LEFT, TaskManagerSettings.Top + PAGE_OFFSET_TOP, TaskManagerSettings.Right - TaskManagerSettings.Left - PAGE_OFFSET_WIDTH, TaskManagerSettings.Bottom - TaskManagerSettings.Top - PAGE_OFFSET_HEIGHT, FALSE);
494 MoveWindow(hPerformancePage, TaskManagerSettings.Left + PAGE_OFFSET_LEFT, TaskManagerSettings.Top + PAGE_OFFSET_TOP, TaskManagerSettings.Right - TaskManagerSettings.Left - PAGE_OFFSET_WIDTH, TaskManagerSettings.Bottom - TaskManagerSettings.Top - PAGE_OFFSET_HEIGHT, FALSE);
497 if (TaskManagerSettings.Maximized)
498 ShowWindow(hWnd, SW_MAXIMIZE);
500 /* Set the always on top style */
501 hMenu = GetMenu(hWnd);
502 hEditMenu = GetSubMenu(hMenu, 1);
503 hViewMenu = GetSubMenu(hMenu, 2);
504 hUpdateSpeedMenu = GetSubMenu(hViewMenu, 1);
505 hCPUHistoryMenu = GetSubMenu(hViewMenu, 7);
507 /* Check or uncheck the always on top menu item */
508 if (TaskManagerSettings.AlwaysOnTop) {
509 CheckMenuItem(hEditMenu, ID_OPTIONS_ALWAYSONTOP, MF_BYCOMMAND|MF_CHECKED);
510 SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
512 CheckMenuItem(hEditMenu, ID_OPTIONS_ALWAYSONTOP, MF_BYCOMMAND|MF_UNCHECKED);
513 SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
516 /* Check or uncheck the minimize on use menu item */
517 if (TaskManagerSettings.MinimizeOnUse)
518 CheckMenuItem(hEditMenu, ID_OPTIONS_MINIMIZEONUSE, MF_BYCOMMAND|MF_CHECKED);
520 CheckMenuItem(hEditMenu, ID_OPTIONS_MINIMIZEONUSE, MF_BYCOMMAND|MF_UNCHECKED);
522 /* Check or uncheck the hide when minimized menu item */
523 if (TaskManagerSettings.HideWhenMinimized)
524 CheckMenuItem(hEditMenu, ID_OPTIONS_HIDEWHENMINIMIZED, MF_BYCOMMAND|MF_CHECKED);
526 CheckMenuItem(hEditMenu, ID_OPTIONS_HIDEWHENMINIMIZED, MF_BYCOMMAND|MF_UNCHECKED);
528 /* Check or uncheck the show 16-bit tasks menu item */
529 if (TaskManagerSettings.Show16BitTasks)
530 CheckMenuItem(hEditMenu, ID_OPTIONS_SHOW16BITTASKS, MF_BYCOMMAND|MF_CHECKED);
532 CheckMenuItem(hEditMenu, ID_OPTIONS_SHOW16BITTASKS, MF_BYCOMMAND|MF_UNCHECKED);
534 if (TaskManagerSettings.View_LargeIcons)
535 CheckMenuRadioItem(hViewMenu, ID_VIEW_LARGE, ID_VIEW_DETAILS, ID_VIEW_LARGE, MF_BYCOMMAND);
536 else if (TaskManagerSettings.View_SmallIcons)
537 CheckMenuRadioItem(hViewMenu, ID_VIEW_LARGE, ID_VIEW_DETAILS, ID_VIEW_SMALL, MF_BYCOMMAND);
539 CheckMenuRadioItem(hViewMenu, ID_VIEW_LARGE, ID_VIEW_DETAILS, ID_VIEW_DETAILS, MF_BYCOMMAND);
541 if (TaskManagerSettings.ShowKernelTimes)
542 CheckMenuItem(hViewMenu, ID_VIEW_SHOWKERNELTIMES, MF_BYCOMMAND|MF_CHECKED);
544 CheckMenuItem(hViewMenu, ID_VIEW_SHOWKERNELTIMES, MF_BYCOMMAND|MF_UNCHECKED);
546 if (TaskManagerSettings.UpdateSpeed == 1)
547 CheckMenuRadioItem(hUpdateSpeedMenu, ID_VIEW_UPDATESPEED_HIGH, ID_VIEW_UPDATESPEED_PAUSED, ID_VIEW_UPDATESPEED_HIGH, MF_BYCOMMAND);
548 else if (TaskManagerSettings.UpdateSpeed == 2)
549 CheckMenuRadioItem(hUpdateSpeedMenu, ID_VIEW_UPDATESPEED_HIGH, ID_VIEW_UPDATESPEED_PAUSED, ID_VIEW_UPDATESPEED_NORMAL, MF_BYCOMMAND);
550 else if (TaskManagerSettings.UpdateSpeed == 4)
551 CheckMenuRadioItem(hUpdateSpeedMenu, ID_VIEW_UPDATESPEED_HIGH, ID_VIEW_UPDATESPEED_PAUSED, ID_VIEW_UPDATESPEED_LOW, MF_BYCOMMAND);
553 CheckMenuRadioItem(hUpdateSpeedMenu, ID_VIEW_UPDATESPEED_HIGH, ID_VIEW_UPDATESPEED_PAUSED, ID_VIEW_UPDATESPEED_PAUSED, MF_BYCOMMAND);
555 if (TaskManagerSettings.CPUHistory_OneGraphPerCPU)
556 CheckMenuRadioItem(hCPUHistoryMenu, ID_VIEW_CPUHISTORY_ONEGRAPHALL, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, MF_BYCOMMAND);
558 CheckMenuRadioItem(hCPUHistoryMenu, ID_VIEW_CPUHISTORY_ONEGRAPHALL, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, ID_VIEW_CPUHISTORY_ONEGRAPHALL, MF_BYCOMMAND);
560 nActivePage = TaskManagerSettings.ActiveTabPage;
561 TabCtrl_SetCurFocus/*Sel*/(hTabWnd, 0);
562 TabCtrl_SetCurFocus/*Sel*/(hTabWnd, 1);
563 TabCtrl_SetCurFocus/*Sel*/(hTabWnd, 2);
564 TabCtrl_SetCurFocus/*Sel*/(hTabWnd, nActivePage);
566 if (TaskManagerSettings.UpdateSpeed == 0)
568 else if (TaskManagerSettings.UpdateSpeed == 1)
571 SetTimer(hWnd, 1, 1000, NULL);
573 else if (TaskManagerSettings.UpdateSpeed == 2)
576 SetTimer(hWnd, 1, 2000, NULL);
578 else if (TaskManagerSettings.UpdateSpeed == 4)
581 SetTimer(hWnd, 1, 4000, NULL);
585 * Refresh the performance data
586 * Sample it twice so we can establish
587 * the delta values & cpu usage
592 RefreshApplicationPage();
593 RefreshProcessPage();
594 RefreshPerformancePage();
596 TrayIcon_ShellAddTrayIcon();
602 * This function handles all the moving events for the application
603 * It moves every child window that needs moving
605 void OnMove( UINT nType, int cx, int cy )
607 #ifdef __GNUC__TEST__
608 MoveWindow(hApplicationPage, TaskManagerSettings.Left + PAGE_OFFSET_LEFT, TaskManagerSettings.Top + PAGE_OFFSET_TOP, TaskManagerSettings.Right - TaskManagerSettings.Left - PAGE_OFFSET_WIDTH, TaskManagerSettings.Bottom - TaskManagerSettings.Top - PAGE_OFFSET_HEIGHT, FALSE);
609 MoveWindow(hProcessPage, TaskManagerSettings.Left + PAGE_OFFSET_LEFT, TaskManagerSettings.Top + PAGE_OFFSET_TOP, TaskManagerSettings.Right - TaskManagerSettings.Left - PAGE_OFFSET_WIDTH, TaskManagerSettings.Bottom - TaskManagerSettings.Top - PAGE_OFFSET_HEIGHT, FALSE);
610 MoveWindow(hPerformancePage, TaskManagerSettings.Left + PAGE_OFFSET_LEFT, TaskManagerSettings.Top + PAGE_OFFSET_TOP, TaskManagerSettings.Right - TaskManagerSettings.Left - PAGE_OFFSET_WIDTH, TaskManagerSettings.Bottom - TaskManagerSettings.Top - PAGE_OFFSET_HEIGHT, FALSE);
615 * This function handles all the sizing events for the application
616 * It re-sizes every window, and child window that needs re-sizing
618 void OnSize( UINT nType, int cx, int cy )
625 if (nType == SIZE_MINIMIZED)
628 nXDifference = cx - nOldWidth;
629 nYDifference = cy - nOldHeight;
633 /* Update the status bar size */
634 GetWindowRect(hStatusWnd, &rc);
635 SendMessage(hStatusWnd, WM_SIZE, nType, MAKELPARAM(cx, cy + (rc.bottom - rc.top)));
637 /* Update the status bar pane sizes */
638 nParts[0] = bInMenuLoop ? -1 : 100;
641 SendMessage(hStatusWnd, SB_SETPARTS, bInMenuLoop ? 1 : 3, (long)nParts);
643 /* Resize the tab control */
644 GetWindowRect(hTabWnd, &rc);
645 cx = (rc.right - rc.left) + nXDifference;
646 cy = (rc.bottom - rc.top) + nYDifference;
647 SetWindowPos(hTabWnd, NULL, 0, 0, cx, cy, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOMOVE|SWP_NOZORDER);
649 /* Resize the application page */
650 GetWindowRect(hApplicationPage, &rc);
651 cx = (rc.right - rc.left) + nXDifference;
652 cy = (rc.bottom - rc.top) + nYDifference;
653 SetWindowPos(hApplicationPage, NULL, 0, 0, cx, cy, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOMOVE|SWP_NOZORDER);
655 /* Resize the process page */
656 GetWindowRect(hProcessPage, &rc);
657 cx = (rc.right - rc.left) + nXDifference;
658 cy = (rc.bottom - rc.top) + nYDifference;
659 SetWindowPos(hProcessPage, NULL, 0, 0, cx, cy, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOMOVE|SWP_NOZORDER);
661 /* Resize the performance page */
662 GetWindowRect(hPerformancePage, &rc);
663 cx = (rc.right - rc.left) + nXDifference;
664 cy = (rc.bottom - rc.top) + nYDifference;
665 SetWindowPos(hPerformancePage, NULL, 0, 0, cx, cy, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOMOVE|SWP_NOZORDER);
668 void LoadSettings(void)
671 TCHAR szSubKey[] = _T("Software\\ReactWare\\TaskManager");
675 /* Window size & position settings */
676 TaskManagerSettings.Maximized = FALSE;
677 TaskManagerSettings.Left = 0;
678 TaskManagerSettings.Top = 0;
679 TaskManagerSettings.Right = 0;
680 TaskManagerSettings.Bottom = 0;
683 TaskManagerSettings.ActiveTabPage = 0;
685 /* Options menu settings */
686 TaskManagerSettings.AlwaysOnTop = FALSE;
687 TaskManagerSettings.MinimizeOnUse = TRUE;
688 TaskManagerSettings.HideWhenMinimized = TRUE;
689 TaskManagerSettings.Show16BitTasks = TRUE;
691 /* Update speed settings */
692 TaskManagerSettings.UpdateSpeed = 2;
694 /* Applications page settings */
695 TaskManagerSettings.View_LargeIcons = FALSE;
696 TaskManagerSettings.View_SmallIcons = FALSE;
697 TaskManagerSettings.View_Details = TRUE;
699 /* Processes page settings */
700 TaskManagerSettings.ShowProcessesFromAllUsers = FALSE; /* Server-only? */
701 TaskManagerSettings.Column_ImageName = TRUE;
702 TaskManagerSettings.Column_PID = TRUE;
703 TaskManagerSettings.Column_CPUUsage = TRUE;
704 TaskManagerSettings.Column_CPUTime = TRUE;
705 TaskManagerSettings.Column_MemoryUsage = TRUE;
706 TaskManagerSettings.Column_MemoryUsageDelta = FALSE;
707 TaskManagerSettings.Column_PeakMemoryUsage = FALSE;
708 TaskManagerSettings.Column_PageFaults = FALSE;
709 TaskManagerSettings.Column_USERObjects = FALSE;
710 TaskManagerSettings.Column_IOReads = FALSE;
711 TaskManagerSettings.Column_IOReadBytes = FALSE;
712 TaskManagerSettings.Column_SessionID = FALSE; /* Server-only? */
713 TaskManagerSettings.Column_UserName = FALSE; /* Server-only? */
714 TaskManagerSettings.Column_PageFaultsDelta = FALSE;
715 TaskManagerSettings.Column_VirtualMemorySize = FALSE;
716 TaskManagerSettings.Column_PagedPool = FALSE;
717 TaskManagerSettings.Column_NonPagedPool = FALSE;
718 TaskManagerSettings.Column_BasePriority = FALSE;
719 TaskManagerSettings.Column_HandleCount = FALSE;
720 TaskManagerSettings.Column_ThreadCount = FALSE;
721 TaskManagerSettings.Column_GDIObjects = FALSE;
722 TaskManagerSettings.Column_IOWrites = FALSE;
723 TaskManagerSettings.Column_IOWriteBytes = FALSE;
724 TaskManagerSettings.Column_IOOther = FALSE;
725 TaskManagerSettings.Column_IOOtherBytes = FALSE;
727 for (i = 0; i < 25; i++) {
728 TaskManagerSettings.ColumnOrderArray[i] = i;
730 TaskManagerSettings.ColumnSizeArray[0] = 105;
731 TaskManagerSettings.ColumnSizeArray[1] = 50;
732 TaskManagerSettings.ColumnSizeArray[2] = 107;
733 TaskManagerSettings.ColumnSizeArray[3] = 70;
734 TaskManagerSettings.ColumnSizeArray[4] = 35;
735 TaskManagerSettings.ColumnSizeArray[5] = 70;
736 TaskManagerSettings.ColumnSizeArray[6] = 70;
737 TaskManagerSettings.ColumnSizeArray[7] = 100;
738 TaskManagerSettings.ColumnSizeArray[8] = 70;
739 TaskManagerSettings.ColumnSizeArray[9] = 70;
740 TaskManagerSettings.ColumnSizeArray[10] = 70;
741 TaskManagerSettings.ColumnSizeArray[11] = 70;
742 TaskManagerSettings.ColumnSizeArray[12] = 70;
743 TaskManagerSettings.ColumnSizeArray[13] = 70;
744 TaskManagerSettings.ColumnSizeArray[14] = 60;
745 TaskManagerSettings.ColumnSizeArray[15] = 60;
746 TaskManagerSettings.ColumnSizeArray[16] = 60;
747 TaskManagerSettings.ColumnSizeArray[17] = 60;
748 TaskManagerSettings.ColumnSizeArray[18] = 60;
749 TaskManagerSettings.ColumnSizeArray[19] = 70;
750 TaskManagerSettings.ColumnSizeArray[20] = 70;
751 TaskManagerSettings.ColumnSizeArray[21] = 70;
752 TaskManagerSettings.ColumnSizeArray[22] = 70;
753 TaskManagerSettings.ColumnSizeArray[23] = 70;
754 TaskManagerSettings.ColumnSizeArray[24] = 70;
756 TaskManagerSettings.SortColumn = 1;
757 TaskManagerSettings.SortAscending = TRUE;
759 /* Performance page settings */
760 TaskManagerSettings.CPUHistory_OneGraphPerCPU = TRUE;
761 TaskManagerSettings.ShowKernelTimes = FALSE;
764 if (RegOpenKeyEx(HKEY_CURRENT_USER, szSubKey, 0, KEY_READ, &hKey) != ERROR_SUCCESS)
766 /* Read the settings */
767 dwSize = sizeof(TASKMANAGER_SETTINGS);
768 RegQueryValueEx(hKey, _T("Preferences"), NULL, NULL, (LPBYTE)&TaskManagerSettings, &dwSize);
774 void SaveSettings(void)
777 TCHAR szSubKey1[] = _T("Software");
778 TCHAR szSubKey2[] = _T("Software\\ReactWare");
779 TCHAR szSubKey3[] = _T("Software\\ReactWare\\TaskManager");
781 /* Open (or create) the key */
783 RegCreateKeyEx(HKEY_CURRENT_USER, szSubKey1, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
786 RegCreateKeyEx(HKEY_CURRENT_USER, szSubKey2, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
789 if (RegCreateKeyEx(HKEY_CURRENT_USER, szSubKey3, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL) != ERROR_SUCCESS)
791 /* Save the settings */
792 RegSetValueEx(hKey, _T("Preferences"), 0, REG_BINARY, (LPBYTE)&TaskManagerSettings, sizeof(TASKMANAGER_SETTINGS));
797 void TaskManager_OnEnterMenuLoop(HWND hWnd)
801 /* Update the status bar pane sizes */
803 SendMessage(hStatusWnd, SB_SETPARTS, 1, (long)&nParts);
805 SendMessage(hStatusWnd, SB_SETTEXT, (WPARAM)0, (LPARAM)_T(""));
808 void TaskManager_OnExitMenuLoop(HWND hWnd)
815 /* Update the status bar pane sizes */
816 GetClientRect(hWnd, &rc);
819 nParts[2] = rc.right;
820 SendMessage(hStatusWnd, SB_SETPARTS, 3, (long)nParts);
821 SendMessage(hStatusWnd, SB_SETTEXT, 0, (LPARAM)_T(""));
822 wsprintf(text, _T("CPU Usage: %3d%%"), PerfDataGetProcessorUsage());
823 SendMessage(hStatusWnd, SB_SETTEXT, 1, (LPARAM)text);
824 wsprintf(text, _T("Processes: %d"), PerfDataGetProcessCount());
825 SendMessage(hStatusWnd, SB_SETTEXT, 0, (LPARAM)text);
828 void TaskManager_OnMenuSelect(HWND hWnd, UINT nItemID, UINT nFlags, HMENU hSysMenu)
832 _tcscpy(str, TEXT(""));
833 if (LoadString(hInst, nItemID, str, 100)) {
834 /* load appropriate string */
836 /* first newline terminates actual string */
837 lpsz = _tcschr(lpsz, '\n');
841 SendMessage(hStatusWnd, SB_SETTEXT, 0, (LPARAM)str);
844 void TaskManager_OnViewUpdateSpeedHigh(void)
848 HMENU hUpdateSpeedMenu;
850 hMenu = GetMenu(hMainWnd);
851 hViewMenu = GetSubMenu(hMenu, 2);
852 hUpdateSpeedMenu = GetSubMenu(hViewMenu, 1);
854 TaskManagerSettings.UpdateSpeed = 1;
855 CheckMenuRadioItem(hUpdateSpeedMenu, ID_VIEW_UPDATESPEED_HIGH, ID_VIEW_UPDATESPEED_PAUSED, ID_VIEW_UPDATESPEED_HIGH, MF_BYCOMMAND);
857 KillTimer(hMainWnd, 1);
858 SetTimer(hMainWnd, 1, 1000, NULL);
861 void TaskManager_OnViewUpdateSpeedNormal(void)
865 HMENU hUpdateSpeedMenu;
867 hMenu = GetMenu(hMainWnd);
868 hViewMenu = GetSubMenu(hMenu, 2);
869 hUpdateSpeedMenu = GetSubMenu(hViewMenu, 1);
871 TaskManagerSettings.UpdateSpeed = 2;
872 CheckMenuRadioItem(hUpdateSpeedMenu, ID_VIEW_UPDATESPEED_HIGH, ID_VIEW_UPDATESPEED_PAUSED, ID_VIEW_UPDATESPEED_NORMAL, MF_BYCOMMAND);
874 KillTimer(hMainWnd, 1);
875 SetTimer(hMainWnd, 1, 2000, NULL);
878 void TaskManager_OnViewUpdateSpeedLow(void)
882 HMENU hUpdateSpeedMenu;
884 hMenu = GetMenu(hMainWnd);
885 hViewMenu = GetSubMenu(hMenu, 2);
886 hUpdateSpeedMenu = GetSubMenu(hViewMenu, 1);
888 TaskManagerSettings.UpdateSpeed = 4;
889 CheckMenuRadioItem(hUpdateSpeedMenu, ID_VIEW_UPDATESPEED_HIGH, ID_VIEW_UPDATESPEED_PAUSED, ID_VIEW_UPDATESPEED_LOW, MF_BYCOMMAND);
891 KillTimer(hMainWnd, 1);
892 SetTimer(hMainWnd, 1, 4000, NULL);
895 void TaskManager_OnViewRefresh(void)
897 PostMessage(hMainWnd, WM_TIMER, 0, 0);
900 void TaskManager_OnViewUpdateSpeedPaused(void)
904 HMENU hUpdateSpeedMenu;
906 hMenu = GetMenu(hMainWnd);
907 hViewMenu = GetSubMenu(hMenu, 2);
908 hUpdateSpeedMenu = GetSubMenu(hViewMenu, 1);
909 TaskManagerSettings.UpdateSpeed = 0;
910 CheckMenuRadioItem(hUpdateSpeedMenu, ID_VIEW_UPDATESPEED_HIGH, ID_VIEW_UPDATESPEED_PAUSED, ID_VIEW_UPDATESPEED_PAUSED, MF_BYCOMMAND);
911 KillTimer(hMainWnd, 1);
914 void TaskManager_OnTabWndSelChange(void)
922 hMenu = GetMenu(hMainWnd);
923 hViewMenu = GetSubMenu(hMenu, 2);
924 hOptionsMenu = GetSubMenu(hMenu, 1);
925 TaskManagerSettings.ActiveTabPage = TabCtrl_GetCurSel(hTabWnd);
926 for (i = GetMenuItemCount(hViewMenu) - 1; i > 2; i--) {
927 hSubMenu = GetSubMenu(hViewMenu, i);
929 DestroyMenu(hSubMenu);
930 RemoveMenu(hViewMenu, i, MF_BYPOSITION);
932 RemoveMenu(hOptionsMenu, 3, MF_BYPOSITION);
933 switch (TaskManagerSettings.ActiveTabPage) {
935 ShowWindow(hApplicationPage, SW_SHOW);
936 ShowWindow(hProcessPage, SW_HIDE);
937 ShowWindow(hPerformancePage, SW_HIDE);
938 BringWindowToTop(hApplicationPage);
939 AppendMenu(hViewMenu, MF_STRING, ID_VIEW_LARGE, _T("Lar&ge Icons"));
940 AppendMenu(hViewMenu, MF_STRING, ID_VIEW_SMALL, _T("S&mall Icons"));
941 AppendMenu(hViewMenu, MF_STRING, ID_VIEW_DETAILS, _T("&Details"));
943 if (GetMenuItemCount(hMenu) <= 4) {
944 hSubMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_WINDOWSMENU));
945 InsertMenu(hMenu, 3, MF_BYPOSITION|MF_POPUP, (UINT)hSubMenu, _T("&Windows"));
946 DrawMenuBar(hMainWnd);
948 if (TaskManagerSettings.View_LargeIcons)
949 CheckMenuRadioItem(hViewMenu, ID_VIEW_LARGE, ID_VIEW_DETAILS, ID_VIEW_LARGE, MF_BYCOMMAND);
950 else if (TaskManagerSettings.View_SmallIcons)
951 CheckMenuRadioItem(hViewMenu, ID_VIEW_LARGE, ID_VIEW_DETAILS, ID_VIEW_SMALL, MF_BYCOMMAND);
953 CheckMenuRadioItem(hViewMenu, ID_VIEW_LARGE, ID_VIEW_DETAILS, ID_VIEW_DETAILS, MF_BYCOMMAND);
955 * Give the application list control focus
957 SetFocus(hApplicationPageListCtrl);
961 ShowWindow(hApplicationPage, SW_HIDE);
962 ShowWindow(hProcessPage, SW_SHOW);
963 ShowWindow(hPerformancePage, SW_HIDE);
964 BringWindowToTop(hProcessPage);
965 AppendMenu(hViewMenu, MF_STRING, ID_VIEW_SELECTCOLUMNS, _T("&Select Columns..."));
966 AppendMenu(hOptionsMenu, MF_STRING, ID_OPTIONS_SHOW16BITTASKS, _T("&Show 16-bit tasks"));
967 if (TaskManagerSettings.Show16BitTasks)
968 CheckMenuItem(hOptionsMenu, ID_OPTIONS_SHOW16BITTASKS, MF_BYCOMMAND|MF_CHECKED);
969 if (GetMenuItemCount(hMenu) > 4)
971 RemoveMenu(hMenu, 3, MF_BYPOSITION);
972 DrawMenuBar(hMainWnd);
975 * Give the process list control focus
977 SetFocus(hProcessPageListCtrl);
981 ShowWindow(hApplicationPage, SW_HIDE);
982 ShowWindow(hProcessPage, SW_HIDE);
983 ShowWindow(hPerformancePage, SW_SHOW);
984 BringWindowToTop(hPerformancePage);
985 if (GetMenuItemCount(hMenu) > 4) {
986 RemoveMenu(hMenu, 3, MF_BYPOSITION);
987 DrawMenuBar(hMainWnd);
989 hSubMenu = CreatePopupMenu();
990 AppendMenu(hSubMenu, MF_STRING, ID_VIEW_CPUHISTORY_ONEGRAPHALL, _T("&One Graph, All CPUs"));
991 AppendMenu(hSubMenu, MF_STRING, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, _T("One Graph &Per CPU"));
992 AppendMenu(hViewMenu, MF_STRING|MF_POPUP, (UINT)hSubMenu, _T("&CPU History"));
993 AppendMenu(hViewMenu, MF_STRING, ID_VIEW_SHOWKERNELTIMES, _T("&Show Kernel Times"));
994 if (TaskManagerSettings.ShowKernelTimes)
995 CheckMenuItem(hViewMenu, ID_VIEW_SHOWKERNELTIMES, MF_BYCOMMAND|MF_CHECKED);
997 CheckMenuItem(hViewMenu, ID_VIEW_SHOWKERNELTIMES, MF_BYCOMMAND|MF_UNCHECKED);
998 if (TaskManagerSettings.CPUHistory_OneGraphPerCPU)
999 CheckMenuRadioItem(hSubMenu, ID_VIEW_CPUHISTORY_ONEGRAPHALL, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, MF_BYCOMMAND);
1001 CheckMenuRadioItem(hSubMenu, ID_VIEW_CPUHISTORY_ONEGRAPHALL, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, ID_VIEW_CPUHISTORY_ONEGRAPHALL, MF_BYCOMMAND);
1003 * Give the tab control focus
1010 LPTSTR GetLastErrorText(LPTSTR lpszBuf, DWORD dwSize)
1013 LPTSTR lpszTemp = NULL;
1015 dwRet = FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_ARGUMENT_ARRAY,
1023 /* supplied buffer is not long enough */
1024 if (!dwRet || ( (long)dwSize < (long)dwRet+14)) {
1025 lpszBuf[0] = TEXT('\0');
1027 lpszTemp[lstrlen(lpszTemp)-2] = TEXT('\0'); /*remove cr and newline character */
1028 _stprintf(lpszBuf, TEXT("%s (0x%x)"), lpszTemp, (int)GetLastError());
1031 LocalFree((HLOCAL)lpszTemp);