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

  1. /*
  2.  *  Function Name:   EnableMenuItem
  3.  *  Program Name:    enmenuit.c
  4.  *  SDK Version:         2.03
  5.  *  Runtime Version:     2.03
  6.  *  Microsoft C Version: 5.0
  7.  *
  8.  *  Description:
  9.  *   The program below allows a menu item to be grayed, enabled, or disabled.
  10.  *   The following program will gray a menu item.
  11.  */
  12.  
  13. #include "windows.h"
  14.  
  15. long FAR PASCAL WndProc(HWND, unsigned, WORD, LONG);
  16.  
  17. /***********************************************************************/
  18.  
  19. void CALL_EnableMenuItem(hWnd)
  20. HWND hWnd;
  21. {
  22.   HMENU hCurrentWindowMenu, hSubItem;
  23.   unsigned wIDNewItem1, wIDNewItem2;
  24.   BOOL  bPrevState;
  25.  
  26.   hCurrentWindowMenu = CreateMenu();
  27.   hSubItem           = CreateMenu();
  28.                                               /* menu heading created. */
  29.   ChangeMenu (hCurrentWindowMenu, NULL, (LPSTR) "Heading",
  30.               hSubItem, MF_APPEND | MF_BYPOSITION | MF_POPUP);
  31.  
  32.                                              /* 2 items added under Heading */
  33.   ChangeMenu (hSubItem, NULL, (LPSTR ) "Item1", wIDNewItem1,
  34.                MF_APPEND | MF_BYCOMMAND | MF_STRING);
  35.   ChangeMenu (hSubItem, NULL, (LPSTR) "Item2", wIDNewItem2,
  36.                MF_APPEND | MF_BYCOMMAND | MF_STRING);
  37.  
  38.   SetMenu(hWnd, hCurrentWindowMenu);
  39.  
  40.   bPrevState = EnableMenuItem(hCurrentWindowMenu, wIDNewItem1, MF_GRAYED);
  41.  
  42. /*  if (bPrevState == 0 or 1 or 2)         0 = ENABLED or error (as in
  43.                                               BYPOSITION invalid number)
  44.                                           1 = GRAYED
  45.                                           2 = DISABLED
  46.   {
  47.     MessageBox(NULL,(LPSTR)"EnableMenuItem failed",(LPSTR)"ERROR",MB_ICONHAND);
  48.     return FALSE;
  49.   }
  50. */
  51.  
  52.   return;
  53. }
  54.  
  55. /**************************************************************************/
  56.  
  57. /* Procedure called when the application is loaded for the first time */
  58. BOOL WinInit( hInstance )
  59. HANDLE hInstance;
  60. {
  61.     WNDCLASS   wcClass;
  62.  
  63.     wcClass.style          = CS_HREDRAW | CS_VREDRAW;
  64.     wcClass.lpfnWndProc    = WndProc;
  65.     wcClass.cbClsExtra     =0;
  66.     wcClass.cbWndExtra     =0;
  67.     wcClass.hInstance      = hInstance;
  68.     wcClass.hIcon          = LoadIcon( hInstance,NULL );
  69.     wcClass.hCursor        = LoadCursor( NULL, IDC_ARROW );
  70.     wcClass.hbrBackground  = (HBRUSH)GetStockObject( WHITE_BRUSH );
  71.     wcClass.lpszMenuName   = (LPSTR)NULL;
  72.     wcClass.lpszClassName  = (LPSTR)"EnableMenuItem";
  73.  
  74.     if (!RegisterClass( (LPWNDCLASS)&wcClass ) )
  75.         /* Initialization failed.
  76.          * Windows will automatically deallocate all allocated memory.
  77.          */
  78.         return FALSE;
  79.  
  80.     return TRUE;        /* Initialization succeeded */
  81. }
  82.  
  83.  
  84. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  85. HANDLE hInstance, hPrevInstance;
  86. LPSTR lpszCmdLine;
  87. int cmdShow;
  88. {
  89.     MSG   msg;
  90.     HWND  hWnd;
  91.  
  92.     if (!hPrevInstance)
  93.         {
  94.         /* Call initialization procedure if this is the first instance */
  95.         if (!WinInit( hInstance ))
  96.             return FALSE;
  97.         }
  98.  
  99.     hWnd = CreateWindow((LPSTR)"EnableMenuItem",
  100.                         (LPSTR)"EnableMenuItem()",
  101.                         WS_OVERLAPPEDWINDOW,
  102.                         CW_USEDEFAULT,
  103.                         CW_USEDEFAULT,
  104.                         CW_USEDEFAULT,
  105.                         CW_USEDEFAULT,
  106.                         (HWND)NULL,        /* no parent */
  107.                         (HMENU)NULL,       /* use class menu */
  108.                         (HANDLE)hInstance, /* handle to window instance */
  109.                         (LPSTR)NULL        /* no params to pass on */
  110.                         );
  111.  
  112.     /* Make window visible according to the way the app is activated */
  113.     ShowWindow( hWnd, cmdShow );
  114.     UpdateWindow( hWnd );
  115.  
  116.     /* Polling messages from event queue */
  117.     while (GetMessage((LPMSG)&msg, NULL, 0, 0))
  118.         {
  119.         TranslateMessage((LPMSG)&msg);
  120.         DispatchMessage((LPMSG)&msg);
  121.         }
  122.  
  123.     return (int)msg.wParam;
  124. }
  125.  
  126. /* Procedures which make up the window class. */
  127. long FAR PASCAL WndProc( hWnd, message, wParam, lParam )
  128. HWND hWnd;
  129. unsigned message;
  130. WORD wParam;
  131. LONG lParam;
  132. {
  133.     PAINTSTRUCT ps;
  134.  
  135.     switch (message)
  136.     {
  137.  
  138.     case WM_PAINT:
  139.         BeginPaint( hWnd, (LPPAINTSTRUCT)&ps );
  140.         CALL_EnableMenuItem(hWnd);
  141.         EndPaint( hWnd, (LPPAINTSTRUCT)&ps );
  142.         break;
  143.  
  144.     case WM_DESTROY:
  145.         PostQuitMessage( 0 );
  146.         break;
  147.  
  148.     default:
  149.         return DefWindowProc( hWnd, message, wParam, lParam );
  150.         break;
  151.     }
  152.     return(0L);
  153. }
  154.