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

  1. /*
  2.  *
  3.  * GetSubMenu
  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.  The WinMain proceeds to execute the GetSubMenu function
  8.  *  if the user selects the "GetSubMenu" selection from the main menu bar.
  9.  *  If the function call is successful, a message box is created which
  10.  *  copies the menu string into a buffer.
  11.  *
  12.  */
  13.  
  14. #include "windows.h"
  15. #define  IDM_SUB      100   /* ID of the submenu.                          */
  16. #define  N_MAX_COUNT   11   /* Maximum count of the characters of submenu. */
  17. #define  SIZE_OUTBUFF  33   /* Size of the output buffer.                  */
  18.  
  19. /* Global Variables */
  20. static HANDLE hInst;
  21. static HWND hWnd;
  22.  
  23. /* FORWARD REFERENCES */
  24. long FAR PASCAL WindowProc (HWND, unsigned, WORD, LONG);
  25.  
  26. /* WINMAIN */
  27. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  28. HANDLE hInstance, hPrevInstance;
  29. LPSTR lpszCmdLine;
  30. int cmdShow;
  31. {
  32.   MSG msg;
  33.   HDC hDC;     /* Handle to the Display Context. */
  34.  
  35.   if (!hPrevInstance)  {
  36.  
  37.      WNDCLASS rClass;
  38.  
  39.      rClass.lpszClassName = (LPSTR)"gsubmenu";
  40.      rClass.hInstance      = hInstance;
  41.      rClass.lpfnWndProc   = WindowProc;
  42.      rClass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  43.      rClass.hIcon            = LoadIcon(hInstance, IDI_APPLICATION);
  44.      rClass.lpszMenuName  = (LPSTR)"TestSubMenu"; /* Load menu name. */
  45.      rClass.hbrBackground = GetStockObject(WHITE_BRUSH);
  46.      rClass.style            = CS_HREDRAW | CS_VREDRAW;
  47.      rClass.cbClsExtra      = 0;
  48.      rClass.cbWndExtra      = 0;
  49.  
  50.      RegisterClass((LPWNDCLASS)&rClass);
  51.  
  52.      }
  53.    else
  54.       ;
  55.  
  56.    hInst = hInstance;
  57.  
  58.    hWnd = CreateWindow((LPSTR) "gsubmenu",
  59.                (LPSTR) "GetSubMenu",
  60.                WS_OVERLAPPEDWINDOW,           /* Use overlapped window.   */
  61.                CW_USEDEFAULT,                 /* Use default coordinates. */
  62.                CW_USEDEFAULT,                 /* Use default coordinates. */
  63.                CW_USEDEFAULT,                 /* Use default coordinates. */
  64.                CW_USEDEFAULT,                 /* Use default coordinates. */
  65.                (HWND)NULL,
  66.                LoadMenu(hInst, (LPSTR)"TestSubMenu"),  /* Load submenu.   */
  67.                (HANDLE)hInstance,
  68.                (LPSTR)NULL
  69.              );
  70.  
  71.    ShowWindow(hWnd, cmdShow);
  72.  
  73.    UpdateWindow(hWnd);
  74.  
  75.    while (GetMessage((LPMSG)&msg, NULL, 0,0)) {
  76.        TranslateMessage(&msg);
  77.        DispatchMessage(&msg);
  78.    } /* while */
  79.  
  80.  
  81. } /* WinMain */
  82.  
  83. /* WINDOWPROC */
  84.  
  85. long FAR PASCAL WindowProc(hWnd, identifier, wParam, lParam)
  86. HWND    hWnd;
  87. unsigned identifier;
  88. WORD     wParam;
  89. LONG     lParam;
  90.  
  91. {
  92.    char szbuf[N_MAX_COUNT];        /* Buffer for submenu string.   */
  93.    char szoutbuf[SIZE_OUTBUFF];    /* Buffer for output.           */
  94.    short int nCopied;              /* Number of characters copied. */
  95.    HMENU hSubMenu;                 /* Handle to the submenu.       */
  96.  
  97.    switch (identifier) {
  98.     case WM_COMMAND:
  99.        switch (wParam) {
  100.          case IDM_SUB:              /* If the submenu is selected,         */
  101.              hSubMenu = GetSubMenu(GetMenu(hWnd), 0); /* Get the submenu. */
  102.              if (hSubMenu == NULL)
  103.                 {                  /* If the submenu is invalid,          */
  104.                 MessageBox(hWnd,   /*  display a error message box.       */
  105.                            (LPSTR)"No Popup Menu Exists Here!",
  106.                            (LPSTR)"ERROR!!!",
  107.                            MB_OK | MB_ICONEXCLAMATION);
  108.                 break;             /* And stop here.                      */
  109.                 };                 /* Otherwise, get the menu string by position. */
  110.              nCopied = GetMenuString(hSubMenu, 
  111.                                      0,
  112.                                      (LPSTR)szbuf,
  113.                                      N_MAX_COUNT,
  114.                                      MF_BYPOSITION);
  115.              if (nCopied == 0)   /* and issue an error message if fails.   */
  116.                 {
  117.                 MessageBox(hWnd,
  118.                            (LPSTR)"GetMenuString Failed!",
  119.                            (LPSTR)"MF_BYPOSITION",
  120.                            MB_OK | MB_ICONEXCLAMATION);
  121.                 break;
  122.                 }
  123.              else       /* Otherwise, display success through message box. */
  124.                 {
  125.                 sprintf(szoutbuf,
  126.                         "%s%s%s",
  127.                         "The SubMenu is '",
  128.                         szbuf,
  129.                         "'");
  130.                 MessageBox(hWnd,
  131.                            (LPSTR)szoutbuf,
  132.                            (LPSTR)"GetSubMenu",
  133.                            MB_OK);
  134.                 };
  135.               break;
  136.        }
  137.  
  138.     default:
  139.          return(DefWindowProc(hWnd, identifier, wParam, lParam));
  140.          break;
  141.  
  142.    }
  143.  
  144.    return(0L);
  145.  
  146. }
  147.