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

  1. /*
  2.  *  Program Name:    stmenu.c
  3.  *
  4.  *  Description:
  5.  *   The program below creates a menu and sets it in the window.
  6.  *
  7.  */
  8.  
  9. #include "windows.h"
  10.  
  11. long FAR PASCAL WndProc(HWND, unsigned, WORD, LONG);
  12.  
  13. /***********************************************************************/
  14.  
  15. void CALL_SetMenu(hWnd, hDC)
  16. HWND hWnd;
  17. HDC hDC;
  18. {
  19.   HMENU         hCurrentWindowMenu;
  20.   unsigned      wIDItem;
  21.   BOOL          bSet;
  22.  
  23.   hCurrentWindowMenu = CreateMenu();
  24.   ChangeMenu (hCurrentWindowMenu, NULL, (LPSTR) "Heading", wIDItem,
  25.               MF_APPEND | MF_BYCOMMAND | MF_STRING);
  26.  
  27.   bSet = SetMenu(hWnd, hCurrentWindowMenu);  /* menu set and displayed */
  28.  
  29.   if (bSet == FALSE)
  30.     MessageBox(hWnd, (LPSTR)"SetMenu failed", (LPSTR)"ERROR", MB_ICONHAND);
  31.  
  32.   return;
  33. }
  34.  
  35. /**************************************************************************/
  36.  
  37. /* Procedure called when the application is loaded for the first time */
  38. BOOL WinInit( hInstance )
  39. HANDLE hInstance;
  40. {
  41.     WNDCLASS   wcClass;
  42.  
  43.     wcClass.style          = CS_HREDRAW | CS_VREDRAW;
  44.     wcClass.lpfnWndProc    = WndProc;
  45.     wcClass.cbClsExtra     =0;
  46.     wcClass.cbWndExtra     =0;
  47.     wcClass.hInstance      = hInstance;
  48.     wcClass.hIcon          = LoadIcon( hInstance,NULL );
  49.     wcClass.hCursor        = LoadCursor( NULL, IDC_ARROW );
  50.     wcClass.hbrBackground  = (HBRUSH)GetStockObject( WHITE_BRUSH );
  51.     wcClass.lpszMenuName   = (LPSTR)NULL;
  52.     wcClass.lpszClassName  = (LPSTR)"SetMenu";
  53.  
  54.     if (!RegisterClass( (LPWNDCLASS)&wcClass ) )
  55.         /* Initialization failed.
  56.          * Windows will automatically deallocate all allocated memory.
  57.          */
  58.         return FALSE;
  59.  
  60.     return TRUE;        /* Initialization succeeded */
  61. }
  62.  
  63.  
  64. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  65. HANDLE hInstance, hPrevInstance;
  66. LPSTR lpszCmdLine;
  67. int cmdShow;
  68. {
  69.     MSG   msg;
  70.     HWND  hWnd;
  71.  
  72.     if (!hPrevInstance)
  73.         {
  74.         /* Call initialization procedure if this is the first instance */
  75.         if (!WinInit( hInstance ))
  76.             return FALSE;
  77.         }
  78.  
  79.     hWnd = CreateWindow((LPSTR)"SetMenu",
  80.                         (LPSTR)"SetMenu()",
  81.                         WS_OVERLAPPEDWINDOW,
  82.                         CW_USEDEFAULT,
  83.                         CW_USEDEFAULT,
  84.                         CW_USEDEFAULT,
  85.                         CW_USEDEFAULT,
  86.                         (HWND)NULL,        /* no parent */
  87.                         (HMENU)NULL,       /* use class menu */
  88.                         (HANDLE)hInstance, /* handle to window instance */
  89.                         (LPSTR)NULL        /* no params to pass on */
  90.                         );
  91.  
  92.     /* Make window visible according to the way the app is activated */
  93.     ShowWindow( hWnd, cmdShow );
  94.     UpdateWindow( hWnd );
  95.  
  96.     /* Polling messages from event queue */
  97.     while (GetMessage((LPMSG)&msg, NULL, 0, 0))
  98.         {
  99.         TranslateMessage((LPMSG)&msg);
  100.         DispatchMessage((LPMSG)&msg);
  101.         }
  102.  
  103.     return (int)msg.wParam;
  104. }
  105.  
  106. /* Procedures which make up the window class. */
  107. long FAR PASCAL WndProc( hWnd, message, wParam, lParam )
  108. HWND hWnd;
  109. unsigned message;
  110. WORD wParam;
  111. LONG lParam;
  112. {
  113.     PAINTSTRUCT ps;
  114.  
  115.     switch (message)
  116.     {
  117.  
  118.     case WM_PAINT:
  119.         BeginPaint( hWnd, (LPPAINTSTRUCT)&ps );
  120.         CALL_SetMenu(hWnd, ps.hdc);
  121.         ValidateRect(hWnd, (LPRECT) NULL);
  122.         EndPaint( hWnd, (LPPAINTSTRUCT)&ps );
  123.         break;
  124.  
  125.     case WM_DESTROY:
  126.         PostQuitMessage( 0 );
  127.         break;
  128.  
  129.     default:
  130.         return DefWindowProc( hWnd, message, wParam, lParam );
  131.         break;
  132.     }
  133.     return(0L);
  134. }
  135.