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

  1. /*
  2. Windows Version 2.03 function demonstration application
  3.  
  4. Function(s) demonstrated in this program: SetMessageQueue
  5.  
  6. Compiler version: 5.01
  7.  
  8. Description:
  9.  
  10. This program calls SetMessageQueue to destroy the old message queue
  11. and create a new message queue which can hold up to ten messages. The
  12. default message queue can hold only eight messages.  The results of
  13. the call to SetMessageQueue are displayed on the screen.  If the call
  14. works then a new message queue is created and the old message queue
  15. is destroyed, along with any messages it might contain.  If the call
  16. fails then the old message queue is still destroyed but a new message
  17. queue has not been created.  In the latter case the application must
  18. continue calling SetMessageQueue with a smaller queue size until the
  19. function returns a nonzero value.
  20.  
  21.  
  22. ****************************************************************************/
  23.  
  24. #include <windows.h>
  25. #include <string.h>
  26.  
  27. long FAR PASCAL WndProc (HWND, unsigned, WORD, LONG) ;
  28.  
  29. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  30.      HANDLE      hInstance, hPrevInstance ;
  31.      LPSTR       lpszCmdLine ;
  32.      int         nCmdShow ;
  33.      {
  34.      static char szAppName [] = "SetMessageQueue" ;
  35.      HWND        hWnd ;
  36.      WNDCLASS    wndclass ;
  37.      MSG msg;
  38.  
  39.      if (!hPrevInstance) 
  40.           {
  41.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  42.           wndclass.lpfnWndProc   = WndProc ;
  43.           wndclass.cbClsExtra    = 0 ;
  44.           wndclass.cbWndExtra    = 0 ;
  45.           wndclass.hInstance     = hInstance ;
  46.           wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  47.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  48.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  49.           wndclass.lpszMenuName  = NULL ;
  50.           wndclass.lpszClassName = szAppName ;
  51.  
  52.           if (!RegisterClass (&wndclass))
  53.                return FALSE ;
  54.           }
  55.  
  56.      hWnd = CreateWindow (szAppName,            /* window class name       */
  57.                     "SetMessageQueue",          /* window caption          */
  58.                     WS_OVERLAPPEDWINDOW,        /* window style            */
  59.                     CW_USEDEFAULT,              /* initial x position      */
  60.                     0,                          /* initial y position      */
  61.                     CW_USEDEFAULT,              /* initial x size          */
  62.                     0,                          /* initial y size          */
  63.                     NULL,                       /* parent window handle    */
  64.                     NULL,                       /* window menu handle      */
  65.                     hInstance,                  /* program instance handle */
  66.                     NULL) ;                     /* create parameters       */
  67.  
  68.      ShowWindow (hWnd, nCmdShow) ;
  69.  
  70.      UpdateWindow (hWnd) ;
  71.  
  72.      while (GetMessage(&msg, NULL, 0, 0))
  73.      {
  74.       TranslateMessage(&msg);
  75.       DispatchMessage(&msg);
  76.      } 
  77.      return (msg.wParam) ;     
  78.      }
  79.  
  80. long FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  81. HWND     hWnd ;
  82. unsigned iMessage ;
  83. WORD     wParam ;
  84. LONG     lParam ;
  85. {
  86.  PAINTSTRUCT     ps;
  87.  HDC             hDC;
  88.  TEXTMETRIC      tm;
  89.  static short    xChar,
  90.                  yChar;
  91.  short           iRow; /* An index to keep track of the current row *
  92.                         * for text output
  93.                        */
  94.  
  95.  switch(iMessage)
  96.  {
  97.   case WM_CREATE:
  98.   {
  99.    hDC = GetDC (hWnd);
  100.    GetTextMetrics (hDC, &tm);
  101.    xChar = tm.tmAveCharWidth;
  102.    yChar = tm.tmHeight + tm.tmExternalLeading;
  103.    ReleaseDC (hWnd, hDC);
  104.    break;
  105.   }
  106.   case WM_PAINT:
  107.   {
  108.    hDC = BeginPaint(hWnd, (LPPAINTSTRUCT)&ps);
  109.  
  110.    iRow = 1;
  111.  
  112. LOOP:
  113.    if (SetMessageQueue(10) == 0)
  114.       {
  115.        TextOut (hDC,
  116.                xChar,
  117.                yChar * iRow,
  118.                (LPSTR)"SetMessageQueue has failed.  The application has no message queue!",
  119.                strlen("SetMessageQueue has failed.  The application has no message queue!")
  120.               );
  121.  
  122.       iRow++;
  123.       goto LOOP;
  124.       }
  125.    else
  126.       {
  127.        TextOut (hDC,
  128.                xChar,
  129.                yChar * iRow,
  130.                (LPSTR)"SetMessageQueue works.",
  131.                strlen("SetMessageQueue works.")
  132.               );
  133.  
  134.        TextOut (hDC,
  135.                xChar,
  136.                yChar * ++iRow,
  137.                (LPSTR)"The message queue for this application will now hold ten messages.",
  138.                strlen("The message queue for this application will now hold ten messages.")
  139.               );
  140.  
  141.        TextOut (hDC,
  142.                xChar,
  143.                yChar * ++iRow,
  144.                (LPSTR)"Before the SetMessageQueue call it only held eight messages.",
  145.                strlen("Before the SetMessageQueue call it only held eight messages.")
  146.               );
  147.  
  148.       }
  149.    EndPaint(hWnd, (LPPAINTSTRUCT)&ps);
  150.    break;
  151.   }
  152.   case WM_DESTROY:
  153.   {
  154.    PostQuitMessage(0);
  155.    break;
  156.   }
  157.   default:
  158.   {
  159.    return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  160.   }
  161.  }
  162.  return (0L); 
  163. }
  164.