home *** CD-ROM | disk | FTP | other *** search
/ Windows Shareware GOLD / NuclearComputingVol3No1.cdr / utils / f1380 / memgauge.c < prev    next >
C/C++ Source or Header  |  1991-02-16  |  15KB  |  387 lines

  1. /****************************************************************************
  2.   
  3.     PROGRAM: memgauge.c
  4.   
  5.     PURPOSE: Free memory gauge
  6.   
  7.     FUNCTIONS:
  8.   
  9.     WinMain() - calls initialization function, processes message loop
  10.     InitApplication() - initializes window data and registers window
  11.     InitInstance() - saves instance handle and creates main window
  12.     MainWndProc() - processes messages
  13.     About() - processes messages for "About" dialog box
  14.   
  15.     COMMENTS:
  16.   
  17.         Windows can have several copies of your application running at the
  18.         same time.  The variable hInst keeps track of which instance this
  19.         application is so that processing will be to the correct window.
  20.   
  21. ****************************************************************************/
  22.   
  23. #include "windows.h"            /* required for all Windows applications */
  24. #include "memgauge.h"           /* specific to this program              */
  25. HPEN     hSolidPen;             /* pen handle         */
  26. HBRUSH   hOldBrush;             /* old brush handle   */
  27. HBRUSH   hMyBrush;              /* new brush handle   */
  28.  
  29. static long dwInstFreeMemK;
  30.  
  31. #define  TIMER_ID  1
  32. #define  MY_WIDTH  320
  33. #define  MY_HEIGHT 70
  34.   
  35. HANDLE hInst;                   /* current instance   */
  36.   
  37. /****************************************************************************
  38.   
  39.     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  40.   
  41.     PURPOSE: calls initialization function, processes message loop
  42.   
  43.     COMMENTS:
  44.   
  45.         Windows recognizes this function by name as the initial entry point
  46.         for the program.  This function calls the application initialization
  47.         routine, if no other instance of the program is running, and always
  48.         calls the instance initialization routine.  It then executes a message
  49.         retrieval and dispatch loop that is the top-level control structure
  50.         for the remainder of execution.  The loop is terminated when a WM_QUIT
  51.         message is received, at which time this function exits the application
  52.         instance by returning the value passed by PostQuitMessage().
  53.   
  54.         If this function must abort before entering the message loop, it
  55.         returns the conventional value NULL.
  56.   
  57. ****************************************************************************/
  58.   
  59. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  60. HANDLE hInstance;                /* current instance             */
  61. HANDLE hPrevInstance;            /* previous instance            */
  62. LPSTR lpCmdLine;                 /* command line                 */
  63. int nCmdShow;                    /* show-window type (open/icon) */
  64.     {
  65.     MSG msg;                     /* message                      */
  66.   
  67.     if (!hPrevInstance)          /* Other instances of app running?   */
  68.         if (!InitApplication(hInstance)) /* Initialize shared things  */
  69.             return (FALSE);      /* Exits if unable to initialize     */
  70.   
  71.     /* Perform initializations that apply to a specific instance      */
  72.   
  73.     if (!InitInstance(hInstance, nCmdShow))
  74.         return (FALSE);
  75.   
  76.     /* Acquire and dispatch messages until a WM_QUIT message is received. */
  77.   
  78.     while (GetMessage(&msg,    /* message structure              */
  79.         NULL,          /* handle of window receiving the message */
  80.         NULL,          /* lowest message to examine              */
  81.         NULL))         /* highest message to examine             */
  82.         {
  83.         TranslateMessage(&msg); /* Translates virtual key codes           */
  84.         DispatchMessage(&msg);  /* Dispatches message to window           */
  85.         }
  86.     return (msg.wParam);        /* Returns the value from PostQuitMessage */
  87.     }
  88.   
  89.   
  90. /****************************************************************************
  91.   
  92.     FUNCTION: InitApplication(HANDLE)
  93.   
  94.     PURPOSE: Initializes window data and registers window class
  95.   
  96.     COMMENTS:
  97.   
  98.         This function is called at initialization time only if no other
  99.         instances of the application are running.  This function performs
  100.         initialization tasks that can be done once for any number of running
  101.         instances.
  102.   
  103.         In this case, we initialize a window class by filling out a data
  104.         structure of type WNDCLASS and calling the Windows RegisterClass()
  105.         function.  Since all instances of this application use the same window
  106.         class, we only need to do this when the first instance is initialized.
  107.   
  108.   
  109. ****************************************************************************/
  110.   
  111. BOOL InitApplication(hInstance)
  112. HANDLE hInstance;                  /* current instance */
  113.     {
  114.     WNDCLASS  wc;
  115.   
  116.     /* Fill in window class structure with parameters that describe the       */
  117.     /* main window.                                                           */
  118.   
  119.     wc.style = NULL;                    /* Class style(s).                    */
  120.     wc.lpfnWndProc = MainWndProc;       /* Function to retrieve messages for  */
  121.                                         /* windows of this class.             */
  122.     wc.cbClsExtra = 0;                  /* No per-class extra data.           */
  123.     wc.cbWndExtra = 0;                  /* No per-window extra data.          */
  124.     wc.hInstance  = hInstance;          /* Application that owns the class.   */
  125.  
  126.     #if 0
  127.     wc.hIcon      = LoadIcon(hInstance, "memgauge"); /* fixed icon */
  128.     #endif
  129.  
  130.     wc.hIcon      = NULL;               /* This will cause the icon to be
  131.                                            drawn, which allows updates while
  132.                                            the window is iconized */
  133.  
  134.     wc.hCursor    = LoadCursor(NULL, IDC_ARROW);
  135.     wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  136.     wc.lpszMenuName  = "MemGaugeMenu";  /* Name of menu resource in .RC file. */
  137.     wc.lpszClassName = "MemGaugeWClass";/* Name used in call to CreateWindow. */
  138.   
  139.     /* Register the window class and return success/failure code. */
  140.   
  141.     return (RegisterClass(&wc));
  142.   
  143.     }
  144.   
  145.   
  146. /****************************************************************************
  147.   
  148.     FUNCTION:  InitInstance(HANDLE, int)
  149.   
  150.     PURPOSE:  Saves instance handle and creates main window
  151.   
  152.     COMMENTS:
  153.   
  154.         This function is called at initialization time for every instance of
  155.         this application.  This function performs initialization tasks that
  156.         cannot be shared by multiple instances.
  157.   
  158.         In this case, we save the instance handle in a static variable and
  159.         create and display the main program window.
  160.   
  161. ****************************************************************************/
  162.   
  163. BOOL InitInstance(hInstance, nCmdShow)
  164. HANDLE          hInstance;          /* Current instance identifier.       */
  165. int             nCmdShow;           /* Param for first ShowWindow() call. */
  166.     {
  167.     HWND            hWnd;           /* Main window handle.                */
  168.  
  169.     char buffer[50];
  170.   
  171.     /* Save the instance handle in static variable, which will be used in  */
  172.     /* many subsequence calls from this application to Windows.            */
  173.   
  174.     hInst = hInstance;
  175.   
  176.     dwInstFreeMemK = (GetFreeSpace(0) / 1024);
  177.  
  178.     if (GetWinFlags() & WF_LARGEFRAME || GetWinFlags() & WF_SMALLFRAME)
  179.          dwInstFreeMemK += (GetFreeSpace(GMEM_NOT_BANKED) / 1024);
  180.  
  181.     /* Create a main window for this application instance.  */
  182.   
  183.     wsprintf(buffer, "MemGauge Started With %dK Free", dwInstFreeMemK);
  184.     
  185.     hWnd = CreateWindow(
  186.     "MemGaugeWClass",               /* See RegisterClass() call.          */
  187.     (LPSTR)buffer,
  188.     WS_OVERLAPPEDWINDOW,            /* Window style.                      */
  189.     CW_USEDEFAULT,                  /* Default horizontal position.       */
  190.     CW_USEDEFAULT,                  /* Default vertical position.         */
  191.     MY_WIDTH,                       /* Default width.                     */
  192.     MY_HEIGHT,                      /* Default height.                    */
  193.     NULL,                           /* Overlapped windows have no parent. */
  194.     NULL,                           /* Use the window class menu.         */
  195.     hInstance,                      /* This instance owns this window.    */
  196.     NULL                            /* Pointer not needed.                */
  197.     );
  198.   
  199.     /* If window could not be created, return "failure" */
  200.   
  201.     if (!hWnd)
  202.         return (FALSE);
  203.   
  204.     if (!SetTimer( hWnd, TIMER_ID, 1500, NULL ))
  205.         {
  206.         MessageBox( hWnd, "Too many timers in use.",
  207.                     "MemGaugeWClass", MB_ICONEXCLAMATION | MB_OK);
  208.         return FALSE;
  209.         }
  210.         
  211.     /* Make the window visible; update its client area; and return "success" */
  212.   
  213.     ShowWindow(hWnd, nCmdShow);  /* Show the window                        */
  214.     UpdateWindow(hWnd);          /* Sends WM_PAINT message                 */
  215.     return (TRUE);               /* Returns the value from PostQuitMessage */
  216.   
  217.     }
  218.   
  219. /****************************************************************************
  220.   
  221.     FUNCTION: MainWndProc(HWND, unsigned, WORD, LONG)
  222.   
  223.     PURPOSE:  Processes messages
  224.   
  225.     MESSAGES:
  226.   
  227.     WM_COMMAND    - application menu (About dialog box)
  228.     WM_DESTROY    - destroy window
  229.   
  230.     COMMENTS:
  231.   
  232.     To process the IDM_ABOUT message, call MakeProcInstance() to get the
  233.     current instance address of the About() function.  Then call Dialog
  234.     box which will create the box according to the information in your
  235.     memgauge.rc file and turn control over to the About() function. When
  236.     it returns, free the intance address.
  237.   
  238. ****************************************************************************/
  239.   
  240. long FAR PASCAL MainWndProc(hWnd, message, wParam, lParam)
  241. HWND hWnd;                    /* window handle               */
  242. unsigned message;             /* type of message             */
  243. WORD wParam;                  /* additional information      */
  244. LONG lParam;                  /* additional information      */
  245.     {
  246.     FARPROC lpProcAbout;      /* pointer to the "About" function */
  247.     PAINTSTRUCT ps;
  248.     HDC         hdc;
  249.     char outBuff[80];
  250.     static long dwFreeMem, dwPrevMem;
  251.     static RECT  rect;
  252.     static int nTop, nBottom, nLeft, nRight, nRectRight;
  253.     static int nGreen;
  254.     HPEN  hOldPen;  
  255.  
  256.   
  257.         switch (message) {
  258.         case WM_CREATE:
  259.             /* Create the pen */
  260.  
  261.             hSolidPen = CreatePen(PS_SOLID,              /* style */
  262.                 1,                                       /* width */
  263.                 RGB(0, 0, 0));                           /* color */
  264.  
  265.             break;
  266.  
  267.  
  268.         case WM_TIMER:
  269.             dwFreeMem = GetFreeSpace(0);
  270.  
  271.             if (GetWinFlags() & WF_LARGEFRAME || GetWinFlags() & WF_SMALLFRAME)
  272.                 dwFreeMem += GetFreeSpace(GMEM_NOT_BANKED);
  273.  
  274.             if (dwFreeMem != dwPrevMem)
  275.                 InvalidateRect (hWnd, NULL, TRUE);
  276.  
  277.             dwPrevMem = dwFreeMem;
  278.             return (0);
  279.             
  280.         case WM_COMMAND:       /* message: command from application menu */
  281.                 if (wParam == IDM_ABOUT) {
  282.                 lpProcAbout = MakeProcInstance(About, hInst);
  283.   
  284.                 DialogBox(hInst,         /* current instance         */
  285.                 "AboutBox",              /* resource to use          */
  286.                 hWnd,                    /* parent handle            */
  287.                 lpProcAbout);            /* About() instance address */
  288.   
  289.                 FreeProcInstance(lpProcAbout);
  290.                 break;
  291.                 }
  292.             else                         /* Lets Windows process it  */
  293.                 return (DefWindowProc(hWnd, message, wParam, lParam));
  294.  
  295.         case WM_SIZE:
  296.              InvalidateRect(hWnd, NULL, TRUE);
  297.              break;
  298.  
  299.         case WM_PAINT:
  300.             hdc = BeginPaint (hWnd, &ps);
  301.  
  302.             GetClientRect(hWnd, &rect);
  303.  
  304.             nTop = rect.top + ((rect.bottom - rect.top) / 4);
  305.             nBottom = rect.bottom - ((rect.bottom - rect.top) / 4);
  306.             nLeft = rect.left + ((rect.right - rect.left) / 10);
  307.             nRight = rect.right - ((rect.right - rect.left) / 10);
  308.  
  309.  
  310.             hOldPen = SelectObject(hdc, hSolidPen);
  311.  
  312.             Rectangle(hdc, nLeft, nTop, nRight, nBottom);
  313.  
  314.             SelectObject(hdc, hOldPen);
  315.  
  316.             nGreen = ((dwFreeMem / 1024) * 255 ) / dwInstFreeMemK;
  317.             
  318.             hMyBrush = CreateSolidBrush(RGB(255 - nGreen, nGreen, 0));
  319.  
  320.             hOldBrush = SelectObject(hdc, hMyBrush);
  321.  
  322.             if((nRectRight = (((dwInstFreeMemK - (dwFreeMem / 1024)) * (nRight - nLeft)) / dwInstFreeMemK) + nLeft) <= nLeft )
  323.                 nRectRight = nLeft;
  324.  
  325.             Rectangle(hdc, nLeft, nTop, nRectRight, nBottom);
  326.         
  327.             SelectObject(hdc, hOldBrush);
  328.  
  329.             DeleteObject(hMyBrush);
  330.  
  331.             EndPaint(hWnd, &ps);
  332.             return( 0 );
  333.  
  334.         case WM_DESTROY:          /* message: window being destroyed */
  335.             KillTimer (hWnd, TIMER_ID);
  336.             DeleteObject(hSolidPen);
  337.             PostQuitMessage(0);
  338.             break;
  339.   
  340.         default:                  /* Passes it on if unproccessed    */
  341.             return (DefWindowProc(hWnd, message, wParam, lParam));
  342.         }
  343.     return (NULL);
  344.     }
  345.   
  346.   
  347. /****************************************************************************
  348.   
  349.     FUNCTION: About(HWND, unsigned, WORD, LONG)
  350.   
  351.     PURPOSE:  Processes messages for "About" dialog box
  352.   
  353.     MESSAGES:
  354.   
  355.     WM_INITDIALOG - initialize dialog box
  356.     WM_COMMAND    - Input received
  357.   
  358.     COMMENTS:
  359.   
  360.     No initialization is needed for this particular dialog box, but TRUE
  361.     must be returned to Windows.
  362.   
  363.     Wait for user to click on "Ok" button, then close the dialog box.
  364.   
  365. ****************************************************************************/
  366.   
  367. BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
  368. HWND hDlg;                                /* window handle of the dialog box */
  369. unsigned message;                         /* type of message                 */
  370. WORD wParam;                              /* message-specific information    */
  371. LONG lParam;
  372.     {
  373.         switch (message) {
  374.         case WM_INITDIALOG:               /* message: initialize dialog box  */
  375.             return (TRUE);
  376.   
  377.         case WM_COMMAND:                      /* message: received a command */
  378.             if (wParam == IDOK                /* "OK" box selected?          */
  379.                 || wParam == IDCANCEL) {      /* System menu close command?  */
  380.                 EndDialog(hDlg, TRUE);        /* Exits the dialog box        */
  381.                 return (TRUE);
  382.                 }
  383.             break;
  384.         }
  385.     return (FALSE);                       /* Didn't process a message        */
  386.     }
  387.