home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / memory / stack / hello.c next >
Encoding:
C/C++ Source or Header  |  1988-12-15  |  5.4 KB  |  198 lines

  1. /*  Hello.c
  2.     Hello Application
  3.     Windows Toolkit Version 2.03
  4.     Copyright (c) Microsoft 1985,1986,1987,1988 */
  5.  
  6. #include "windows.h"
  7. #include "hello.h"
  8.  
  9. char szAppName[10];
  10. char szAbout[10];
  11. char szMessage[15];
  12. int  MessageLength;
  13.  
  14. static HANDLE hInst;
  15. FARPROC lpprocAbout;
  16.  
  17. void PASCAL InitStackUsed();
  18. int  PASCAL ReportStackUsed();
  19.  
  20. long FAR PASCAL HelloWndProc(HWND, unsigned, WORD, LONG);
  21.  
  22. BOOL FAR PASCAL About( hDlg, message, wParam, lParam )
  23. HWND hDlg;
  24. unsigned message;
  25. WORD wParam;
  26. LONG lParam;
  27. {
  28.     switch (message) {
  29.         case WM_INITDIALOG:
  30.             break;
  31.         case WM_COMMAND:
  32.             EndDialog(hDlg, TRUE);
  33.             break;
  34.         default:
  35.             return FALSE;
  36.             break;
  37.         }
  38.     return(TRUE);
  39. }
  40.  
  41.  
  42. void HelloPaint( hDC )
  43. HDC hDC;
  44. {
  45.   TextOut( hDC, 10, 10, "Determines stack size.", 22 );
  46. }
  47.  
  48.  
  49. /* Procedure called when the application is loaded for the first time */
  50. BOOL HelloInit( hInstance )
  51. HANDLE hInstance;
  52. {
  53.     PWNDCLASS   pHelloClass;
  54.  
  55.     /* Load strings from resource */
  56.     LoadString( hInstance, IDSNAME, (LPSTR)szAppName, 10 );
  57.     LoadString( hInstance, IDSABOUT, (LPSTR)szAbout, 10 );
  58.     MessageLength = LoadString( hInstance, IDSTITLE, (LPSTR)szMessage, 15 );
  59.  
  60.     pHelloClass = (PWNDCLASS)LocalAlloc( LPTR, sizeof(WNDCLASS) );
  61.  
  62.     pHelloClass->hCursor        = LoadCursor( NULL, IDC_ARROW );
  63.     pHelloClass->hIcon          = LoadIcon( hInstance, MAKEINTRESOURCE(HELLOICON) );
  64.     pHelloClass->lpszMenuName   = (LPSTR)szAppName;
  65.     pHelloClass->lpszClassName  = (LPSTR)szAppName;
  66.     pHelloClass->hbrBackground  = (HBRUSH)GetStockObject( WHITE_BRUSH );
  67.     pHelloClass->hInstance      = hInstance;
  68.     pHelloClass->style          = CS_HREDRAW | CS_VREDRAW;
  69.     pHelloClass->lpfnWndProc    = HelloWndProc;
  70.  
  71.     if (!RegisterClass( (LPWNDCLASS)pHelloClass ) )
  72.         /* Initialization failed.
  73.          * Windows will automatically deallocate all allocated memory.
  74.          */
  75.         return FALSE;
  76.  
  77.     LocalFree( (HANDLE)pHelloClass );
  78.     return TRUE;        /* Initialization succeeded */
  79. }
  80.  
  81.  
  82. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  83. HANDLE hInstance, hPrevInstance;
  84. LPSTR lpszCmdLine;
  85. int cmdShow;
  86. {
  87.     MSG   msg;
  88.     HWND  hWnd;
  89.     HMENU hMenu;
  90.  
  91.     InitStackUsed();
  92.  
  93.     if (!hPrevInstance) {
  94.         /* Call initialization procedure if this is the first instance */
  95.         if (!HelloInit( hInstance ))
  96.             return FALSE;
  97.         }
  98.     else {
  99.         /* Copy data from previous instance */
  100.         GetInstanceData( hPrevInstance, (PSTR)szAppName, 10 );
  101.         GetInstanceData( hPrevInstance, (PSTR)szAbout, 10 );
  102.         GetInstanceData( hPrevInstance, (PSTR)szMessage, 15 );
  103.         GetInstanceData( hPrevInstance, (PSTR)&MessageLength, sizeof(int) );
  104.         }
  105.  
  106.     hWnd = CreateWindow((LPSTR)szAppName,
  107.                         (LPSTR)szMessage,
  108.                          WS_OVERLAPPEDWINDOW,
  109.                          CW_USEDEFAULT,
  110.                          CW_USEDEFAULT,
  111.                          CW_USEDEFAULT,
  112.                          CW_USEDEFAULT,
  113.                         (HWND)NULL,        /* no parent */
  114.                         (HMENU)NULL,       /* use class menu */
  115.                         (HANDLE)hInstance, /* handle to window instance */
  116.                         (LPSTR)NULL        /* no params to pass on */
  117.                         );
  118.  
  119.     /* Save instance handle for DialogBox */
  120.     hInst = hInstance;
  121.  
  122.     /* Bind callback function with module instance */
  123.     lpprocAbout = MakeProcInstance( (FARPROC)About, hInstance );
  124.  
  125.     /* Insert "About..." into system menu */
  126.     hMenu = GetSystemMenu(hWnd, FALSE);
  127.     ChangeMenu(hMenu, 0, NULL, 999, MF_APPEND | MF_SEPARATOR);
  128.     ChangeMenu(hMenu, 0, (LPSTR)szAbout, IDSABOUT, MF_APPEND | MF_STRING);
  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.  
  143.  
  144. /* Procedures which make up the window class. */
  145. long FAR PASCAL HelloWndProc( hWnd, message, wParam, lParam )
  146. HWND hWnd;
  147. unsigned message;
  148. WORD wParam;
  149. LONG lParam;
  150. {
  151.     PAINTSTRUCT ps;
  152.     char  buf[20];
  153.     int   iStack;
  154.  
  155.     switch (message)
  156.     {
  157.     case WM_SYSCOMMAND:
  158.         switch (wParam)
  159.         {
  160.         case IDSABOUT:
  161.             DialogBox( hInst, MAKEINTRESOURCE(ABOUTBOX), hWnd, lpprocAbout );
  162.             break;
  163.         default:
  164.             return DefWindowProc( hWnd, message, wParam, lParam );
  165.         }
  166.         break;
  167.  
  168.     case WM_COMMAND:
  169.         switch (wParam)
  170.         {
  171.         case IDMCHECK:
  172.                iStack = ReportStackUsed();
  173.                itoa ( iStack, buf, 10);
  174.                MessageBox (hWnd, buf,"Stack =", MB_OK );
  175.             break;
  176.  
  177.         default:
  178.             return DefWindowProc( hWnd, message, wParam, lParam );
  179.         }
  180.         break;
  181.  
  182.     case WM_DESTROY:
  183.         PostQuitMessage( 0 );
  184.         break;
  185.  
  186.     case WM_PAINT:
  187.         BeginPaint( hWnd, (LPPAINTSTRUCT)&ps );
  188.         HelloPaint( ps.hdc );
  189.         EndPaint( hWnd, (LPPAINTSTRUCT)&ps );
  190.         break;
  191.  
  192.     default:
  193.         return DefWindowProc( hWnd, message, wParam, lParam );
  194.         break;
  195.     }
  196.     return(0L);
  197. }
  198.