home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / windows / winmem / winmem.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-07-16  |  6.5 KB  |  268 lines

  1. #include "windows.h"
  2. #include "hello.h"
  3. #define  LINT_ARGS
  4. #include <stdio.h>
  5.  
  6. char szAppName[10];
  7. char szAbout[10];
  8. char szMessage[15];
  9. int  MessageLength;
  10. char buffer[100];
  11.  
  12. DWORD MaxBlock, MemTot;
  13. int Nchar;
  14. HDC hDCmy;
  15. int Nblock;
  16.  
  17. static HANDLE hInst;
  18. FARPROC lpprocAbout;
  19.  
  20. long FAR PASCAL HelloWndProc(HWND, unsigned, WORD, LONG);
  21.  
  22.  
  23.  
  24. BOOL FAR PASCAL About( hDlg, message, wParam, lParam )
  25. HWND hDlg;    /*--------------------------------------*/
  26. unsigned message;
  27. WORD wParam;
  28. LONG lParam;
  29. {
  30.     if (message == WM_COMMAND) {
  31.     EndDialog( hDlg, TRUE );
  32.     return TRUE;
  33.     }
  34.     else if (message == WM_INITDIALOG)
  35.     return TRUE;
  36.     else return FALSE;
  37. }
  38.  
  39.  
  40.  
  41. void GetMem( hDCarg)
  42.    /*--------*/
  43. HDC hDCarg;
  44. {
  45. #define MaxBlk 100
  46. DWORD Bsize;
  47. HANDLE hMem, hMemSav[ MaxBlk];
  48. int NtoFree;
  49.  
  50. MaxBlock= 0L;
  51. MemTot=   0L;
  52. Nblock= -1;
  53.  
  54. while (TRUE) {
  55.   Bsize= GlobalCompact( 10000000);
  56.   if (Bsize <=0) break;
  57.   if (Bsize >MaxBlock) MaxBlock= Bsize;
  58.   MemTot += Bsize;
  59.   if (Nblock == MaxBlk-1) break;
  60.   hMem= GlobalAlloc( GMEM_FIXED, Bsize);
  61.   if (hMem == NULL) {
  62.     TextOut( hDCarg,
  63.          (short)  1,
  64.          (short) 10,
  65.          (LPSTR) "ERa",
  66.          (short) 3);
  67.     break;
  68.     }
  69.   hMemSav[ ++Nblock]= hMem;
  70.   }    /* end while getting blocks*/
  71.  
  72. NtoFree= Nblock;
  73. while ( NtoFree >=0)  {
  74.   hMem= GlobalFree( hMemSav[ NtoFree--]);
  75.   if (hMem != NULL)
  76.     TextOut( hDCarg,
  77.          (short)  1,
  78.          (short) 10,
  79.          (LPSTR) "ERb",
  80.          (short) 3);
  81.   }    /* end while freeing blocks*/
  82. }
  83.  
  84.  
  85.  
  86. void HelloPaint( hDC, mess)
  87.    /*----------------------*/
  88. HDC hDC;
  89. unsigned mess;
  90. {
  91.     GetMem( hDC);    /* sets  MaxBlock, MemTot, Nblock    */
  92.     Nchar= sprintf( buffer,
  93.        "Repainted:  max block= %lu      ",
  94.        MaxBlock);
  95.     TextOut( hDC,
  96.          (short)1,
  97.          (short)1,
  98.          (LPSTR)buffer,
  99.          (short)Nchar);
  100.     Nchar= sprintf( buffer,
  101.           "%d block(s) total  %lu      ",
  102.           Nblock+1, MemTot);
  103.     TextOut( hDC,
  104.          (short)31,
  105.          (short)10,
  106.          (LPSTR)buffer,
  107.          (short)Nchar);
  108. }
  109.  
  110.  
  111.  
  112. BOOL HelloInit( hInstance )
  113.    /*----------------------
  114.    Procedure called when the application is loaded for the first time */
  115. HANDLE hInstance;
  116. {PWNDCLASS   pHelloClass;
  117.  
  118.     /* Load strings from resource */
  119.     LoadString( hInstance, IDSNAME, (LPSTR)szAppName, 10 );
  120.     LoadString( hInstance, IDSABOUT, (LPSTR)szAbout, 10 );
  121.     MessageLength = LoadString( hInstance, IDSTITLE, (LPSTR)szMessage, 15 );
  122.  
  123.     pHelloClass = (PWNDCLASS)LocalAlloc( LPTR, sizeof(WNDCLASS) );
  124.  
  125.     pHelloClass->hCursor    = LoadCursor( NULL, IDC_ARROW );
  126.     pHelloClass->hIcon        = LoadIcon( hInstance, (LPSTR)szAppName );
  127.     pHelloClass->lpszMenuName    = (LPSTR)NULL;
  128.     pHelloClass->lpszClassName    = (LPSTR)szAppName;
  129.     pHelloClass->hbrBackground    = (HBRUSH)GetStockObject( WHITE_BRUSH );
  130.     pHelloClass->hInstance    = hInstance;
  131.     pHelloClass->style        = CS_HREDRAW | CS_VREDRAW;
  132.     pHelloClass->lpfnWndProc    = HelloWndProc;
  133.  
  134.     if (!RegisterClass( (LPWNDCLASS)pHelloClass ) )
  135.     /* Initialization failed.
  136.      * Windows will automatically deallocate all allocated memory.
  137.      */
  138.     return FALSE;
  139.  
  140.     LocalFree( (HANDLE)pHelloClass );
  141.     return TRUE;    /* Initialization succeeded */
  142. }
  143.  
  144.  
  145.  
  146. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  147.      /*--------------------------------------------------------*/
  148. HANDLE hInstance, hPrevInstance;
  149. LPSTR lpszCmdLine;
  150. int cmdShow;
  151. {
  152.     MSG   msg;
  153.     HWND  hWnd;
  154.     HMENU hMenu;
  155.  
  156.     if (!hPrevInstance) {
  157.     /* Call initialization procedure if this is the first instance */
  158.     if (!HelloInit( hInstance ))
  159.         return FALSE;
  160.     }
  161.     else {
  162.     /* Copy data from previous instance */
  163.     GetInstanceData( hPrevInstance, (PSTR)szAppName, 10 );
  164.     GetInstanceData( hPrevInstance, (PSTR)szAbout, 10 );
  165.     GetInstanceData( hPrevInstance, (PSTR)szMessage, 15 );
  166.     GetInstanceData( hPrevInstance, (PSTR)&MessageLength, sizeof(int) );
  167.     }
  168.  
  169.     hWnd = CreateWindow((LPSTR)szAppName,
  170.             (LPSTR)szMessage,
  171.             WS_TILEDWINDOW,
  172.             0,    /*  x - ignored for tiled windows */
  173.             0,    /*  y - ignored for tiled windows */
  174.             0,    /* cx - ignored for tiled windows */
  175.             0,    /* cy - ignored for tiled windows */
  176.             (HWND)NULL,       /* no parent */
  177.             (HMENU)NULL,       /* use class menu */
  178.             (HANDLE)hInstance, /* handle to window instance */
  179.             (LPSTR)NULL       /* no params to pass on */
  180.             );
  181.  
  182.     /* Save instance handle for DialogBox */
  183.     hInst = hInstance;
  184.  
  185.     /* Bind callback function with module instance */
  186.     lpprocAbout = MakeProcInstance( (FARPROC)About, hInstance );
  187.  
  188.     /* Insert "About..." into system menu */
  189.     hMenu = GetSystemMenu(hWnd, FALSE);
  190.     ChangeMenu(hMenu, 0, NULL, 999, MF_APPEND | MF_SEPARATOR);
  191.     ChangeMenu(hMenu, 0, (LPSTR)szAbout, IDSABOUT, MF_APPEND | MF_STRING);
  192.  
  193.     /* Make window visible according to the way the app is activated */
  194.     ShowWindow( hWnd, cmdShow );
  195.     UpdateWindow( hWnd );
  196.  
  197.  
  198.     /* Polling messages from event queue */
  199.     while (GetMessage((LPMSG)&msg, NULL, 0, 0)) {
  200.       if ( !PeekMessage( (LPMSG)&msg, hWnd, 0, 0, FALSE)) {  /* last one */
  201.     hDCmy= GetDC( hWnd);
  202.     GetMem( hDCmy);    /* sets  MaxBlock, MemTot, Nblock  */
  203.     Nchar= sprintf( buffer,
  204.            "Msg loop:   max block= %lu      ",
  205.            MaxBlock);
  206.     TextOut( hDCmy,
  207.          (short)1,
  208.          (short)1,
  209.          (LPSTR)buffer,
  210.          (short)Nchar);
  211.     Nchar= sprintf( buffer,
  212.          "%d block(s) total  %lu      ",
  213.          Nblock+1, MemTot);
  214.     TextOut( hDCmy,
  215.          (short)31,
  216.          (short)10,
  217.          (LPSTR)buffer,
  218.          (short)Nchar);
  219.     ReleaseDC(hWnd, hDCmy);
  220.     }   /* !Peek..    */
  221.       TranslateMessage( (LPMSG)&msg);
  222.       DispatchMessage(    (LPMSG)&msg);
  223.       }
  224.  
  225.     return (int)msg.wParam;
  226. }
  227.  
  228.  
  229. long FAR PASCAL HelloWndProc( hWnd, message, wParam, lParam)
  230. /*        --------------------------------------------
  231.    Procedures which make up the window class. */
  232. HWND hWnd;
  233. unsigned message;
  234. WORD wParam;
  235. LONG lParam;
  236. {
  237.     PAINTSTRUCT ps;
  238.  
  239.     switch (message)
  240.     {
  241.     case WM_SYSCOMMAND:
  242.     switch (wParam)
  243.     {
  244.     case IDSABOUT:
  245.         DialogBox( hInst, MAKEINTRESOURCE(ABOUTBOX), hWnd, lpprocAbout );
  246.         break;
  247.     default:
  248.         return DefWindowProc( hWnd, message, wParam, lParam );
  249.     }
  250.     break;
  251.  
  252.     case WM_DESTROY:
  253.     PostQuitMessage( 0 );
  254.     break;
  255.  
  256.     case WM_PAINT:
  257.     BeginPaint( hWnd, (LPPAINTSTRUCT)&ps );
  258.     HelloPaint( ps.hdc, message);
  259.     EndPaint( hWnd, (LPPAINTSTRUCT)&ps );
  260.     break;
  261.  
  262.     default:
  263.     return DefWindowProc( hWnd, message, wParam, lParam );
  264.     break;
  265.     }
  266.     return(0L);
  267. }
  268.