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

  1. /*
  2.  *  PostMessage
  3.  *
  4.  *  This example program will use the PostMessage function to send a
  5.  *  WM_SYSCOMMAND message to the main window to make it iconic.  The
  6.  *  wParam parameter will be set to SC_MINIMIZE to accomplish this.
  7.  *
  8.  */
  9.  
  10. #include "windows.h" 
  11. #include "postmsg.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.   HWND hWnd;
  22.   MSG msg;
  23.   HANDLE hMenu;
  24.  
  25.   if (!hPrevInstance)
  26.     if (!PostMsgInit(hInstance))
  27.       return (NULL);
  28.  
  29.   hInst = hInstance;
  30.  
  31.   hMenu = LoadMenu(hInst, (LPSTR)"PostMsgMenu");
  32.  
  33.  
  34.   hWnd = CreateWindow((LPSTR)"PostMsg", (LPSTR)"PostMessage Sample Application",  /* window name       */
  35.   WS_OVERLAPPEDWINDOW,  
  36.       CW_USEDEFAULT, 0,
  37.       CW_USEDEFAULT, 0,
  38.       NULL, hMenu, hInstance, NULL);
  39.  
  40.   if (!hWnd)
  41.     return (NULL);
  42.  
  43.   ShowWindow(hWnd, nCmdShow);
  44.   UpdateWindow(hWnd);
  45.  
  46.   while (GetMessage(&msg,     
  47.       NULL,               
  48.       NULL,               
  49.       NULL))
  50.   {
  51.     TranslateMessage(&msg);
  52.     DispatchMessage(&msg);
  53.   }
  54.   return (msg.wParam);
  55. }
  56.  
  57.  
  58. BOOL PostMsgInit(hInstance)
  59. HANDLE hInstance;
  60. {
  61.   HANDLE hMemory;
  62.   PWNDCLASS pWndClass;
  63.   BOOL bSuccess;
  64.  
  65.   hMemory = LocalAlloc(LPTR, sizeof(WNDCLASS));
  66.   pWndClass = (PWNDCLASS) LocalLock(hMemory);
  67.  
  68.   pWndClass->style = NULL;
  69.   pWndClass->lpfnWndProc = PostMsgWndProc;
  70.   pWndClass->hInstance = hInstance;
  71.   pWndClass->hIcon = LoadIcon(NULL, IDI_APPLICATION);
  72.   pWndClass->hCursor = LoadCursor(NULL, IDC_ARROW);
  73.   pWndClass->hbrBackground = GetStockObject(WHITE_BRUSH);
  74.   pWndClass->lpszMenuName = (LPSTR) "PostMsgMenu";
  75.   pWndClass->lpszClassName = (LPSTR) "PostMsg";
  76.  
  77.   bSuccess = RegisterClass(pWndClass);
  78.  
  79.   LocalUnlock(hMemory);
  80.   LocalFree(hMemory);
  81.  
  82.   return (bSuccess);
  83. }
  84.  
  85.  
  86. long    FAR PASCAL PostMsgWndProc(hWnd, message, wParam, lParam)
  87. HWND hWnd;
  88. unsigned    message;
  89. WORD wParam;
  90. LONG lParam;
  91. {
  92.   FARPROC lpProcAbout;
  93.   HMENU hMenu;
  94.  
  95.   switch (message)
  96.   {
  97.   case WM_SYSCOMMAND:
  98.     if (wParam == ID_ABOUT)
  99.     {
  100.       lpProcAbout = MakeProcInstance(About, hInst);
  101.  
  102.       DialogBox(hInst, "AboutBox", hWnd, lpProcAbout);
  103.  
  104.       FreeProcInstance(lpProcAbout);
  105.       break;
  106.     }
  107.  
  108.     else
  109.       return (DefWindowProc(hWnd, message, wParam, lParam));
  110.  
  111.   case WM_CREATE:
  112.  
  113.     hMenu = GetSystemMenu(hWnd, FALSE);
  114.  
  115. /* Add a separator to the menu */
  116.  
  117.     ChangeMenu(hMenu, NULL, NULL, NULL, MF_APPEND | MF_SEPARATOR);
  118.  
  119. /* Add new menu item to the System menu */
  120.  
  121.     ChangeMenu(hMenu, NULL, "A&bout PostMsg...", ID_ABOUT,           
  122.         MF_APPEND | MF_STRING);
  123.     break;
  124.  
  125.   case WM_COMMAND:
  126.     if (wParam == IDM_ICONIZE)
  127.     {
  128.       PostMessage(hWnd, WM_SYSCOMMAND, SC_MINIMIZE, (LONG) NULL);
  129.     }
  130.     break;
  131.  
  132. /*  PostMessage places the message into the applications message queue and
  133.  *  does not wait for a response.  This is different from the SendMessage
  134.  *  function in that SendMessage does wait.  The Window Procedure is called
  135.  *  immediately when SendMessage is used.  For more information see
  136.  *  PostMessage and SendMessage in the Programmer's Reference.
  137.  */
  138.   case WM_DESTROY:
  139.     PostQuitMessage(0);
  140.     break;
  141.  
  142.   default:
  143.     return (DefWindowProc(hWnd, message, wParam, lParam));
  144.   }
  145.   return (NULL);
  146. }
  147.  
  148.  
  149. BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
  150. HWND hDlg;
  151. unsigned    message;
  152. WORD wParam;
  153. LONG lParam;
  154. {
  155.   switch (message)
  156.   {
  157.   case WM_INITDIALOG:
  158.     return (TRUE);
  159.  
  160.   case WM_COMMAND:
  161.     if (wParam == IDOK)
  162.     {
  163.       EndDialog(hDlg, NULL);
  164.       return (TRUE);
  165.     }
  166.     break;
  167.   }
  168.   return (FALSE);
  169. }
  170.  
  171.  
  172.