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

  1. /*
  2.  *  Yield
  3.  *  yield.c
  4.  *
  5.  *  This program demonstrates the use of the function Yield.
  6.  *  It does this by simply creating a message box that tells the user that
  7.  *  the application is going to yield control to other applications.  After
  8.  *  the user hits the OK button, the Yield call is made.
  9.  *
  10.  */
  11.  
  12. #include "windows.h"      /* required for all Windows applications */
  13. #include "yield.h"        /* specific to this program              */
  14.  
  15. HANDLE hInst;             /* current instance                      */
  16.  
  17. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  18. HANDLE hInstance;         /* current instance             */
  19. HANDLE hPrevInstance;     /* previous instance            */
  20. LPSTR lpCmdLine;          /* command line                 */
  21. int nCmdShow;             /* show-window type (open/icon) */
  22. {
  23.     HWND hWnd;            /* window handle       */
  24.     MSG msg;              /* message             */
  25.     HANDLE hMenu;         /* Handle to the menu  */
  26.  
  27.     if (!hPrevInstance)              /* Has application been initialized? */
  28.         if (!YieldInit(hInstance))
  29.             return (NULL);           /* Exits if unable to initialize     */
  30.  
  31.     hInst = hInstance;               /* Saves the current instance        */
  32.  
  33.     hMenu = LoadMenu( hInst, (LPSTR)"YieldMenu" );
  34.                                      /*  Load the menu    */
  35.  
  36.     hWnd = CreateWindow("Yield",     /* window class      */
  37.         "Yield Sample Application",  /* window name       */
  38.         WS_OVERLAPPEDWINDOW,         /* window style      */
  39.         CW_USEDEFAULT,               /* x position        */
  40.         CW_USEDEFAULT,               /* y position        */
  41.         CW_USEDEFAULT,               /* width             */
  42.         CW_USEDEFAULT,               /* height            */
  43.         NULL,                        /* parent handle     */
  44.         hMenu,                       /* menu or child ID  */
  45.         hInstance,                   /* instance          */
  46.         NULL);                       /* additional info   */
  47.  
  48.     if (!hWnd)                       /* Was the window created? */
  49.         return (NULL);
  50.  
  51.     ShowWindow(hWnd, nCmdShow);      /* Shows the window        */
  52.     UpdateWindow(hWnd);              /* Sends WM_PAINT message  */
  53.  
  54.     while (GetMessage(&msg,     /* message structure                      */
  55.             NULL,               /* handle of window receiving the message */
  56.             NULL,               /* lowest message to examine              */
  57.             NULL))              /* highest message to examine             */
  58.         {
  59.         TranslateMessage(&msg);  /* Translates virtual key codes           */
  60.         DispatchMessage(&msg);   /* Dispatches message to window           */
  61.     }
  62.     return (msg.wParam);         /* Returns the value from PostQuitMessage */
  63. }
  64.  
  65. BOOL YieldInit(hInstance)
  66. HANDLE hInstance;                /* current instance           */
  67. {
  68.     HANDLE hMemory;              /* handle to allocated memory */
  69.     PWNDCLASS pWndClass;         /* structure pointer          */
  70.     BOOL bSuccess;               /* RegisterClass() result     */
  71.  
  72.     hMemory = LocalAlloc(LPTR, sizeof(WNDCLASS));
  73.     pWndClass = (PWNDCLASS) LocalLock(hMemory);
  74.  
  75.     pWndClass->style = NULL;
  76.     pWndClass->lpfnWndProc = YieldWndProc;
  77.     pWndClass->hInstance = hInstance;
  78.     pWndClass->hIcon = LoadIcon(NULL, IDI_APPLICATION);
  79.     pWndClass->hCursor = LoadCursor(NULL, IDC_ARROW);
  80.     pWndClass->hbrBackground = GetStockObject(WHITE_BRUSH);
  81.     pWndClass->lpszMenuName = (LPSTR) "YieldMenu";
  82.     pWndClass->lpszClassName = (LPSTR) "Yield";
  83.  
  84.     bSuccess = RegisterClass(pWndClass);
  85.  
  86.     LocalUnlock(hMemory);     /* Unlocks the memory    */
  87.     LocalFree(hMemory);       /* Returns it to Windows */
  88.  
  89.     return (bSuccess);        /* Returns result of registering the window */
  90. }
  91.  
  92. long FAR PASCAL YieldWndProc(hWnd, message, wParam, lParam)
  93. HWND hWnd;                    /* window handle                   */
  94. unsigned message;             /* type of message                 */
  95. WORD wParam;                  /* additional information          */
  96. LONG lParam;                  /* additional information          */
  97. {
  98.     FARPROC lpProcAbout;      /* pointer to the "About" function */
  99.     HMENU hMenu;              /* handle to the System menu       */
  100.  
  101.     switch (message) {
  102.         case WM_SYSCOMMAND:   /* message: command from system menu */
  103.             if (wParam == ID_ABOUT) {
  104.                 lpProcAbout = MakeProcInstance(About, hInst);
  105.  
  106.                 DialogBox(hInst,         /* current instance         */
  107.                     "AboutBox",          /* resource to use          */
  108.                     hWnd,                /* parent handle            */
  109.                     lpProcAbout);        /* About() instance address */
  110.  
  111.                 FreeProcInstance(lpProcAbout);
  112.                 break;
  113.             }
  114.  
  115.             else                         /* Lets Windows process it       */
  116.                 return (DefWindowProc(hWnd, message, wParam, lParam));
  117.  
  118.         case WM_CREATE:                  /* message: window being created */
  119.  
  120.             /* Get the handle of the System menu */
  121.  
  122.             hMenu = GetSystemMenu(hWnd, FALSE);
  123.  
  124.             /* Add a separator to the menu */
  125.  
  126.             ChangeMenu(hMenu,                    /* menu handle         */
  127.                 NULL,                            /* menu item to change */
  128.                 NULL,                            /* new menu item       */
  129.                 NULL,                            /* menu identifier     */
  130.                 MF_APPEND | MF_SEPARATOR);       /* type of change      */
  131.  
  132.             /* Add new menu item to the System menu */
  133.  
  134.             ChangeMenu(hMenu,                    /* menu handle         */
  135.                 NULL,                            /* menu item to change */
  136.                 "A&bout Yield...",               /* new menu item       */
  137.                 ID_ABOUT,                        /* menu identifier     */
  138.                 MF_APPEND | MF_STRING);          /* type of change      */
  139.             break;
  140.  
  141.         case WM_COMMAND:
  142.             if ( wParam == IDM_YIELD )
  143.               {
  144.               MessageBox(hWnd,(LPSTR)"Hit OK to Give Up Control of Processor",
  145.                               (LPSTR)"Give It Up!",MB_OK);
  146.  
  147.                         /*  Tell User what we are going to do  */
  148.  
  149.               Yield();  /*  Do it!  -  This will cause Windows to see if
  150.                          *  are currently any other applications that wish
  151.                          *  to run.  If there are, then control will be
  152.                          *  given to one of those applications.
  153.                          */
  154.  
  155.               MessageBox(hWnd,(LPSTR)"We Have Control Back!",
  156.                               (LPSTR)"Got it Back",MB_OK);
  157.               }
  158.             break;
  159.  
  160. /*  Because of how quickly the whole process goes, the user will not be able
  161.  *  notice the difference unless a very demanding application is present at
  162.  *  the time the Yield box is selected from the menu.
  163.  */
  164.         case WM_DESTROY:             /* message: window being destroyed */
  165.             PostQuitMessage(0);
  166.             break;
  167.  
  168.         default:                     /* Passes it on if unproccessed    */
  169.             return (DefWindowProc(hWnd, message, wParam, lParam));
  170.     }
  171.     return (NULL);
  172. }
  173.  
  174. BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
  175. HWND hDlg;
  176. unsigned message;
  177. WORD wParam;
  178. LONG lParam;
  179. {
  180.     switch (message) {
  181.         case WM_INITDIALOG:              /* message: initialize dialog box */
  182.             return (TRUE);
  183.  
  184.         case WM_COMMAND:                 /* message: received a command */
  185.             if (wParam == IDOK) {        /* "OK" box selected?          */
  186.                 EndDialog(hDlg, NULL);   /* Exits the dialog box        */
  187.                 return (TRUE);
  188.             }
  189.             break;
  190.     }
  191.     return (FALSE);                      /* Didn't process a message    */
  192. }
  193.