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

  1. /*
  2.  *  SetPriority
  3.  *  setpri.c
  4.  *
  5.  *  This program modifies its priority using the SetPriority function.  It
  6.  *  has two menu items.  One to set the priority high and the other to set
  7.  *  it low.
  8.  *
  9.  */
  10.  
  11. #include "windows.h"      /* required for all Windows applications */
  12. #include "setpri.h"       /* specific to this program              */
  13.  
  14. HANDLE hInst;             /* current instance                      */
  15.  
  16. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  17. HANDLE hInstance;         /* current instance             */
  18. HANDLE hPrevInstance;     /* previous instance            */
  19. LPSTR lpCmdLine;          /* command line                 */
  20. int nCmdShow;             /* show-window type (open/icon) */
  21. {
  22.     HWND hWnd;            /* window handle       */
  23.     MSG msg;              /* message             */
  24.     HANDLE hMenu;         /* Handle to the menu  */
  25.  
  26.     if (!hPrevInstance)              /* Has application been initialized? */
  27.         if (!SetPriInit(hInstance))
  28.             return (NULL);           /* Exits if unable to initialize     */
  29.  
  30.     hInst = hInstance;               /* Saves the current instance        */
  31.  
  32.     hMenu = LoadMenu( hInst, (LPSTR)"SetPriMenu" );
  33.                                      /*  Load the menu    */
  34.  
  35.     hWnd = CreateWindow("SetPri",   /* window class      */
  36.         "SetPri Sample Application",  /* window name       */
  37.         WS_OVERLAPPEDWINDOW,         /* window style      */
  38.         CW_USEDEFAULT,               /* x position        */
  39.         CW_USEDEFAULT,               /* y position        */
  40.         CW_USEDEFAULT,               /* width             */
  41.         CW_USEDEFAULT,               /* height            */
  42.         NULL,                        /* parent handle     */
  43.         hMenu,                       /* menu or child ID  */
  44.         hInstance,                   /* instance          */
  45.         NULL);                       /* additional info   */
  46.  
  47.     if (!hWnd)                       /* Was the window created? */
  48.         return (NULL);
  49.  
  50.     ShowWindow(hWnd, nCmdShow);      /* Shows the window        */
  51.     UpdateWindow(hWnd);              /* Sends WM_PAINT message  */
  52.  
  53.     while (GetMessage(&msg,     /* message structure                      */
  54.             NULL,               /* handle of window receiving the message */
  55.             NULL,               /* lowest message to examine              */
  56.             NULL))              /* highest message to examine             */
  57.         {
  58.         TranslateMessage(&msg);  /* Translates virtual key codes           */
  59.         DispatchMessage(&msg);   /* Dispatches message to window           */
  60.     }
  61.     return (msg.wParam);         /* Returns the value from PostSetPriMessage */
  62. }
  63.  
  64. BOOL SetPriInit(hInstance)
  65. HANDLE hInstance;                /* current instance           */
  66. {
  67.     HANDLE hMemory;              /* handle to allocated memory */
  68.     PWNDCLASS pWndClass;         /* structure pointer          */
  69.     BOOL bSuccess;               /* RegisterClass() result     */
  70.  
  71.     hMemory = LocalAlloc(LPTR, sizeof(WNDCLASS));
  72.     pWndClass = (PWNDCLASS) LocalLock(hMemory);
  73.  
  74.     pWndClass->style = NULL;
  75.     pWndClass->lpfnWndProc = SetPriWndProc;
  76.     pWndClass->hInstance = hInstance;
  77.     pWndClass->hIcon = LoadIcon(NULL, IDI_APPLICATION);
  78.     pWndClass->hCursor = LoadCursor(NULL, IDC_ARROW);
  79.     pWndClass->hbrBackground = GetStockObject(WHITE_BRUSH);
  80.     pWndClass->lpszMenuName = (LPSTR) "SetPriMenu";
  81.     pWndClass->lpszClassName = (LPSTR) "SetPri";
  82.  
  83.     bSuccess = RegisterClass(pWndClass);
  84.  
  85.     LocalUnlock(hMemory);     /* Unlocks the memory    */
  86.     LocalFree(hMemory);       /* Returns it to Windows */
  87.  
  88.     return (bSuccess);        /* Returns result of registering the window */
  89. }
  90.  
  91. long FAR PASCAL SetPriWndProc(hWnd, message, wParam, lParam)
  92. HWND hWnd;                    /* window handle                   */
  93. unsigned message;             /* type of message                 */
  94. WORD wParam;                  /* additional information          */
  95. LONG lParam;                  /* additional information          */
  96. {
  97.     FARPROC lpProcAbout;      /* pointer to the "About" function */
  98.     HMENU hMenu;              /* handle to the System menu       */
  99.  
  100.     switch (message) {
  101.         case WM_SYSCOMMAND:   /* message: command from system menu */
  102.             if (wParam == ID_ABOUT) {
  103.                 lpProcAbout = MakeProcInstance(About, hInst);
  104.  
  105.                 DialogBox(hInst,         /* current instance         */
  106.                     "AboutBox",          /* resource to use          */
  107.                     hWnd,                /* parent handle            */
  108.                     lpProcAbout);        /* About() instance address */
  109.  
  110.                 FreeProcInstance(lpProcAbout);
  111.                 break;
  112.             }
  113.  
  114.             else                         /* Lets Windows process it       */
  115.                 return (DefWindowProc(hWnd, message, wParam, lParam));
  116.  
  117.         case WM_CREATE:                  /* message: window being created */
  118.  
  119.             /* Get the handle of the System menu */
  120.  
  121.             hMenu = GetSystemMenu(hWnd, FALSE);
  122.  
  123.             /* Add a separator to the menu */
  124.  
  125.             ChangeMenu(hMenu,                    /* menu handle         */
  126.                 NULL,                            /* menu item to change */
  127.                 NULL,                            /* new menu item       */
  128.                 NULL,                            /* menu identifier     */
  129.                 MF_APPEND | MF_SEPARATOR);       /* type of change      */
  130.  
  131.             /* Add new menu item to the System menu */
  132.  
  133.             ChangeMenu(hMenu,                    /* menu handle         */
  134.                 NULL,                            /* menu item to change */
  135.                 "A&bout SetPri...",             /* new menu item       */
  136.                 ID_ABOUT,                        /* menu identifier     */
  137.                 MF_APPEND | MF_STRING);          /* type of change      */
  138.             break;
  139.  
  140.         case WM_COMMAND:
  141.             if ( wParam == IDM_HIGH )
  142.               SetPriority( GetCurrentTask(),  /*  Priority of current task  */
  143.                            -15 );  /*  -15 is the highest priority  */
  144.             if ( wParam == IDM_LOW )
  145.               SetPriority( GetCurrentTask(),  /*  Again, the current task  */
  146.                            15 );  /*  15 is the lowest priority  */
  147.  
  148. /*  The default value for the priority is zero  */
  149.  
  150.             break;
  151.  
  152.         case WM_DESTROY:             /* message: window being destroyed */
  153.             PostQuitMessage(0);
  154.             break;
  155.  
  156.         default:                     /* Passes it on if unproccessed    */
  157.             return (DefWindowProc(hWnd, message, wParam, lParam));
  158.     }
  159.     return (NULL);
  160. }
  161.  
  162. BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
  163. HWND hDlg;
  164. unsigned message;
  165. WORD wParam;
  166. LONG lParam;
  167. {
  168.     switch (message) {
  169.         case WM_INITDIALOG:              /* message: initialize dialog box */
  170.             return (TRUE);
  171.  
  172.         case WM_COMMAND:                 /* message: received a command */
  173.             if (wParam == IDOK) {        /* "OK" box selected?          */
  174.                 EndDialog(hDlg, NULL);   /* Exits the dialog box        */
  175.                 return (TRUE);
  176.             }
  177.             break;
  178.     }
  179.     return (FALSE);                      /* Didn't process a message    */
  180. }
  181.