home *** CD-ROM | disk | FTP | other *** search
/ Windows Shareware GOLD / NuclearComputingVol3No1.cdr / _bbs4 / f1231.zip / SYSGRAPH.C < prev    next >
C/C++ Source or Header  |  1990-12-15  |  8KB  |  308 lines

  1. /* SYSGRAPH  3.0
  2.  *
  3.  * Version 3.0 14-dec-90 J.R. Hasselbeck
  4.  *                 o  Option to show CPU loading rather or
  5.  *                a relative CPU freetime type of display.
  6.  *                 o  Rebuilt for windows 3.0
  7.  *                 o  Added user selectable update rate.
  8.  *
  9.  * Version 2.0 4/20/88    (c) Copyright 1988, Bill Crow, Hewlett-Packard Co
  10.  *             Renamed from SYSQUEUE to SYSGRAPH
  11.  *                       Added dynnamic scaling
  12.  *                       Added menu commands for rescaling and Auto Rescaling
  13.  *                       Added About... Dialog box.
  14.  *
  15.  * Previous versions:
  16.  *
  17.  * SysQueue.C -- Windows application that displays system load
  18.  * by Barry Press, adapted from FreeMem written
  19.  * by Charles Petzold as printed in Microsoft Systems Journal
  20.  *
  21.  * Adapted for Graphic Display by Alec Saunders
  22.  * Graph goes down when load increases and rises when
  23.  * load decreases.
  24.  */
  25.  
  26.  
  27. #include <windows.h>    /* all Windows functions */
  28. #include <stdlib.h>    /* ltoa */
  29. #include <string.h>    /* strcat & strlen */
  30. #include "sysgraph.h"   /* RC defines */
  31.  
  32. #define MAXCOUNT 25
  33.  
  34. static HANDLE globhInst;
  35.  
  36. static int  lLoopCount[MAXCOUNT];
  37. static int  iMaxSize=0;
  38.  
  39. static BOOL bRescale=FALSE;
  40. static BOOL bAutoRescale=FALSE;
  41. static short nCurrentType = IDD_FREE;
  42. static short nCurrentUpdate = IDD_1SEC;
  43. static int lTmpCnt = 0;
  44. static int smpcnt = 2; /* 2 samples per 1 sec def update */
  45.  
  46.  
  47. long FAR PASCAL WndProc     (HWND, unsigned, WORD, LONG);
  48. BOOL FAR PASCAL AboutDlgProc(HWND, unsigned, WORD, LONG);
  49.  
  50. /* WinMain - Main loop for all Windows applications */
  51. int PASCAL WinMain ( hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  52. HANDLE  hInstance, hPrevInstance;
  53. LPSTR   lpszCmdLine;
  54. int     nCmdShow;
  55. {
  56.     static   char szAppName [] = "SysGraph";
  57.     WNDCLASS WndClass;
  58.     HWND     hWnd;
  59.     MSG      msg;
  60.         HMENU    hMenu;
  61.     int     i;
  62.  
  63.     /* allow only one instance */
  64.     if (hPrevInstance)
  65.        return FALSE;
  66.  
  67.         globhInst = hInstance;
  68.  
  69.     /* define Window class */
  70.     WndClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  71.     WndClass.hIcon         = NULL;
  72.     WndClass.cbClsExtra    = 0;
  73.     WndClass.cbWndExtra    = 0;
  74.     WndClass.lpszMenuName  = NULL;
  75.     WndClass.lpszClassName = szAppName;
  76.     WndClass.hbrBackground = GetStockObject( WHITE_BRUSH );
  77.     WndClass.hInstance     = hInstance;
  78.     WndClass.style         = CS_VREDRAW | CS_HREDRAW;
  79.     WndClass.lpfnWndProc   = WndProc;
  80.  
  81.     /* register this new class with WINDOWS */
  82.     if (!RegisterClass( &WndClass ))
  83.        return FALSE;   /* Initialization failed */
  84.  
  85.     /* create window */
  86.     hWnd = CreateWindow( szAppName,
  87.             szAppName,
  88.             WS_TILEDWINDOW,
  89.             0, 0, 0, 0,
  90.             (HWND)   NULL,
  91.             (HMENU)  NULL,
  92.             hInstance,
  93.             NULL);
  94.  
  95.     /* set up timer (1 update/sec) */
  96.     if (!SetTimer (hWnd, 1, 1000, NULL) )
  97.        return FALSE;
  98.  
  99.     hMenu = GetSystemMenu(hWnd,FALSE);
  100.     ChangeMenu(hMenu, 0, NULL, 999, MF_APPEND | MF_SEPARATOR);
  101.     ChangeMenu(hMenu,0,(LPSTR)"R&escale", IDRESCALE,
  102.             MF_APPEND | MF_STRING | (bAutoRescale? MF_GRAYED : MF_ENABLED));
  103.     ChangeMenu(hMenu,0,(LPSTR)"A&uto Rescale", IDAUTORESCALE, 
  104.             MF_APPEND | MF_STRING | (bAutoRescale? MF_CHECKED : MF_UNCHECKED));
  105.     ChangeMenu(hMenu,0,(LPSTR)"A&bout...",IDABOUT, MF_APPEND | MF_STRING);
  106.        
  107.         /* show window (start in ICONIC state) */
  108.     ShowWindow( hWnd, SHOW_ICONWINDOW );
  109.     UpdateWindow( hWnd );
  110.  
  111.     /* initialize loop count which is bumped in loop */
  112.     for(i=0;i<MAXCOUNT;lLoopCount[i++] = 0)
  113.         ;
  114.  
  115.     /* main program loop */
  116.     while (TRUE) {
  117.          lTmpCnt++;
  118.         if (PeekMessage( &msg, NULL, 0, 0, TRUE )) {
  119.             if (msg.message == WM_QUIT)
  120.                 break;
  121.             TranslateMessage( &msg );
  122.             DispatchMessage( &msg );
  123.         }
  124.     }
  125.  
  126.     /* exit back to Windows */
  127.     return (int) msg.wParam;
  128.  
  129. } /* end WinMain */
  130.  
  131. /* WndProc - Window procedure for main window */
  132. long FAR PASCAL WndProc (hWnd, message, wParam, lParam)
  133. HWND     hWnd;
  134. unsigned message;
  135. WORD     wParam;
  136. LONG     lParam;
  137. {
  138.  
  139. char    buffer[MAXCOUNT];
  140. PAINTSTRUCT ps;
  141. RECT    rect;
  142. HMENU   hMenu;
  143. int    i;
  144. static int lupdate;
  145.  
  146.     /* switch on message type sent to window by Windows dispatcher */
  147.     switch(message) {
  148.  
  149.        case WM_TIMER:
  150.            if (lupdate != nCurrentUpdate)
  151.             {
  152.             KillTimer (hWnd, 1);
  153.         lupdate = nCurrentUpdate;
  154.         SetTimer (hWnd, 1, (lupdate - 9) * 1000, NULL);
  155.         }
  156.  
  157.            lLoopCount[MAXCOUNT - 1] = lTmpCnt / smpcnt;
  158.               if (iMaxSize<lLoopCount[MAXCOUNT-1])
  159.                   iMaxSize=lLoopCount[MAXCOUNT-1];
  160.           for (i=0; i<(MAXCOUNT - 1); i++)
  161.               lLoopCount[i] = lLoopCount[i+1];
  162.            lTmpCnt = 0;
  163.               InvalidateRect( hWnd, NULL, TRUE );
  164.           break;
  165.  
  166.        case WM_PAINT:
  167.           BeginPaint( hWnd, &ps );
  168.           GetClientRect( hWnd, &rect );
  169.           SetMapMode( ps.hdc, MM_ANISOTROPIC);
  170.               if (bRescale | bAutoRescale) {
  171.          iMaxSize=0; 
  172.          for (i=0;i<MAXCOUNT;i++) {
  173.             if (iMaxSize<lLoopCount[i])
  174.             iMaxSize=lLoopCount[i];
  175.          }
  176.                  bRescale=FALSE;
  177.               }
  178.           SetWindowOrg( ps.hdc, 0, 0);
  179.           SetWindowExt( ps.hdc, MAXCOUNT, iMaxSize);
  180.           SetViewportOrg(ps.hdc, 0, 0);
  181.           SetViewportExt(ps.hdc, rect.right - rect.left,
  182.                         rect.bottom - rect.top);
  183.  
  184.            if (nCurrentType == IDD_LOAD)
  185.             {
  186.             MoveTo( ps.hdc, 0, lLoopCount[0]);
  187.             for (i = 1; i < (MAXCOUNT - 1); i++)
  188.                 LineTo( ps.hdc, i, lLoopCount[i]);
  189.                 }
  190.           else
  191.             {
  192.             MoveTo( ps.hdc, 0, iMaxSize - lLoopCount[0]);
  193.             for (i = 1; i < (MAXCOUNT - 1); i++)
  194.                 LineTo( ps.hdc, i, iMaxSize - lLoopCount[i]);
  195.                 }
  196.  
  197.           EndPaint( hWnd, &ps );
  198.           break;
  199.  
  200.           case WM_SYSCOMMAND:
  201.         switch (wParam)
  202.         {
  203.           case IDABOUT:
  204.             DialogBox(globhInst,
  205.               (LPSTR)"AboutBox",
  206.               hWnd,
  207.               MakeProcInstance( (FARPROC)AboutDlgProc, globhInst));
  208.             break;
  209.  
  210.           case IDRESCALE:
  211.             bRescale=TRUE;
  212.                     break;
  213.  
  214.           case IDAUTORESCALE:
  215.             bAutoRescale = ! bAutoRescale;
  216.             hMenu = GetSystemMenu(hWnd,FALSE);
  217.             CheckMenuItem(hMenu,
  218.                IDAUTORESCALE,
  219.                        MF_BYCOMMAND | 
  220.                          (bAutoRescale? MF_CHECKED : MF_UNCHECKED));
  221.             EnableMenuItem(hMenu,
  222.                IDRESCALE,
  223.                MF_BYCOMMAND | 
  224.                          (bAutoRescale? MF_GRAYED : MF_ENABLED));
  225.             break;
  226.  
  227.           default:
  228.             return DefWindowProc(hWnd,message,wParam,lParam);  
  229.             break;
  230.  
  231.         }
  232.         break;
  233.  
  234.        case WM_DESTROY:
  235.           KillTimer (hWnd, 1);
  236.           PostQuitMessage( 0 );
  237.           break;
  238.  
  239.        default:
  240.           return DefWindowProc( hWnd, message, wParam, lParam);
  241.     }
  242.  
  243.     return (long) 0;
  244.  
  245. } /* end WndProc */
  246.  
  247.  
  248.  
  249. BOOL FAR PASCAL AboutDlgProc(hDlg, imessage, wParam, lParam )
  250. HWND hDlg;
  251. unsigned imessage;
  252. WORD wParam;
  253. LONG lParam;
  254. {
  255.  
  256. static HWND hCtrlBlock;
  257. static short nupdate;    /* Update rate */
  258. static short ntype;
  259.  
  260. switch (imessage)
  261.   {
  262.   case WM_INITDIALOG:    nupdate = nCurrentUpdate;
  263.                 ntype = nCurrentType;
  264.             CheckRadioButton (hDlg, IDD_1SEC, IDD_5SEC, nupdate);
  265.             CheckRadioButton (hDlg, IDD_FREE, IDD_LOAD, ntype);
  266.             hCtrlBlock = GetDlgItem(hDlg, IDD_PAINT);
  267.             SetFocus (GetDlgItem(hDlg, nupdate));
  268.             return FALSE;
  269.  
  270.   case WM_COMMAND:
  271.     switch (wParam)
  272.       {
  273.       case IDOK:    nCurrentType = ntype;
  274.                   nCurrentUpdate = nupdate;
  275.             EndDialog (hDlg, TRUE);
  276.                  smpcnt = 10;
  277.              if (nupdate == IDD_1SEC)
  278.               smpcnt = 2;
  279.             if (nupdate == IDD_2SEC)
  280.               smpcnt = 4;
  281.  
  282.             break;
  283.  
  284.       case IDCANCEL:    EndDialog (hDlg, TRUE);
  285.                   break;
  286.  
  287.       case IDD_1SEC:
  288.       case IDD_2SEC:
  289.       case IDD_5SEC:
  290.                   nupdate = wParam;
  291.             CheckRadioButton (hDlg, IDD_1SEC, IDD_5SEC, wParam);
  292.             break;
  293.  
  294.       case IDD_FREE:
  295.       case IDD_LOAD:
  296.                   ntype = wParam;
  297.             CheckRadioButton (hDlg, IDD_FREE, IDD_LOAD, wParam);
  298.             break;
  299.  
  300.       default:        return FALSE;
  301.       } /* end switch */
  302.     break;
  303.  
  304.   default:      return FALSE;
  305.   } /* end switch */
  306. return TRUE;
  307. } /* end of procedure AboutDlgProc() */
  308.