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

  1. /*
  2.  *
  3.  *   FreeResource
  4.  *   freeres1.c travisd, v1.00, 29-Dec-1987
  5.  *
  6.  *   This program demonstrates the use of the function FreeResource.
  7.  *   FreeResource removes a loaded resource from memory by freeing the
  8.  *   alllocated memory occupied by that resource.
  9.  *
  10.  *   Microsoft Product Support Services
  11.  *   Windows Version 2.0 function demonstration application
  12.  *   Copyright (c) Microsoft 1987
  13.  *
  14.  */
  15.  
  16. #include "windows.h"
  17. #include "freeres1.h"
  18. #include "string.h"
  19.  
  20. char szAppName[10];
  21. static HANDLE hInst;
  22.  
  23. /* Forward-Reference All Functions */
  24.  
  25. long FAR PASCAL FreeRes1WndProc(HWND, unsigned, WORD, LONG);
  26.  
  27. /* --------------------------------------------------------------------- */
  28.  
  29. void FreeRes1Paint(hWnd, hDC )
  30. HWND hWnd;
  31. HDC  hDC;
  32.  
  33. {
  34.     GLOBALHANDLE hBitmap1Data; /* Handle to global memory containing bitmap */
  35.     HBITMAP hBitmap1Info;
  36.     BOOL nResult;
  37.  
  38. /***************************************************************************/
  39. /* This section demonstrates the use of FreeResource */
  40.  
  41.    /* Locate the bitmap in the resource file and identify it with a handle */
  42.     hBitmap1Info = FindResource(hInst, "face1", RT_BITMAP);
  43.  
  44.             /* Identify the global memory block to receive the bitmap data */
  45.     hBitmap1Data = LoadResource(hInst, hBitmap1Info);
  46.  
  47.     nResult = FreeResource(hBitmap1Info);         /* Free allocated memory */
  48.     if (nResult = FALSE)
  49.        {
  50.        TextOut( hDC, 10, 10, (LPSTR)"FreeResource did not work",
  51.                 strlen("FreeResource did not work") );
  52.        }
  53.     else
  54.        {
  55.        TextOut( hDC, 10, 10, (LPSTR)"FreeResource worked",
  56.                 strlen("FreeResource worked") );
  57.        }
  58.  
  59. /* End of the FreeResource demonstration section */
  60. /***************************************************************************/
  61. }
  62. /* End FreeRes1Paint */
  63. /* --------------------------------------------------------------------- */
  64.  
  65. /* Procedure called when the application is loaded for the first time */
  66. BOOL FreeRes1Init( hInstance )
  67. HANDLE hInstance;
  68. {
  69.     PWNDCLASS   pFreeRes1Class;
  70.  
  71.     /* Load strings from resource */
  72.     LoadString( hInstance, IDSNAME, (LPSTR)szAppName, 10 );
  73.  
  74.     pFreeRes1Class = (PWNDCLASS)LocalAlloc( LPTR, sizeof(WNDCLASS) );
  75.  
  76.     pFreeRes1Class->hCursor       = LoadCursor( NULL, IDC_ARROW );
  77.     pFreeRes1Class->hIcon         = LoadIcon( hInstance, MAKEINTRESOURCE(FREERES1ICON) );
  78.     pFreeRes1Class->lpszMenuName  = (LPSTR)NULL;
  79.     pFreeRes1Class->lpszClassName = (LPSTR)szAppName;
  80.     pFreeRes1Class->hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
  81.     pFreeRes1Class->hInstance     = hInstance;
  82.     pFreeRes1Class->style         = CS_HREDRAW | CS_VREDRAW;
  83.     pFreeRes1Class->lpfnWndProc   = FreeRes1WndProc;
  84.  
  85.     if (!RegisterClass( (LPWNDCLASS)pFreeRes1Class ) )
  86.         /* Initialization failed.
  87.          * Windows will automatically deallocate all allocated memory.
  88.          */
  89.         return FALSE;
  90.  
  91.     LocalFree( (HANDLE)pFreeRes1Class );
  92.     return TRUE;        /* Initialization succeeded */
  93. }
  94. /* End FreeRes1Init */
  95. /* --------------------------------------------------------------------- */
  96.  
  97. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  98. HANDLE hInstance, hPrevInstance;
  99. LPSTR lpszCmdLine;
  100. int cmdShow;
  101. {
  102.     MSG   msg;
  103.     HWND  hWnd;
  104.  
  105.     if (!hPrevInstance) {
  106.         /* Call initialization procedure if this is the first instance */
  107.         if (!FreeRes1Init( hInstance ))
  108.             return FALSE;
  109.         }
  110.     else {
  111.         /* Copy data from previous instance */
  112.         GetInstanceData( hPrevInstance, (PSTR)szAppName, 10 );
  113.         }
  114.     hWnd = CreateWindow((LPSTR)szAppName,
  115.                         (LPSTR)"FreeResource",
  116.                         WS_OVERLAPPEDWINDOW | WS_MAXIMIZE,
  117.                         10,              /*  x - ignored for tiled windows */
  118.                         10,              /*  y - ignored for tiled windows */
  119.                         CW_USEDEFAULT,   /* cx - ignored for tiled windows */
  120.                         CW_USEDEFAULT,   /* cy - ignored for tiled windows */
  121.                         (HWND)NULL,      /* no parent */
  122.                         (HMENU)NULL,     /* use class menu */
  123.                         (HANDLE)hInstance, /* handle to window instance */
  124.                         (LPSTR)NULL        /* no params to pass on */
  125.                         );
  126.  
  127.     /* Save instance handle for DialogBox */
  128.     hInst = hInstance;
  129.  
  130.     /* Make window visible according to the way the app is activated */
  131.     ShowWindow( hWnd, cmdShow );
  132.     UpdateWindow( hWnd );
  133.  
  134.     /* Polling messages from event queue */
  135.     while (GetMessage((LPMSG)&msg, NULL, 0, 0)) {
  136.         TranslateMessage((LPMSG)&msg);
  137.         DispatchMessage((LPMSG)&msg);
  138.         }
  139.  
  140.     return (int)msg.wParam;
  141. }
  142. /* End WinMain */
  143. /* --------------------------------------------------------------------- */
  144.  
  145. /* Procedures which make up the window class. */
  146. long FAR PASCAL FreeRes1WndProc( hWnd, message, wParam, lParam )
  147. HWND hWnd;
  148. unsigned message;
  149. WORD wParam;
  150. LONG lParam;
  151. {
  152.     PAINTSTRUCT ps;
  153.  
  154.     switch (message)
  155.     {
  156.     case WM_DESTROY:
  157.         PostQuitMessage( 0 );
  158.         break;
  159.  
  160.     case WM_PAINT:
  161.         BeginPaint( hWnd, (LPPAINTSTRUCT)&ps );
  162.         FreeRes1Paint(hWnd, ps.hdc );
  163.         EndPaint( hWnd, (LPPAINTSTRUCT)&ps );
  164.         break;
  165.  
  166.     default:
  167.         return DefWindowProc( hWnd, message, wParam, lParam );
  168.         break;
  169.     }
  170.     return(0L);
  171. }
  172.  
  173. /* End FreeRes1WndProc */
  174. /* --------------------------------------------------------------------- */
  175.