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

  1. /*
  2.  *
  3.  * GetMenuString
  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 GetMenuString function
  8.  *  if the user selects the "GetMenuString" 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.  The function is duplicated for
  11.  *  use by position of the menu selection and by the resource ID.
  12.  *
  13.  */
  14.  
  15. #include "windows.h"
  16. #define  IDM_TEST      100     /* ID of the Test menu.    */
  17. #define  N_MAX_COUNT   14      /* Temporary buffer count. */
  18. #define  SIZE_OUTBUFF  36      /* 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)"gmenustr";
  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) "gmenustr",
  60.                (LPSTR) "GetMenuString",
  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 szbuf[N_MAX_COUNT];     /* Temp Buffer, stores results of function */
  94.    char szoutbuf[SIZE_OUTBUFF]; /* Formatted output buffer for message Box */
  95.    short int nCopied;           /* Number of characters copied, error chk  */
  96.    
  97.    switch (identifier) {                              /*******************/
  98.     case WM_COMMAND:                                  /* Process the     */
  99.        switch (wParam) {                              /* menu selections */
  100.          case IDM_TEST:                                /* here.           */
  101.            nCopied = GetMenuString(GetMenu(hWnd),     /* Get the string  */
  102.                                    wParam,            /* of the menu and */
  103.                                    (LPSTR) szbuf,     /* put it into a   */
  104.                                    N_MAX_COUNT,       /* temporary buf-  */
  105.                                    MF_BYCOMMAND);     /* fer by command. */
  106.            if (nCopied == 0)                            /* If no charac- */
  107.              {                                          /* ters were     */
  108.              MessageBox(hWnd,                           /* copied, issue */
  109.                         (LPSTR)"GetMenuString Failed!", /* an error in   */
  110.                         (LPSTR)"ERROR!!!",              /* the form of a */
  111.                         MB_OK | MB_ICONEXCLAMATION);    /* message box.  */
  112.              break;
  113.              }
  114.            else                                       /* Otherwise, put  */
  115.              {                                        /* a more meaning- */
  116.              sprintf(szoutbuf,                        /* ful message in  */
  117.                      "%s%s%s",                        /* a buffer for    */
  118.                      "The Menu String is '",          /* the user to     */
  119.                      szbuf,                           /* recognize.      */
  120.                      "'");                            /* Then...         */
  121.              MessageBox(hWnd,                         /* show the result */
  122.                         (LPSTR)szoutbuf,              /* in a message    */
  123.                         (LPSTR)"MF_BYCOMMAND",        /* box.            */
  124.                         MB_OK);
  125.              nCopied = GetMenuString(GetMenu(hWnd),   /* Now, get the    */
  126.                                      0,               /* same string     */
  127.                                      (LPSTR)szbuf,    /* from the same   */
  128.                                      N_MAX_COUNT,     /* menu by using   */
  129.                                      MF_BYPOSITION);  /* the position of */
  130.              if (nCopied == 0)                        /* the menu.       */
  131.                 {                                     /* If no charac-   */
  132.                 MessageBox(hWnd,                      /* ters were       */
  133.                            (LPSTR)"GetMenuString Failed!", /* found,     */
  134.                            (LPSTR)"MF_BYPOSITION",         /* issue an   */
  135.                            MB_OK | MB_ICONEXCLAMATION);    /* error in a */
  136.                 break;                                /* message box.    */
  137.                 }
  138.              else                                     /* Finally,        */
  139.                 {                                     /* display the     */
  140.                 sprintf(szoutbuf,                     /* results of the  */
  141.                         "%s%s%s",                     /* GetMenuString   */
  142.                         "The Menu String is '",       /* using position  */
  143.                         szbuf,                        /* rather than ID. */
  144.                         "'");                         /* But put it in a */
  145.                 MessageBox(hWnd,                      /* form which is   */
  146.                            (LPSTR)szoutbuf,           /* more pleasing   */
  147.                            (LPSTR)"MF_BYPOSITION",    /* to the user.    */
  148.                            MB_OK);                    /*******************/
  149.                 };
  150.               break;
  151.              }
  152.        }
  153.  
  154.     default:
  155.          return(DefWindowProc(hWnd, identifier, wParam, lParam));
  156.          break;
  157.  
  158.    }
  159.  
  160.    return(0L);
  161.  
  162. }
  163.