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

  1. /*
  2.  *  Function Name:   ChangeMenu
  3.  *  Microsoft C Version: 5.1
  4.  *
  5.  *  Description:
  6.  *   The program below will add new items to a menu.
  7.  */
  8.  
  9. #include "windows.h"
  10.  
  11. long    FAR PASCAL WndProc(HWND, unsigned, WORD, LONG);
  12.  
  13. /***********************************************************************/
  14.  
  15. void CALL_ChangeMenu(hWnd)
  16. HWND hWnd;
  17.   {
  18.   HMENU     hSubItem;
  19.   HMENU     hCurrentWindowMenu;
  20.   unsigned  wIDNewItem1, wIDNewItem2;    /* assign items numbers for ID */
  21.   BOOL      bChanged1;
  22.  
  23.   hCurrentWindowMenu = CreateMenu();
  24.   hSubItem           = CreateMenu();
  25.     /* menu heading created. */
  26.   bChanged1 = ChangeMenu (hCurrentWindowMenu, NULL, (LPSTR) "Heading",
  27.       hSubItem, MF_APPEND | MF_BYPOSITION | MF_POPUP);
  28.  
  29.   if (bChanged1 == FALSE)
  30.     MessageBox(hWnd, (LPSTR)"ChangeMenu failed", (LPSTR)"ERROR", MB_ICONHAND);
  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.   SetMenu(hWnd, hCurrentWindowMenu);
  38.  
  39.   return;
  40.   }
  41.  
  42. /**************************************************************************/
  43.  
  44. /* Procedure called when the application is loaded for the first time */
  45. BOOL WinInit(hInstance)
  46. HANDLE hInstance;
  47.   {
  48.   WNDCLASS   wcClass;
  49.  
  50.   wcClass.style          = CS_HREDRAW | CS_VREDRAW;
  51.   wcClass.lpfnWndProc    = WndProc;
  52.   wcClass.cbClsExtra     = 0;
  53.   wcClass.cbWndExtra     = 0;
  54.   wcClass.hInstance      = hInstance;
  55.   wcClass.hIcon          = LoadIcon(hInstance, NULL);
  56.   wcClass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  57.   wcClass.hbrBackground  = (HBRUSH)GetStockObject(WHITE_BRUSH);
  58.   wcClass.lpszMenuName   = (LPSTR)NULL;
  59.   wcClass.lpszClassName  = (LPSTR)"ChangeMenu";
  60.  
  61.   if (!RegisterClass((LPWNDCLASS) & wcClass))
  62.     return FALSE;
  63.  
  64.   return TRUE;
  65.   }
  66.  
  67. int     PASCAL WinMain(hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  68. HANDLE  hInstance, hPrevInstance;
  69. LPSTR   lpszCmdLine;
  70. int    cmdShow;
  71.   {
  72.   MSG   msg;
  73.   HWND  hWnd;
  74.  
  75.   if (!hPrevInstance)
  76.     {
  77.     if (!WinInit(hInstance))
  78.       return FALSE;
  79.     }
  80.  
  81.   hWnd = CreateWindow((LPSTR)"ChangeMenu",
  82.                      (LPSTR)"ChangeMenu()",
  83.                      WS_OVERLAPPEDWINDOW,
  84.                      CW_USEDEFAULT,
  85.                      CW_USEDEFAULT,
  86.                      CW_USEDEFAULT,
  87.                      CW_USEDEFAULT,
  88.                      (HWND)NULL,        /* no parent */
  89.                      (HMENU)NULL,       /* use class menu */
  90.                      (HANDLE)hInstance, /* handle to window instance */
  91.                      (LPSTR)NULL );     /* no params to pass on */
  92.  
  93.   ShowWindow(hWnd, cmdShow);
  94.   UpdateWindow(hWnd);
  95.  
  96.   while (GetMessage((LPMSG) & msg, NULL, 0, 0))
  97.     {
  98.     TranslateMessage((LPMSG) & msg);
  99.     DispatchMessage((LPMSG) & msg);
  100.     }
  101.   return (int)msg.wParam;
  102.   }
  103.  
  104.         /* Procedures which make up the window class. */
  105. long    FAR PASCAL WndProc(hWnd, message, wParam, lParam)
  106. HWND      hWnd;
  107. unsigned  message;
  108. WORD      wParam;
  109. LONG      lParam;
  110.   {
  111.   switch (message)
  112.     {
  113.     case WM_CREATE:
  114.       CALL_ChangeMenu (hWnd); /* Change the menu when the window is created */
  115.       break;
  116.  
  117.     case WM_DESTROY:
  118.       PostQuitMessage (0);
  119.       break;
  120.  
  121.     default:
  122.       return DefWindowProc(hWnd, message, wParam, lParam);
  123.       break;
  124.     }
  125.   return(0L);
  126.   }
  127.