home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / progwin / chap09 / menudemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-12  |  4.9 KB  |  138 lines

  1. /*-----------------------------------------
  2.    MENUDEMO.C -- Menu Demonstration
  3.                  (c) Charles Petzold, 1990
  4.   -----------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include "menudemo.h"
  8.  
  9. long FAR PASCAL WndProc (HWND, WORD, WORD, LONG) ;
  10.  
  11. char szAppName [] = "MenuDemo" ;
  12.  
  13. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  14.                     LPSTR lpszCmdLine, int nCmdShow)
  15.      {
  16.      HWND     hwnd ;
  17.      MSG      msg ;
  18.      WNDCLASS wndclass ;
  19.  
  20.      if (!hPrevInstance) 
  21.           {
  22.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  23.           wndclass.lpfnWndProc   = WndProc ;
  24.           wndclass.cbClsExtra    = 0 ;
  25.           wndclass.cbWndExtra    = 0 ;
  26.           wndclass.hInstance     = hInstance ;
  27.           wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  28.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  29.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  30.           wndclass.lpszMenuName  = szAppName ;
  31.           wndclass.lpszClassName = szAppName ;
  32.  
  33.           RegisterClass (&wndclass) ;
  34.           }
  35.  
  36.      hwnd = CreateWindow (szAppName, "Menu Demonstration",
  37.                           WS_OVERLAPPEDWINDOW,
  38.                           CW_USEDEFAULT, CW_USEDEFAULT,
  39.                           CW_USEDEFAULT, CW_USEDEFAULT,
  40.                           NULL, NULL, hInstance, NULL) ;
  41.  
  42.      ShowWindow (hwnd, nCmdShow) ;
  43.      UpdateWindow (hwnd) ;
  44.  
  45.      while (GetMessage (&msg, NULL, 0, 0))
  46.           {
  47.           TranslateMessage (&msg) ;
  48.           DispatchMessage (&msg) ;
  49.           }
  50.      return msg.wParam ;
  51.      }
  52.  
  53. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  54.      {
  55.      static int  wColorID [5] = { WHITE_BRUSH,  LTGRAY_BRUSH, GRAY_BRUSH,
  56.                                   DKGRAY_BRUSH, BLACK_BRUSH } ;
  57.      static WORD wSelection = IDM_WHITE ;
  58.      HMENU       hMenu ;
  59.  
  60.      switch (message)
  61.           {
  62.           case WM_COMMAND:
  63.                hMenu = GetMenu (hwnd) ;
  64.  
  65.                switch (wParam)
  66.                     {
  67.                     case IDM_NEW:
  68.                     case IDM_OPEN:
  69.                     case IDM_SAVE:
  70.                     case IDM_SAVEAS:
  71.                          MessageBeep (0) ;
  72.                          return 0 ;
  73.  
  74.                     case IDM_EXIT:
  75.                          SendMessage (hwnd, WM_CLOSE, 0, 0L) ;
  76.                          return 0 ;
  77.  
  78.                     case IDM_UNDO:
  79.                     case IDM_CUT:
  80.                     case IDM_COPY:
  81.                     case IDM_PASTE:
  82.                     case IDM_CLEAR:
  83.                          MessageBeep (0) ;
  84.                          return 0 ;
  85.  
  86.                     case IDM_WHITE:          // Note: Logic below
  87.                     case IDM_LTGRAY:         //   assumes that IDM_WHITE
  88.                     case IDM_GRAY:           //   through IDM_BLACK are
  89.                     case IDM_DKGRAY:         //   consecutive numbers in
  90.                     case IDM_BLACK:          //   the order shown here.
  91.  
  92.                          CheckMenuItem (hMenu, wSelection, MF_UNCHECKED) ;
  93.                          wSelection = wParam ;
  94.                          CheckMenuItem (hMenu, wSelection, MF_CHECKED) ;
  95.      
  96.                          SetClassWord (hwnd, GCW_HBRBACKGROUND,
  97.                               GetStockObject (wColorID [wParam - IDM_WHITE])) ;
  98.  
  99.                          InvalidateRect (hwnd, NULL, TRUE) ;
  100.                          return 0 ;
  101.  
  102.                     case IDM_START:
  103.                          if (SetTimer (hwnd, 1, 1000, NULL))
  104.                               {
  105.                               EnableMenuItem (hMenu, IDM_START, MF_GRAYED) ;
  106.                               EnableMenuItem (hMenu, IDM_STOP,  MF_ENABLED) ;
  107.                               }
  108.                          return 0 ;
  109.  
  110.                     case IDM_STOP:
  111.                          KillTimer (hwnd, 1) ;
  112.                          EnableMenuItem (hMenu, IDM_START, MF_ENABLED) ;
  113.                          EnableMenuItem (hMenu, IDM_STOP,  MF_GRAYED) ;
  114.                          return 0 ;
  115.  
  116.                     case IDM_HELP:
  117.                          MessageBox (hwnd, "Help not yet implemented.",
  118.                                      szAppName, MB_ICONINFORMATION | MB_OK) ;
  119.                          return 0 ;
  120.  
  121.                     case IDM_ABOUT:
  122.                          MessageBox (hwnd, "Menu Demonstration Program.",
  123.                                      szAppName, MB_ICONINFORMATION | MB_OK) ;
  124.                          return 0 ;
  125.                     }
  126.                break ;
  127.  
  128.           case WM_TIMER:
  129.                MessageBeep (0) ;
  130.                return 0 ;
  131.  
  132.           case WM_DESTROY :
  133.                PostQuitMessage (0) ;
  134.                return 0 ;
  135.           }
  136.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  137.      }
  138.