home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / catch.c next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  5.4 KB  |  185 lines

  1. /*
  2.  * Function(s) demonstrated in this program: Catch, Throw
  3.  * Compiler version:  C 5.10
  4.  * Description:  The Catch function catches the current execution environment
  5.  *    and stores it in a buffer.  The Throw function jump to an execution
  6.  *         state described by the parameter.
  7.  *
  8.  */
  9.  
  10. #define NOMINMAX
  11. #include <windows.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include "Catch.h"
  15.  
  16. HWND     hWndParent1;
  17. HANDLE   hInstMain;
  18.  
  19. char    szOutputBuffer1 [70];
  20. char    szOutputBuffer2 [500];
  21. CATCHBUF CatchBuf;
  22. int    CatchVal;
  23.  
  24. /****************************************************************************/
  25. /************************    Message Structure      *************************/
  26. /****************************************************************************/
  27.  
  28. struct   {
  29.   char    *szMessage;
  30.   } Messages [] =   {
  31.   "About",
  32.   "     This is a sample application to demonstrate the\n\
  33. use of the Catch and Throw Windows functions.",
  34.  
  35.   "Help Message",
  36.   "     This program uses the Catch Windows function\n\
  37. to copy the current state of the execution environment.\n\
  38. Then the Throw Windows function is used to return to\n\
  39. the original execution environment.  Use the menu to\n\
  40. invoke this function.",
  41.  
  42.   };
  43.  
  44.  
  45. /****************************************************************************/
  46.  
  47. void ProcessMessage (HWND, int);
  48.  
  49. void ProcessMessage (hWnd, MessageNumber)
  50. HWND    hWnd;
  51. int    MessageNumber;
  52.   {
  53.   sprintf (szOutputBuffer1, "%s", Messages [MessageNumber]);
  54.   sprintf (szOutputBuffer2, "%s", Messages [MessageNumber + 1]);
  55.   MessageBox (hWnd, szOutputBuffer2, szOutputBuffer1, MB_OK);
  56.   }
  57.  
  58.  
  59. /****************************************************************************/
  60.  
  61. int     PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  62. HANDLE      hInstance, hPrevInstance;
  63. LPSTR       lpszCmdLine;
  64. int    nCmdShow;
  65.   {
  66.   static char    szAppName [] = "Catch";
  67.   HWND        hWnd;
  68.   WNDCLASS    wndclass;
  69.   MSG msg;
  70.   short xScreen, yScreen;
  71.  
  72.   if (!hPrevInstance)
  73.     {
  74.     wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  75.     wndclass.lpfnWndProc   = WndProc;
  76.     wndclass.cbClsExtra    = 0;
  77.     wndclass.cbWndExtra    = 0;
  78.     wndclass.hInstance     = hInstance;
  79.     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION);
  80.     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
  81.     wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
  82.     wndclass.lpszMenuName  = szAppName;
  83.     wndclass.lpszClassName = szAppName;
  84.  
  85.     if (!RegisterClass (&wndclass))
  86.       return FALSE;
  87.     }
  88.  
  89.   xScreen = GetSystemMetrics (SM_CXSCREEN);
  90.   yScreen = GetSystemMetrics (SM_CYSCREEN);
  91.  
  92. hWndParent1 = CreateWindow (szAppName,         /* window class name       */
  93.                            "Catch N Throw",    /* window caption          */
  94.                            WS_OVERLAPPEDWINDOW,/* window style            */
  95.                            CW_USEDEFAULT,      /* initial x position      */
  96.                            0,                  /* initial y position      */
  97.                            CW_USEDEFAULT,      /* initial x size          */
  98.                            0,                  /* initial y size          */
  99.                            NULL,               /* parent window handle    */
  100.                            NULL,               /* window menu handle      */
  101.                            hInstance,          /* program instance handle */
  102.                            NULL);              /* create parameters       */
  103.  
  104.   ShowWindow (hWndParent1, nCmdShow);
  105.   UpdateWindow (hWndParent1);
  106.  
  107.   hInstMain = hInstance;
  108.  
  109.   while (GetMessage(&msg, NULL, 0, 0))
  110.     {
  111.     TranslateMessage(&msg);
  112.     DispatchMessage(&msg);
  113.     }
  114.   return (msg.wParam);
  115.   }
  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.  
  128.   switch (iMessage)
  129.     {
  130.     case WM_CREATE:
  131.       hMenu = GetSystemMenu (hWnd, FALSE);
  132.  
  133.       ChangeMenu (hMenu, NULL, "&About", IDM_ABOUT,
  134.                   MF_APPEND | MF_STRING);
  135.       break;
  136.  
  137.     case WM_SYSCOMMAND:
  138.       switch (wParam)
  139.         {
  140.         case IDM_ABOUT:
  141.           ProcessMessage (hWnd, 0);
  142.           break;
  143.         default:
  144.           return DefWindowProc (hWnd, iMessage, wParam, lParam);
  145.         }
  146.       break;
  147.  
  148.     case WM_COMMAND:
  149.       switch (wParam)
  150.         {
  151.         case IDM_CATCH:
  152.           if (CatchVal = Catch((LPCATCHBUF)CatchBuf))
  153.             {
  154.             switch (CatchVal)
  155.               {
  156.               case ID_ERROR:
  157.                 MessageBox(hWnd,
  158.                            "This message made possible by the Throw function",
  159.                            "Catch N Throw",
  160.                            MB_OK | MB_ICONHAND | MB_SYSTEMMODAL);
  161.                 break;
  162.               }
  163.             return 0L;
  164.             }
  165.           MessageBox (hWnd, "    About to use Throw to make a jump",
  166.                       "Catch N Throw", MB_OK);
  167.           Throw((LPCATCHBUF)CatchBuf, ID_ERROR);
  168.           break;
  169.  
  170.         case IDM_HELP:
  171.           ProcessMessage (hWnd, 2);
  172.           break;
  173.         }
  174.       break;
  175.  
  176.     case WM_DESTROY:
  177.       PostQuitMessage(0);
  178.       break;
  179.  
  180.       default:
  181.         return DefWindowProc (hWnd, iMessage, wParam, lParam);
  182.     }
  183.   return (0L);
  184.   }
  185.