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

  1. /*
  2.  *   This program demonstrates the use of the CheckMenuItem() function.
  3.  *   CheckMenuItem() checks or unchecks the given menu item. CheckMenuItem()
  4.  *   is called in response to a WM_COMMAND message in HelloWndProc() in
  5.  *   this sample application.
  6.  */
  7.  
  8. #include "windows.h"
  9. #include "ckmnuitm.h"
  10.  
  11. WORD   mOldChoice = NULL;   /* the last chosen menu item */
  12. HMENU  hMenu;            /* handle to menu of window  */
  13.  
  14. long    FAR PASCAL HelloWndProc(HWND, unsigned, WORD, LONG);
  15.  
  16. /* Procedure called when the application is loaded for the first time */
  17. BOOL HelloInit(hInstance)
  18. HANDLE hInstance;
  19.   {
  20.   PWNDCLASS   pHelloClass;
  21.  
  22.   pHelloClass = (PWNDCLASS)LocalAlloc(LPTR, sizeof(WNDCLASS));
  23.  
  24.   pHelloClass->hCursor        = LoadCursor(NULL, IDC_ARROW);
  25.   pHelloClass->hIcon          = LoadIcon(hInstance, NULL);
  26.   pHelloClass->lpszMenuName   = (LPSTR)"menucnt";
  27.   pHelloClass->lpszClassName  = (LPSTR)"CheckMenuItem";
  28.   pHelloClass->hbrBackground  = (HBRUSH)GetStockObject(WHITE_BRUSH);
  29.   pHelloClass->hInstance      = hInstance;
  30.   pHelloClass->style          = CS_HREDRAW | CS_VREDRAW;
  31.   pHelloClass->lpfnWndProc    = HelloWndProc;
  32.  
  33.   if (!RegisterClass((LPWNDCLASS)pHelloClass))
  34.     return FALSE;
  35.  
  36.   LocalFree((HANDLE)pHelloClass);
  37.   return TRUE;        /* Initialization succeeded */
  38.   }
  39.  
  40.  
  41. int     PASCAL WinMain(hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  42. HANDLE   hInstance, hPrevInstance;
  43. LPSTR    lpszCmdLine;
  44. int      cmdShow;
  45.   {
  46.   MSG   msg;
  47.   HWND  hWnd;
  48.  
  49.   HelloInit(hInstance);
  50.   hWnd = CreateWindow((LPSTR)"CheckMenuItem",
  51.                       (LPSTR)"CheckMenuItem()",
  52.                       WS_OVERLAPPEDWINDOW,
  53.                       CW_USEDEFAULT,
  54.                       CW_USEDEFAULT,
  55.                       CW_USEDEFAULT,
  56.                       CW_USEDEFAULT,
  57.                       (HWND)NULL,        /* no parent */
  58.                       (HMENU)NULL,       /* use class menu */
  59.                       (HANDLE)hInstance, /* handle to window instance */
  60.                       (LPSTR)NULL );     /* no params to pass on */
  61.  
  62.   ShowWindow(hWnd, cmdShow);
  63.   UpdateWindow(hWnd);
  64.  
  65.   hMenu = GetMenu(hWnd);     /* get the handle to the menu */
  66.  
  67.   while (GetMessage((LPMSG) & msg, NULL, 0, 0))
  68.     {
  69.     TranslateMessage((LPMSG) & msg);
  70.     DispatchMessage((LPMSG) & msg);
  71.     }
  72.   return (int)msg.wParam;
  73.   }
  74.  
  75.  
  76. /* Procedures which make up the window class. */
  77. long    FAR PASCAL HelloWndProc(hWnd, message, wParam, lParam)
  78. HWND     hWnd;
  79. unsigned message;
  80. WORD     wParam;
  81. LONG     lParam;
  82.   {
  83.   PAINTSTRUCT ps;
  84.  
  85.   switch (message)
  86.     {
  87.     case WM_COMMAND:
  88.       switch( wParam )
  89.         {
  90.         case IDM_CHOICE1:
  91.         case IDM_CHOICE2:
  92.         case IDM_CHOICE3:
  93.           if (mOldChoice)  /* if another menu item previously chosen */
  94.             CheckMenuItem(hMenu, mOldChoice, MF_UNCHECKED);   /* uncheck it */
  95.           mOldChoice = wParam;  /* keep track of this choice for later */
  96.           CheckMenuItem(hMenu, mOldChoice, MF_CHECKED);   /* check the new choice */
  97.           break;
  98.         }
  99.       break;
  100.  
  101.     case WM_DESTROY:
  102.       PostQuitMessage(0);
  103.       break;
  104.  
  105.     default:
  106.       return DefWindowProc(hWnd, message, wParam, lParam);
  107.       break;
  108.     }
  109.   return(0L);
  110.   }
  111.