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

  1. /*
  2.  
  3. Function(s) demonstrated in this program: WaitMessage
  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 yields control to other applications when an
  12.     application has no other tasks to perform.  The WaitMessage function
  13.      suspends the application and does not return until a new message is
  14.      placed in the application's queue.
  15.  
  16. Additional Comments:
  17.  
  18. */
  19.  
  20. #define NOMINMAX
  21. #include <windows.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include "WaitMsg.h"
  25.  
  26.  
  27. HWND     hWndMain;
  28. HANDLE   hInstMain;
  29.  
  30. char     szOutputBuffer1 [70];
  31. char     szOutputBuffer2 [500];
  32.  
  33.  
  34. /****************************************************************************/
  35. /************************    Message Structure      *************************/
  36. /****************************************************************************/
  37.  
  38. struct { char *szMessage; }
  39.        Messages [] = {
  40. "About",
  41. "     This is a sample application to demonstrate the\n\
  42. use  of  the  WaitMessage Windows function.",
  43.  
  44. "Help Message",
  45. "     This program uses the WaitMessage Windows\n\
  46. function to suspend processing of the current\n\
  47. Window function until the window receives another\n\
  48. Message.  To test this function type a character\n\
  49. making sure not to move the cursor.  At this\n\
  50. point the Window will be suspended until you give\n\
  51. it another message.  This can be done by simply\n\
  52. moving the cursor.",
  53.  
  54. };    
  55.  
  56. /****************************************************************************/
  57.  
  58. void ProcessMessage (HWND, int); 
  59.  
  60. void ProcessMessage (hWnd, MessageNumber) 
  61.      HWND     hWnd;
  62.      int      MessageNumber;
  63. {
  64.      sprintf (szOutputBuffer1, "%s", Messages [MessageNumber]);
  65.      sprintf (szOutputBuffer2, "%s", Messages [MessageNumber + 1]);
  66.      MessageBox (hWnd, szOutputBuffer2, szOutputBuffer1, MB_OK);
  67. }       
  68.  
  69. /****************************************************************************/
  70.  
  71. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  72.      HANDLE      hInstance, hPrevInstance ;
  73.      LPSTR       lpszCmdLine ;
  74.      int         nCmdShow ;
  75.      {
  76.      static char szAppName [] = "WaitMsg" ;
  77.      HWND        hWnd ;
  78.      WNDCLASS    wndclass ;
  79.      MSG msg;
  80.  
  81.      if (!hPrevInstance) 
  82.           {
  83.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  84.           wndclass.lpfnWndProc   = WndProc ;
  85.           wndclass.cbClsExtra    = 0 ;
  86.           wndclass.cbWndExtra    = 0 ;
  87.           wndclass.hInstance     = hInstance ;
  88.           wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  89.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  90.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  91.           wndclass.lpszMenuName  = szAppName ;
  92.           wndclass.lpszClassName = szAppName ;
  93.  
  94.           if (!RegisterClass (&wndclass))
  95.                return FALSE ;
  96.           }
  97.  
  98.      hWndMain = CreateWindow (szAppName,        /* window class name       */
  99.                     "WaitMessage",              /* window caption          */
  100.                     WS_OVERLAPPEDWINDOW,        /* window style            */
  101.                     CW_USEDEFAULT,              /* initial x position      */
  102.                     0,                          /* initial y position      */
  103.                     CW_USEDEFAULT,              /* initial x size          */
  104.                     0,                          /* initial y size          */
  105.                     NULL,                       /* parent window handle    */
  106.                     NULL,                       /* window menu handle      */
  107.                     hInstance,                  /* program instance handle */
  108.                     NULL) ;                     /* create parameters       */
  109.  
  110.      ShowWindow (hWndMain, nCmdShow) ;
  111.      UpdateWindow (hWndMain) ;
  112.  
  113.      hInstMain = hInstance;
  114.  
  115.      while (GetMessage(&msg, NULL, 0, 0))
  116.      {
  117.       TranslateMessage(&msg);
  118.       DispatchMessage(&msg);
  119.      } 
  120.      return (msg.wParam) ;     
  121.      }
  122.  
  123. /****************************************************************************/
  124.  
  125. long FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  126. HWND     hWnd ;
  127. unsigned iMessage ;
  128. WORD     wParam ;
  129. LONG     lParam ;
  130. {
  131.  HMENU       hMenu;
  132.  PAINTSTRUCT ps;
  133.  
  134.  switch(iMessage)
  135.  {
  136.   case WM_CREATE:
  137.        hMenu = GetSystemMenu (hWnd, FALSE);
  138.  
  139.        ChangeMenu (hMenu, NULL, "&About", IDM_ABOUT, 
  140.                    MF_APPEND | MF_STRING);
  141.        break;
  142.  
  143.   case WM_KEYUP:
  144.        WaitMessage ();
  145.        MessageBox (hWnd, "Window returned to regular processing",
  146.                    "WaitMessage",MB_OK);
  147.        break;
  148.  
  149.   case WM_SYSCOMMAND:
  150.        switch (wParam) {
  151.           case IDM_ABOUT:
  152.                ProcessMessage (hWnd, 0);
  153.                break;
  154.           default:
  155.                return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  156.        }
  157.        break;
  158.  
  159.   case WM_COMMAND:
  160.        switch (wParam) {
  161.           case IDM_HELP:
  162.                ProcessMessage (hWnd, 2);
  163.                break;
  164.        }
  165.        break;
  166.  
  167.   case WM_PAINT:
  168.        BeginPaint(hWnd, (LPPAINTSTRUCT)&ps);
  169.        EndPaint(hWnd, (LPPAINTSTRUCT)&ps);
  170.        break;
  171.  
  172.   case WM_DESTROY:
  173.        PostQuitMessage(0);
  174.        break;
  175.  
  176.   default:
  177.   {
  178.    return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  179.   }
  180.  }
  181.  return (0L); 
  182. }
  183.  
  184.                                                 
  185.