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

  1. /*
  2.  *  GetCurrentTask
  3.  *
  4.  *  This program will get the handle to the current task and send it a 
  5.  *  message to kill itself.
  6.  *
  7.  */
  8.  
  9. #include "windows.h"      /* required for all Windows applications */
  10. #include "getctask.h"
  11.  
  12. HANDLE hInst;             /* current instance                      */
  13.  
  14. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  15. HANDLE hInstance;         /* current instance             */
  16. HANDLE hPrevInstance;     /* previous instance            */
  17. LPSTR lpCmdLine;          /* command line                 */
  18. int nCmdShow;             /* show-window type (open/icon) */
  19. {
  20.     HWND hWnd;            /* window handle       */
  21.     MSG msg;              /* message             */
  22.     HANDLE hMenu;         /* Handle to the menu  */
  23.  
  24.     if (!hPrevInstance)              /* Has application been initialized? */
  25.         if (!GetCTaskInit(hInstance))
  26.             return (NULL);           /* Exits if unable to initialize     */
  27.  
  28.     hInst = hInstance;               /* Saves the current instance        */
  29.  
  30.     hMenu = LoadMenu( hInst, (LPSTR)"GetCTaskMenu" );
  31.                                      /*  Load the menu    */
  32.  
  33.     hWnd = CreateWindow("GetCTask",   /* window class      */
  34.         "GetCurrentTask Sample Application",  /* window name       */
  35.         WS_OVERLAPPEDWINDOW,         /* window style      */
  36.         CW_USEDEFAULT,               /* x position        */
  37.         CW_USEDEFAULT,               /* y position        */
  38.         CW_USEDEFAULT,               /* width             */
  39.         CW_USEDEFAULT,               /* height            */
  40.         NULL,                        /* parent handle     */
  41.         hMenu,                       /* menu or child ID  */
  42.         hInstance,                   /* instance          */
  43.         NULL);                       /* additional info   */
  44.  
  45.     if (!hWnd)                       /* Was the window created? */
  46.         return (NULL);
  47.  
  48.     ShowWindow(hWnd, nCmdShow);      /* Shows the window        */
  49.     UpdateWindow(hWnd);              /* Sends WM_PAINT message  */
  50.  
  51.     while (GetMessage(&msg,     /* message structure                      */
  52.             NULL,               /* handle of window receiving the message */
  53.             NULL,               /* lowest message to examine              */
  54.             NULL))              /* highest message to examine             */
  55.         {
  56.         TranslateMessage(&msg);  /* Translates virtual key codes           */
  57.         DispatchMessage(&msg);   /* Dispatches message to window           */
  58.     }
  59.     return (msg.wParam);         /* Returns the value from PostQuitMessage */
  60. }
  61.  
  62. BOOL GetCTaskInit(hInstance)
  63. HANDLE hInstance;                /* current instance           */
  64. {
  65.     HANDLE hMemory;              /* handle to allocated memory */
  66.     PWNDCLASS pWndClass;         /* structure pointer          */
  67.     BOOL bSuccess;               /* RegisterClass() result     */
  68.  
  69.     hMemory = LocalAlloc(LPTR, sizeof(WNDCLASS));
  70.     pWndClass = (PWNDCLASS) LocalLock(hMemory);
  71.  
  72.     pWndClass->style = NULL;
  73.     pWndClass->lpfnWndProc = GetCTaskWndProc;
  74.     pWndClass->hInstance = hInstance;
  75.     pWndClass->hIcon = LoadIcon(NULL, IDI_APPLICATION);
  76.     pWndClass->hCursor = LoadCursor(NULL, IDC_ARROW);
  77.     pWndClass->hbrBackground = GetStockObject(WHITE_BRUSH);
  78.     pWndClass->lpszMenuName = (LPSTR) "GetCTaskMenu";
  79.     pWndClass->lpszClassName = (LPSTR) "GetCTask";
  80.  
  81.     bSuccess = RegisterClass(pWndClass);
  82.  
  83.     LocalUnlock(hMemory);     /* Unlocks the memory    */
  84.     LocalFree(hMemory);       /* Returns it to Windows */
  85.  
  86.     return (bSuccess);        /* Returns result of registering the window */
  87. }
  88.  
  89. long FAR PASCAL GetCTaskWndProc(hWnd, message, wParam, lParam)
  90. HWND hWnd;                    /* window handle                   */
  91. unsigned message;             /* type of message                 */
  92. WORD wParam;                  /* additional information          */
  93. LONG lParam;                  /* additional information          */
  94. {
  95.     FARPROC lpProcAbout;      /* pointer to the "About" function */
  96.     HMENU hMenu;              /* handle to the System menu       */
  97.  
  98.     switch (message) {
  99.  
  100.         case WM_COMMAND:
  101.             if ( wParam == IDM_TRASH )
  102.               {
  103.               PostAppMessage( GetCurrentTask( ),  /*  Get current task  */
  104.                               WM_QUIT,      /*  Message to quit  */
  105.                               NULL,         /*  wParam  */
  106.                               (LONG)NULL);  /*  lParam  */
  107.               }
  108.             break;
  109.  
  110. /*  PostMessage places the message into the applications message queue and
  111.  *  does not wait for a response.  This is different from the SendMessage
  112.  *  function in that SendMessage does wait.  The Window Procedure is called
  113.  *  immediately when SendMessage is used.  For more information see
  114.  *  PostMessage and SendMessage in the Programmer's Reference.
  115.  */
  116.         case WM_DESTROY:             /* message: window being destroyed */
  117.             PostQuitMessage(0);
  118.             break;
  119.  
  120.         default:                     /* Passes it on if unproccessed    */
  121.             return (DefWindowProc(hWnd, message, wParam, lParam));
  122.     }
  123.     return (NULL);
  124. }
  125.