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

  1. /*
  2.  *
  3.  * GetMenuState
  4.  *
  5.  * This program registers a window and creates it on the screen.  The
  6.  *  program then creates the window, shows the window, and then updates
  7.  *  the window.  When the user executes the "Test" menu selection, the
  8.  *  state of the popup menu selection "Item 2", in the "GetMenuState"
  9.  *  menu, is verified as "CHECKED" in a message box.  Otherwise, an
  10.  *  error message is issued, also in the form of a message box.
  11.  *
  12.  */
  13.  
  14. #include "windows.h"
  15. #define  IDM_TEST       100  /* ID of the Test menu.                */
  16. #define  IDM_ITEM2      102  /* ID of Item 2.                       */
  17. #define  IDM_GETMENUPOS   1  /* Position of the GetMenuState Popup. */
  18. #define  SIZE_OUTBUFF    80  /* Output Buffer size.                 */
  19.  
  20. /* Global Variables */
  21. static HANDLE hInst;
  22. static HWND hWnd;
  23.  
  24. /* FORWARD REFERENCES */
  25. long FAR PASCAL WindowProc (HWND, unsigned, WORD, LONG);
  26.  
  27. /* WINMAIN */
  28. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  29. HANDLE hInstance, hPrevInstance;
  30. LPSTR lpszCmdLine;
  31. int cmdShow;
  32. {
  33.   MSG msg;
  34.   HDC hDC;     /* Handle to the Display Context. */
  35.  
  36.   if (!hPrevInstance)  {
  37.  
  38.      WNDCLASS rClass;
  39.  
  40.      rClass.lpszClassName = (LPSTR)"gmnustat";
  41.      rClass.hInstance      = hInstance;
  42.      rClass.lpfnWndProc   = WindowProc;
  43.      rClass.hCursor         = LoadCursor(NULL, IDC_ARROW);
  44.      rClass.hIcon            = LoadIcon(hInstance, IDI_APPLICATION);
  45.      rClass.lpszMenuName  = (LPSTR)"TestMenu";            /* Load the menu. */
  46.      rClass.hbrBackground = GetStockObject(WHITE_BRUSH);
  47.      rClass.style            = CS_HREDRAW | CS_VREDRAW;
  48.      rClass.cbClsExtra      = 0;
  49.      rClass.cbWndExtra      = 0;
  50.  
  51.      RegisterClass((LPWNDCLASS)&rClass);
  52.  
  53.      }
  54.    else
  55.       ;
  56.  
  57.    hInst = hInstance;
  58.  
  59.    hWnd = CreateWindow((LPSTR) "gmnustat",
  60.                (LPSTR) "GetMenuState",
  61.                WS_OVERLAPPEDWINDOW,           /* Use overlapped window.   */
  62.                CW_USEDEFAULT,                 /* Use default coordinates. */
  63.                CW_USEDEFAULT,                 /* Use default coordinates. */
  64.                CW_USEDEFAULT,                 /* Use default coordinates. */
  65.                CW_USEDEFAULT,                 /* Use default coordinates. */
  66.                (HWND)NULL,
  67.                (HMENU)NULL,
  68.                (HANDLE)hInstance,
  69.                (LPSTR)NULL
  70.              );
  71.  
  72.    ShowWindow(hWnd, cmdShow);
  73.  
  74.    UpdateWindow(hWnd);
  75.  
  76.    while (GetMessage((LPMSG)&msg, NULL, 0,0)) {
  77.        TranslateMessage(&msg);
  78.        DispatchMessage(&msg);
  79.    } /* while */
  80.  
  81.  
  82. } /* WinMain */
  83.  
  84. /* WINDOWPROC */
  85.  
  86. long FAR PASCAL WindowProc(hWnd, identifier, wParam, lParam)
  87. HWND    hWnd;
  88. unsigned identifier;
  89. WORD     wParam;
  90. LONG     lParam;
  91.  
  92. {
  93.    char szoutbuf[SIZE_OUTBUFF]; /* Formatted output buffer for message Box */
  94.    WORD wMState;                /* The WORD state of the menu selection.   */
  95.    
  96.    switch (identifier) {                              /*******************/
  97.     case WM_COMMAND:                                  /* Process the     */
  98.        switch (wParam) {                              /* menu selections */
  99.          case IDM_TEST:                                /* here.           */
  100.            wMState = GetMenuState(GetSubMenu(GetMenu(hWnd),
  101.                                              IDM_GETMENUPOS),
  102.                                   IDM_ITEM2,          /* Get the number  */
  103.                                   MF_BYCOMMAND);      /* of the menu     */
  104.                                                       /* a temp buffer.  */
  105.            if (wMState == NULL)                       /* If the menu    */
  106.              {                                          /* does not      */
  107.              MessageBox(hWnd,                           /* exist, issue  */
  108.                         (LPSTR)"Menu Does Not Exist!",  /* an error in   */
  109.                         (LPSTR)"ERROR!!!",              /* the form of a */
  110.                         MB_OK | MB_ICONEXCLAMATION);    /* message box.  */
  111.              break;
  112.              }
  113.            else                                       /* Otherwise, put  */
  114.              {                                        /* a more meaning- */
  115.              if (wMState && MF_CHECKED != 0)
  116.                 {
  117.                 sprintf(szoutbuf,                        /* ful message in  */
  118.                         "%s%s",                       /* a buffer for    */
  119.                         "The State of GetMenuState ", /* the user to     */
  120.                         "(Item 2) is CHECKED.");      /* recognize.      */
  121.                 MessageBox(hWnd,                      /* Then...         */
  122.                            (LPSTR)szoutbuf,           /* show the result */
  123.                            (LPSTR)"GetMenuState",     /* in a message    */
  124.                            MB_OK);                    /* box.            */
  125.                 }
  126.              else
  127.                 {
  128.                 sprintf(szoutbuf,                        
  129.                         "%s%s",                          
  130.                         "The State of GetMenuState ",    
  131.                         "(Item 2) is UNCHECKED.");         
  132.                 MessageBox(hWnd,                         
  133.                            (LPSTR)szoutbuf,              
  134.                            (LPSTR)"GetMenuState",        
  135.                            MB_OK);
  136.                 }
  137.               break;
  138.              }
  139.        }
  140.  
  141.     default:
  142.          return(DefWindowProc(hWnd, identifier, wParam, lParam));
  143.          break;
  144.  
  145.    }
  146.  
  147.    return(0L);
  148.  
  149. }
  150.