home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / dialog / isdlgmsg.c < prev    next >
C/C++ Source or Header  |  1988-08-10  |  4KB  |  153 lines

  1. /*
  2.  *   IsDialogMessage
  3.  *
  4.  *   This program demonstrates the use of the IsDialogMessage function.
  5.  *   The IsDialogMessage function is usually used in a window's message
  6.  *   loop to determine if messages are intended for modeless dialog boxes.
  7.  *   If a message it intended for the given dialog box, IsDialogMessage
  8.  *   processes the mesage for the dialog box. IsDialogMessage is called
  9.  *   from WinMain in this sample application
  10.  */
  11.  
  12. #include "windows.h"
  13. #include "isdlgmsg.h"
  14.  
  15. HANDLE hInst;
  16. HWND hToTheDialog = NULL;     /*** handle to the modeless dialog box ***/
  17.  
  18. FARPROC lpprocDialog;
  19. long    FAR PASCAL HelloWndProc (HWND, unsigned, WORD, LONG);
  20.  
  21.  
  22. BOOL FAR PASCAL DialogBoxProc (hDlg, message, wParam, lParam)
  23. HWND hDlg;
  24. unsigned    message;
  25. WORD wParam;
  26. LONG lParam;
  27. {
  28.   switch (message)
  29.   {
  30.   case WM_INITDIALOG:
  31.     return TRUE;
  32.     break;
  33.  
  34.   case WM_COMMAND:
  35.     switch (wParam)
  36.     {
  37.     case TOP_CONTROL:
  38.       MessageBox (NULL, (LPSTR)"The message was processed",
  39.           (LPSTR)" ", MB_OK);
  40.       break;
  41.  
  42.     case ID_OK:
  43.       EndDialog (hDlg, TRUE);
  44.       break;
  45.     }
  46.     break;
  47.  
  48.   case WM_DESTROY:
  49.     hToTheDialog = NULL;
  50.     return TRUE;
  51.     break;
  52.  
  53.   default:
  54.     return FALSE;
  55.     break;
  56.   }
  57. }
  58.  
  59.  
  60. /* Procedure called when the application is loaded for the first time */
  61. BOOL HelloInit (hInstance)
  62. HANDLE hInstance;
  63. {
  64.   PWNDCLASS   pHelloClass;
  65.  
  66.   pHelloClass = (PWNDCLASS)LocalAlloc (LPTR, sizeof (WNDCLASS));
  67.  
  68.   pHelloClass->hCursor        = LoadCursor (NULL, IDC_ARROW);
  69.   pHelloClass->hIcon          = LoadIcon (hInstance, NULL);
  70.   pHelloClass->lpszMenuName   = (LPSTR)NULL;
  71.   pHelloClass->lpszClassName  = (LPSTR)"Sample Application";
  72.   pHelloClass->hbrBackground  = (HBRUSH)GetStockObject (WHITE_BRUSH);
  73.   pHelloClass->hInstance      = hInstance;
  74.   pHelloClass->style          = CS_HREDRAW | CS_VREDRAW;
  75.   pHelloClass->lpfnWndProc    = HelloWndProc;
  76.  
  77.   if (!RegisterClass ( (LPWNDCLASS)pHelloClass))
  78.     return FALSE;
  79.  
  80.   LocalFree ( (HANDLE)pHelloClass);
  81.   return TRUE;        /* Initialization succeeded */
  82. }
  83.  
  84.  
  85. int    PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  86. HANDLE    hInstance, hPrevInstance;
  87. LPSTR     lpszCmdLine;
  88. int    cmdShow;
  89. {
  90.   MSG   msg;
  91.   HWND  hWnd;
  92.   HMENU hMenu;
  93.  
  94.   HelloInit (hInstance);
  95.   hMenu = LoadMenu (hInstance, "isdlgmsg");
  96.  
  97.   hWnd = CreateWindow ( (LPSTR)"Sample Application", (LPSTR)"IsDialogMessage",
  98.       WS_OVERLAPPEDWINDOW,
  99.       CW_USEDEFAULT, 0,
  100.       CW_USEDEFAULT, 0,
  101.       NULL, hMenu, hInstance, NULL);
  102.   hInst = hInstance;
  103.  
  104.   ShowWindow (hWnd, cmdShow);
  105.   UpdateWindow (hWnd);
  106.  
  107.   while (GetMessage ( (LPMSG) & msg, NULL, 0, 0))
  108.   {
  109.     if (hToTheDialog == NULL || !IsDialogMessage (hToTheDialog, &msg))
  110.     {/*  Make sure the message is meant for this window...  */
  111.  
  112.       TranslateMessage ( (LPMSG) & msg);
  113.       DispatchMessage ( (LPMSG) & msg);
  114.     }
  115.   }
  116.   return (int)msg.wParam;
  117. }
  118.  
  119.  
  120. /* Procedures which make up the window class. */
  121. long    FAR PASCAL HelloWndProc (hWnd, message, wParam, lParam)
  122. HWND hWnd;
  123. unsigned    message;
  124. WORD wParam;
  125. LONG lParam;
  126. {
  127.   PAINTSTRUCT ps;
  128.  
  129.   switch (message)
  130.   {
  131.   case WM_COMMAND:
  132.     if (wParam == 80)
  133.     {
  134.       lpprocDialog = MakeProcInstance ( (FARPROC)DialogBoxProc, hInst);
  135.       hToTheDialog = CreateDialog (hInst, MAKEINTRESOURCE (2), hWnd,
  136.           lpprocDialog);
  137.     }
  138.     break;
  139.  
  140.   case WM_DESTROY:
  141.     FreeProcInstance ( (FARPROC)lpprocDialog);
  142.     PostQuitMessage (0);
  143.     break;
  144.  
  145.   default:
  146.     return DefWindowProc (hWnd, message, wParam, lParam);
  147.     break;
  148.   }
  149.   return (0L);
  150. }
  151.  
  152.  
  153.