home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / windows / sysgrf.zip / SYSGRAPH.C < prev    next >
Text File  |  1988-04-19  |  6KB  |  229 lines

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