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

  1. /*
  2.  * 
  3.  *  QUIT - Demonstrates PostAppMessage and GetWindowTask
  4.  *
  5.  *  This program will get the Task handle to the current Window, and send
  6.  *  a WM_QUIT message to the window which will cause it to go away.
  7.  */
  8.  
  9. #include <windows.h>
  10. #define IDM_QUIT    100
  11.  
  12. HANDLE hInst;
  13.  
  14. int PASCAL WinMain(HANDLE, HANDLE, LPSTR, int);
  15. long FAR PASCAL QuitWndProc(HWND, unsigned, WORD, LONG);
  16. BOOL FAR PASCAL About(HWND, unsigned, WORD, LONG);
  17.  
  18. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  19. HANDLE     hInstance;
  20. HANDLE     hPrevInstance;
  21. LPSTR     lpCmdLine;
  22. int     nCmdShow;
  23.   {
  24.   HWND        hWnd;
  25.   MSG        msg;
  26.   HANDLE    hMenu;
  27.   WNDCLASS  wndclass;
  28.   if (!hPrevInstance)
  29.     {
  30.     wndclass.style        = CS_HREDRAW | CS_VREDRAW;
  31.     wndclass.lpfnWndProc    = QuitWndProc;
  32.     wndclass.cbClsExtra     = 0;
  33.     wndclass.cbWndExtra     = 0;
  34.     wndclass.hInstance        = hInstance;
  35.     wndclass.hIcon        = LoadIcon(NULL, IDI_APPLICATION);
  36.     wndclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  37.     wndclass.hbrBackground  = GetStockObject(WHITE_BRUSH);
  38.     wndclass.lpszMenuName   = "QuitMenu";
  39.     wndclass.lpszClassName  = "Quit";
  40.  
  41.     if (!RegisterClass(&wndclass))
  42.       return FALSE;
  43.     }
  44.   hInst = hInstance;               /* Saves the current instance        */
  45.  
  46.   hMenu = LoadMenu(hInst, "QuitMenu");
  47.  
  48.   hWnd = CreateWindow("Quit",
  49.               "Quit Sample Application",
  50.               WS_OVERLAPPEDWINDOW,
  51.               CW_USEDEFAULT,
  52.               CW_USEDEFAULT,
  53.               CW_USEDEFAULT,
  54.               CW_USEDEFAULT,
  55.               NULL,
  56.               hMenu,
  57.               hInstance,
  58.               NULL);
  59.  
  60.   ShowWindow(hWnd, nCmdShow);
  61.   UpdateWindow(hWnd);
  62.  
  63.   while (GetMessage(&msg, NULL, 0, 0))
  64.     {
  65.     TranslateMessage(&msg);
  66.     DispatchMessage(&msg);
  67.     }
  68.   return msg.wParam;
  69.   }
  70.  
  71.  
  72. long FAR PASCAL QuitWndProc(hWnd, message, wParam, lParam)
  73. HWND      hWnd;
  74. unsigned  message;
  75. WORD      wParam;
  76. LONG      lParam;
  77.   {
  78.   HANDLE  hTask;
  79.  
  80.   switch (message) 
  81.     {
  82.     case WM_COMMAND:
  83.       if (wParam == IDM_QUIT)
  84.     {
  85.     hTask = GetWindowTask(hWnd);
  86.     PostAppMessage(hTask, WM_QUIT, 0, 0L);
  87.     }
  88.       break;
  89.  
  90.     case WM_DESTROY:
  91.       PostQuitMessage(0);
  92.       break;
  93.  
  94.     default:
  95.       return (DefWindowProc(hWnd, message, wParam, lParam));
  96.     }
  97.   return (NULL);
  98.   }
  99.