home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / memory / global / globcomp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  5.3 KB  |  189 lines

  1. /*
  2.  
  3. Function(s) demonstrated in this program: GlobalCompact
  4.  
  5. Windows version:  2.03
  6.  
  7. Windows SDK version:  2.00
  8.  
  9. Compiler version:  C 5.10
  10.  
  11. Description:  This function compacts global memory.
  12.  
  13. Additional Comments:
  14.  
  15. */
  16.  
  17. #define NOMINMAX
  18. #include <windows.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include "GlobComp.h"
  22.  
  23. HWND     hWndParent1;
  24. HANDLE   hInstMain;
  25.  
  26. char     szOutputBuffer1 [70];
  27. char     szOutputBuffer2 [500];
  28.  
  29.  
  30. /****************************************************************************/
  31. /************************    Message Structure      *************************/
  32. /****************************************************************************/
  33.  
  34. struct { char *szMessage; }
  35.        Messages [] = {
  36. "About",
  37. "     This is a sample application to demonstrate the\n\
  38. use of the GlobalCompact Windows function.",
  39.  
  40. "Help Message",
  41. "     This program uses the GlobalCompact Windows\n\
  42. function to compact global memory.  Use the menu\n\
  43. to invoke this function.",
  44.  
  45. };    
  46.  
  47. /****************************************************************************/
  48.  
  49. void ProcessMessage (HWND, int); 
  50.  
  51. void ProcessMessage (hWnd, MessageNumber) 
  52.      HWND     hWnd;
  53.      int      MessageNumber;
  54. {
  55.      sprintf (szOutputBuffer1, "%s", Messages [MessageNumber]);
  56.      sprintf (szOutputBuffer2, "%s", Messages [MessageNumber + 1]);
  57.      MessageBox (hWnd, szOutputBuffer2, szOutputBuffer1, MB_OK);
  58. }       
  59.  
  60. /****************************************************************************/
  61.  
  62. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  63.      HANDLE      hInstance, hPrevInstance ;
  64.      LPSTR       lpszCmdLine ;
  65.      int         nCmdShow ;
  66.      {
  67.      static char szAppName [] = "GlobComp" ;
  68.      HWND        hWnd ;
  69.      WNDCLASS    wndclass ;
  70.      MSG msg;
  71.      short       xScreen, yScreen ;
  72.  
  73.      if (!hPrevInstance) 
  74.           {
  75.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  76.           wndclass.lpfnWndProc   = WndProc ;
  77.           wndclass.cbClsExtra    = 0 ;
  78.           wndclass.cbWndExtra    = 0 ;
  79.           wndclass.hInstance     = hInstance ;
  80.           wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  81.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  82.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  83.           wndclass.lpszMenuName  = szAppName ;
  84.           wndclass.lpszClassName = szAppName ;
  85.  
  86.           if (!RegisterClass (&wndclass))
  87.                return FALSE ;
  88.           }
  89.  
  90.      xScreen = GetSystemMetrics (SM_CXSCREEN) ;
  91.      yScreen = GetSystemMetrics (SM_CYSCREEN) ;
  92.  
  93.      hWndParent1 = CreateWindow (szAppName,     /* window class name       */
  94.                     "GlobalCompact",            /* window caption          */
  95.                     WS_OVERLAPPEDWINDOW,        /* window style            */
  96.                     CW_USEDEFAULT,              /* initial x position      */
  97.                     0,                          /* initial y position      */
  98.                     CW_USEDEFAULT,              /* initial x size          */
  99.                     0,                          /* initial y size          */
  100.                     NULL,                       /* parent window handle    */
  101.                     NULL,                       /* window menu handle      */
  102.                     hInstance,                  /* program instance handle */
  103.                     NULL) ;                     /* create parameters       */
  104.  
  105.      ShowWindow (hWndParent1, nCmdShow) ;
  106.      UpdateWindow (hWndParent1) ;
  107.  
  108.      hInstMain = hInstance;
  109.  
  110.      while (GetMessage(&msg, NULL, 0, 0))
  111.      {
  112.       TranslateMessage(&msg);
  113.       DispatchMessage(&msg);
  114.      } 
  115.      return (msg.wParam) ;     
  116.      }
  117.  
  118. /****************************************************************************/
  119.  
  120. long FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  121. HWND     hWnd ;
  122. unsigned iMessage ;
  123. WORD     wParam ;
  124. LONG     lParam ;
  125. {
  126.  HMENU       hMenu;
  127.  HDC         hDC;
  128.  PAINTSTRUCT ps;
  129.  static int  xClient, yClient;
  130.  
  131.  DWORD ContigFreeBytes;
  132.  
  133.  switch(iMessage)
  134.  {
  135.   case WM_CREATE:
  136.        hMenu = GetSystemMenu (hWnd, FALSE);
  137.  
  138.        ChangeMenu (hMenu, NULL, "&About", IDM_ABOUT, 
  139.                    MF_APPEND | MF_STRING);
  140.        break;
  141.  
  142.   case WM_SYSCOMMAND:
  143.        switch (wParam) {
  144.           case IDM_ABOUT:
  145.                ProcessMessage (hWnd, 0);
  146.                break;
  147.           default:
  148.                return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  149.        }
  150.        break;
  151.  
  152.   case WM_COMMAND:
  153.        switch (wParam) {
  154.           case IDM_GLOBALCOMPACT:
  155.                ContigFreeBytes = (DWORD)GlobalCompact((DWORD)0);
  156.                sprintf(szOutputBuffer1, "%s%lu",
  157.                    "Number of contiguous free bytes = ",
  158.                    ContigFreeBytes);
  159.            
  160.                MessageBox(hWnd, szOutputBuffer1, "GlobalCompact", MB_OK);
  161.  
  162.                break;
  163.  
  164.           case IDM_HELP:
  165.                ProcessMessage (hWnd, 2);
  166.                break;
  167.        }
  168.        break;
  169.  
  170.   case WM_PAINT:
  171.        BeginPaint(hWnd, (LPPAINTSTRUCT)&ps);
  172.        EndPaint(hWnd, (LPPAINTSTRUCT)&ps);
  173.        break;
  174.  
  175.   case WM_DESTROY:
  176.        PostQuitMessage(0);
  177.        break;
  178.  
  179.   default:
  180.   {
  181.    return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  182.   }
  183.  }
  184.  return (0L); 
  185. }
  186.  
  187.  
  188.  
  189.