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

  1. /*
  2.  *  PeekMessage
  3.  *
  4.  *  This program will demonstrate the use of PeekMessage by catching all
  5.  *  of the WM_PAINT messages and beeping, using MessageBeep, when there
  6.  *  is one in the queue.
  7.  *
  8.  */
  9.  
  10. #include "windows.h" 
  11. #include "peekmsg.h" 
  12.  
  13. HANDLE hInst;
  14.  
  15. int    PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  16. HANDLE hInstance;
  17. HANDLE hPrevInstance;
  18. LPSTR lpCmdLine;
  19. int    nCmdShow;
  20. {
  21.   BOOL bStatus;
  22.   HWND hWnd;
  23.   MSG msg;
  24.  
  25.   if (!hPrevInstance)
  26.     if (!PeekMessageInit(hInstance))
  27.       return (NULL);
  28.  
  29.   hInst = hInstance;   /* Saves the current instance      */
  30.  
  31.   hWnd = CreateWindow((LPSTR)"PeekMessage", (LPSTR)"PeekMessage Sample Application",  
  32.       WS_OVERLAPPEDWINDOW,  
  33.       CW_USEDEFAULT,  0,
  34.       CW_USEDEFAULT,  0,
  35.       NULL,  NULL,  hInstance, NULL);
  36.  
  37.   if (!hWnd)
  38.     return (NULL);
  39.  
  40.   ShowWindow(hWnd, nCmdShow);
  41.   UpdateWindow(hWnd);
  42.  
  43.   while (GetMessage(&msg, NULL, NULL, NULL))
  44.   {
  45.     TranslateMessage(&msg);
  46.     DispatchMessage(&msg);
  47.     bStatus = PeekMessage(&msg,   /*  Message Structure  */
  48.                           hWnd,   /*  Handle of window receiving the message  */
  49.                           WM_PAINT,  /*  Lowest Message to examine  */
  50.                           WM_PAINT,  /*  Highest Message to examine  */
  51.                           PM_NOREMOVE);  /*  Don't remove the message  */
  52.  
  53. /*  To get a WM_PAINT message to be sent, size the window in different ways.
  54.  *  Using the Diagonal sizing seems to give the best results  */
  55.  
  56.     if (bStatus)
  57.       MessageBeep(MB_OK);  /*  We got a message, beep!!!  */
  58.   }
  59.   return (msg.wParam);    /* Returns the value from PostQuitMessage */
  60. }
  61.  
  62.  
  63. BOOL PeekMessageInit(hInstance)
  64. HANDLE hInstance;
  65. {
  66.   HANDLE hMemory;          /* handle to allocated memory */
  67.   PWNDCLASS pWndClass;
  68.   BOOL bSuccess;          /* RegisterClass() result     */
  69.  
  70.   hMemory = LocalAlloc(LPTR, sizeof(WNDCLASS));
  71.   pWndClass = (PWNDCLASS) LocalLock(hMemory);
  72.  
  73.   pWndClass->style = NULL;
  74.   pWndClass->lpfnWndProc = PeekMessageWndProc;
  75.   pWndClass->hInstance = hInstance;
  76.   pWndClass->hIcon = LoadIcon(NULL, IDI_APPLICATION);
  77.   pWndClass->hCursor = LoadCursor(NULL, IDC_ARROW);
  78.   pWndClass->hbrBackground = GetStockObject(WHITE_BRUSH);
  79.   pWndClass->lpszMenuName = (LPSTR) NULL;
  80.   pWndClass->lpszClassName = (LPSTR) "PeekMessage";
  81.  
  82.   bSuccess = RegisterClass(pWndClass);
  83.  
  84.   LocalUnlock(hMemory);       /* Unlocks the memory    */
  85.   LocalFree(hMemory);        /* Returns it to Windows */
  86.  
  87.   return (bSuccess);   /* Returns result of registering the window */
  88. }
  89.  
  90.  
  91. long    FAR PASCAL PeekMessageWndProc(hWnd, message, wParam, lParam)
  92. HWND hWnd;
  93. unsigned    message;
  94. WORD wParam;
  95. LONG lParam;
  96. {
  97.   FARPROC lpProcAbout;
  98.   HMENU hMenu;
  99.  
  100.   switch (message)
  101.   {
  102.   case WM_SYSCOMMAND:
  103.     if (wParam == ID_ABOUT)
  104.     {
  105.       lpProcAbout = MakeProcInstance(About, hInst);
  106.  
  107.       DialogBox(hInst, "AboutBox", hWnd, lpProcAbout);
  108.  
  109.       FreeProcInstance(lpProcAbout);
  110.       break;
  111.     }
  112.  
  113.     else
  114.       return (DefWindowProc(hWnd, message, wParam, lParam));
  115.  
  116.   case WM_CREATE:
  117.  
  118.     hMenu = GetSystemMenu(hWnd, FALSE);
  119.  
  120. /* Add a separator to the menu */
  121.  
  122.     ChangeMenu(hMenu, NULL, NULL, NULL, MF_APPEND | MF_SEPARATOR);
  123.  
  124. /* Add new menu item to the System menu */
  125.  
  126.     ChangeMenu(hMenu, NULL, "A&bout PeekMessage...", ID_ABOUT,  
  127.         MF_APPEND | MF_STRING);
  128.     break;
  129.  
  130.   case WM_DESTROY:    /* message: window being destroyed */
  131.     PostQuitMessage(0);
  132.     break;
  133.  
  134.   default:
  135.     return (DefWindowProc(hWnd, message, wParam, lParam));
  136.   }
  137.   return (0L);
  138. }
  139.  
  140.  
  141. BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
  142. HWND hDlg;
  143. unsigned    message;
  144. WORD wParam;
  145. LONG lParam;
  146. {
  147.   switch (message)
  148.   {
  149.   case WM_INITDIALOG:     /* message: initialize dialog box */
  150.     return (TRUE);
  151.  
  152.   case WM_COMMAND:
  153.     if (wParam == IDOK)
  154.     {
  155.       EndDialog(hDlg, NULL);
  156.       return (TRUE);
  157.     }
  158.     break;
  159.   }
  160.   return (FALSE);
  161. }
  162.  
  163.  
  164.