home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 7.ddi / MWHC.007 / 1A < prev    next >
Encoding:
Text File  |  1991-09-11  |  20.4 KB  |  692 lines

  1. /***************************************************************************
  2.  *                                       *
  3.  *  PROGRAM    : Menu.c                           *
  4.  *                                       *
  5.  *  PURPOSE    : To give a demonstration of the use of popup menus, user  *
  6.  *          defined menus and menu functions.               *
  7.  *                                       *
  8.  *  FUNCTIONS    : WinMain()          - Calls the initialization function  *
  9.  *                    and enters the message loop.       *
  10.  *                                       *
  11.  *          MenuInit()          - Registers the app. window class.   *
  12.  *                                       *
  13.  *          About()          - Dialog function for the About..    *
  14.  *                    dialog.                *
  15.  *                                       *
  16.  *          ShrinkBitmap()      - Shrinks a 64x64 bitmap to a size   *
  17.  *                    useable for a user-defined menu    *
  18.  *                    checkmark.               *
  19.  *                                       *
  20.  *          HandleCreate()      - Creates a new menu and appends it  *
  21.  *                    to the main menu           *
  22.  *                                       *
  23.  *          HandlePaint()       - Handles repainting the app's client*
  24.  *                    area                   *
  25.  *                                       *
  26.  *          HandleChangeColors()- Changes the state of the "colors"  *
  27.  *                    menu item.               *
  28.  *                                       *
  29.  *          HandleDrawItem()    - Redraws the menu items in the       *
  30.  *                    "colors" menu               *
  31.  *                                       *
  32.  *          HandlePopupMenu()   - handles display of the "floating"  *
  33.  *                    popup.                   *
  34.  *                                       *
  35.  *          MenuWndProc()       - Window function for the app.       *
  36.  *                                       *
  37.  *                                       *
  38.  ***************************************************************************/
  39. #include "windows.h"
  40. #include "menu.h"
  41. #include <string.h>
  42.  
  43. HANDLE     hInst;
  44. HMENU     hBigMenu;
  45. HBITMAP  hbmCheckOn;
  46. HBITMAP  hbmCheckOff;
  47.  
  48. /****************************************************************************
  49.  *                                        *
  50.  *  FUNCTION   : WinMain(HANDLE, HANDLE, LPSTR, int)                *
  51.  *                                        *
  52.  *  PURPOSE    : Creates the main app. window, calls an initialization        *
  53.  *         function and enters the message loop.                *
  54.  *                                        *
  55.  ****************************************************************************/
  56. int PASCAL WinMain (hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  57.  
  58. HANDLE hInstance, hPrevInstance;
  59. LPSTR lpCmdLine;
  60. int nCmdShow;
  61. {
  62.     HWND  hWnd;
  63.     MSG   msg;
  64.  
  65.     /* Register main window class if this is the first instance of the app. */
  66.     if (!hPrevInstance)
  67.     if (!MenuInit (hInstance))
  68.         return NULL;
  69.  
  70.     hInst = hInstance;
  71.  
  72.     /* Create the app. window */
  73.     hWnd = CreateWindow ("menu",
  74.              "Menu Example",
  75.              WS_OVERLAPPEDWINDOW,
  76.              CW_USEDEFAULT,
  77.              CW_USEDEFAULT,
  78.              CW_USEDEFAULT,
  79.              CW_USEDEFAULT,
  80.              (HWND) NULL,
  81.              NULL,
  82.              hInstance,
  83.              (LPSTR) NULL);
  84.  
  85.     if (!hWnd)
  86.     return NULL;
  87.  
  88.     ShowWindow (hWnd, nCmdShow);
  89.     UpdateWindow (hWnd);
  90.  
  91.     while (GetMessage (&msg, NULL, NULL, NULL)){
  92.     /* Since we have no accelerators, no need to call
  93.      * TranslateAccelerator here.
  94.      */
  95.     TranslateMessage (&msg);
  96.     DispatchMessage (&msg);
  97.     }
  98.     return(msg.wParam);
  99. }
  100.  
  101.  
  102. /****************************************************************************
  103.  *                                        *
  104.  *  FUNCTION   : MenuInit (hInstance)                        *
  105.  *                                        *
  106.  *  PURPOSE    : Registers the main window class.                *
  107.  *                                        *
  108.  *  RETURNS    : TRUE    -  if RegisterClass() went off ok            *
  109.  *         FALSE    -  otherwise.                        *
  110.  *                                        *
  111.  ****************************************************************************/
  112. BOOL NEAR PASCAL MenuInit (hInstance)
  113.  
  114. HANDLE hInstance;
  115.  
  116. {
  117.     PWNDCLASS pWndClass;
  118.     WNDCLASS  ww;
  119.     BOOL      bSuccess;
  120.  
  121.     pWndClass = &ww;
  122.     memset(pWndClass, 0, sizeof ww);
  123.  
  124.     pWndClass->style         = NULL;
  125.     pWndClass->lpfnWndProc   = MenuWndProc;
  126.     pWndClass->hInstance     = hInstance;
  127.     pWndClass->hIcon         = LoadIcon (hInstance, "menu");
  128.     pWndClass->hCursor         = LoadCursor (NULL, IDC_ARROW);
  129.     pWndClass->hbrBackground = GetStockObject (WHITE_BRUSH);
  130.     pWndClass->lpszMenuName  = "MenuMenu",
  131.     pWndClass->lpszClassName = "menu";
  132.  
  133.     bSuccess = RegisterClass (pWndClass);
  134.  
  135.     return bSuccess;
  136. }
  137.  
  138.  
  139. /****************************************************************************
  140.  *                                        *
  141.  *  FUNCTION   : About (hDlg, message, wParam, lParam)                *
  142.  *                                        *
  143.  *  PURPOSE    : Dialog function for the About menu... dialog.            *
  144.  *                                        *
  145.  ****************************************************************************/
  146. BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
  147.  
  148. HWND hDlg;
  149. unsigned message;
  150. WORD wParam;
  151. LONG lParam;
  152.  
  153. {
  154.     switch (message){
  155.     case WM_INITDIALOG:
  156.         return(TRUE);
  157.  
  158.     case WM_COMMAND:
  159.         if (wParam == IDOK){
  160.         EndDialog(hDlg,NULL);
  161.         return(TRUE);
  162.         }
  163.         break;
  164.     }
  165.     return(FALSE);
  166. }
  167.  
  168.  
  169. /****************************************************************************
  170.  *                                        *
  171.  *  FUNCTION   : ShrinkBitmap(hwnd, hbm)                    *
  172.  *                                        *
  173.  *  PURPOSE    : This function shrinks a 64x64 bitmap into a bitmap useable *
  174.  *         for the user-defined checkmark for menu items. This can be *
  175.  *         easily generalized to shrink bitmaps of any size.        *
  176.  *                                        *
  177.  *  RETURNS    : HBITMAP - A handle to the new bitmap.                *
  178.  *                                        *
  179.  ****************************************************************************/
  180. HBITMAP FAR PASCAL ShrinkBitmap (hwnd, hbm)
  181.  
  182. HWND hwnd;
  183. HBITMAP hbm;
  184.  
  185. {
  186.     HDC     hdc;
  187.     HDC     hmemorydcNew;
  188.     HDC     hmemorydcOld;
  189.     LONG    checkMarkSize;
  190.     HBITMAP hCheckBitmap;
  191.     HBITMAP hOldBitmapSave;
  192.     HBITMAP hNewBitmapSave;
  193.  
  194.     hdc = GetDC (hwnd);
  195.  
  196.     /* Create DCs for the source (old) and target (new) bitmaps */
  197.     hmemorydcNew = CreateCompatibleDC (hdc);
  198.     hmemorydcOld = CreateCompatibleDC (hdc);
  199.  
  200.     /* Determine the dimensions of the default menu checkmark and
  201.      * create a target bitmap of the same dimensions
  202.      */
  203.     checkMarkSize = GetMenuCheckMarkDimensions ();
  204.     hCheckBitmap  = CreateCompatibleBitmap (hdc,
  205.                         LOWORD (checkMarkSize),
  206.                         HIWORD (checkMarkSize));
  207.  
  208.     /* Select the source bitmap and the target bitmap into their
  209.      * respective DCs.
  210.      */
  211.     hOldBitmapSave = SelectObject (hmemorydcNew, hCheckBitmap);
  212.     hNewBitmapSave = SelectObject (hmemorydcOld, hbm);
  213.  
  214.     /* Shrink the source bitmap into the target DC */
  215.     StretchBlt (hmemorydcNew,
  216.         0,
  217.         0,
  218.         LOWORD(checkMarkSize),
  219.         HIWORD(checkMarkSize),
  220.         hmemorydcOld,
  221.         0,
  222.         0,
  223.         64,
  224.         64,
  225.         SRCCOPY);
  226.  
  227.     /* De-select the bitmaps and clean up .. */
  228.     SelectObject (hmemorydcNew, hOldBitmapSave);
  229.     SelectObject (hmemorydcOld, hNewBitmapSave);
  230.     DeleteDC (hmemorydcNew);
  231.     DeleteDC (hmemorydcOld);
  232.     ReleaseDC (hwnd, hdc);
  233.  
  234.     /* .. and return a handle to the target bitmap */
  235.     return hCheckBitmap;
  236. }
  237.  
  238.  
  239. /****************************************************************************
  240.  *                                        *
  241.  *  FUNCTION   : HandleCreate ( hwnd )                        *
  242.  *                                        *
  243.  *  PURPOSE    : Creates a new (empty) menu and appends to it the "State"   *
  244.  *         menu items. It sets up the user-defined checkmarks for the *
  245.  *         menu. It then inserts this menu into the main menu bar.    *
  246.  *                                        *
  247.  ****************************************************************************/
  248. void FAR PASCAL HandleCreate (hwnd)
  249. HWND hwnd;
  250. {
  251.     HMENU   hMenu;
  252.     HMENU   hWndMenu;
  253.  
  254.     /* Create a new menu into the menubar on the fly */
  255.     hMenu = CreateMenu ();
  256.     if (!hMenu)
  257.     return;
  258.  
  259.     /* Append the state menu items to it */
  260.     AppendMenu (hMenu, MF_STRING, IDM_STATE1, "South Dakota");
  261.     AppendMenu (hMenu, MF_STRING, IDM_STATE2, "Washington");
  262.     AppendMenu (hMenu, MF_STRING, IDM_STATE3, "California");
  263.     if (!AppendMenu (hMenu, MF_STRING, IDM_STATE4, "Oregon")){
  264.     /* It is unlikely the other appends will fail and this will succeed.
  265.      * So just check this one. And if it fails, Destroy the menu for
  266.      * good measure and return.
  267.      */
  268.     DestroyMenu(hMenu);
  269.     return;
  270.     }
  271.     hbmCheckOn    = ShrinkBitmap (hwnd, LoadBitmap (hInst, "checkon"));
  272.     hbmCheckOff = ShrinkBitmap (hwnd, LoadBitmap (hInst, "checkoff"));
  273.  
  274.     /* Set up the user-defined check marks */
  275.     SetMenuItemBitmaps (hMenu, 0, MF_BYPOSITION, hbmCheckOff, hbmCheckOn);
  276.     SetMenuItemBitmaps (hMenu, 1, MF_BYPOSITION, hbmCheckOff, hbmCheckOn);
  277.     SetMenuItemBitmaps (hMenu, 2, MF_BYPOSITION, hbmCheckOff, hbmCheckOn);
  278.     SetMenuItemBitmaps (hMenu, 3, MF_BYPOSITION, hbmCheckOff, hbmCheckOn);
  279.  
  280.     /* Now insert the menu into the main menu bar. */
  281.     hWndMenu = GetMenu (hwnd);
  282.     InsertMenu (hWndMenu, 2, MF_POPUP|MF_BYPOSITION, (WORD)hMenu, "States");
  283.     return;
  284. }
  285.  
  286. /****************************************************************************
  287.  *                                        *
  288.  *  FUNCTION   : HandlePaint ( hwnd )                        *
  289.  *                                        *
  290.  *  PURPOSE    : Handles the repainting of the main app's client area.      *
  291.  *                                        *
  292.  ****************************************************************************/
  293. void FAR PASCAL HandlePaint (hwnd)
  294. HWND hwnd;
  295. {
  296.     HDC     hdc;
  297.     PAINTSTRUCT ps;
  298.     RECT    rc;
  299.  
  300.     hdc = BeginPaint (hwnd, (LPPAINTSTRUCT)&ps);
  301.  
  302.     /* Center the text in the client area */
  303.     GetClientRect (hwnd, (LPRECT)&rc);
  304.     DrawText (hdc,
  305.           "Down click in the window for a popup menu",
  306.            41,
  307.            (LPRECT)&rc,
  308.            DT_CENTER | DT_WORDBREAK);
  309.     EndPaint(hwnd, (LPPAINTSTRUCT)&ps);
  310. }
  311.  
  312. char szText[100];
  313.  
  314. /****************************************************************************
  315.  *                                        *
  316.  *  FUNCTION   : HandleChangeColors (hwnd)                    *
  317.  *                                        *
  318.  *  PURPOSE    : Toggles the state of the Owner Draw item in the Colors     *
  319.  *         menu. If it is on, the "Black", "Blue", "Red", and "Green" *
  320.  *               individual menu text items are modified so that they will  *
  321.  *         contain bands of color. Otherwise, the colors are replaced *
  322.  *         by the text.                                               *
  323.  *                                        *
  324.  ****************************************************************************/
  325. void FAR PASCAL HandleChangeColors(hwnd)
  326. HWND hwnd;
  327. {
  328.     HMENU hMenu=0;
  329.     BOOL  fOwnerDraw;
  330.  
  331.     /* Get a handle to the Colors menu. This is at position 1. */
  332.     hMenu = GetSubMenu (GetMenu (hwnd), IDCOLORS_POS);
  333.  
  334.     /* Get the current state of the item */
  335.     fOwnerDraw = GetMenuState ( hMenu,
  336.                 IDM_COLOROWNERDR, MF_BYCOMMAND) & MF_CHECKED;
  337.  
  338.     /* Toggle the state of the item. */
  339.     CheckMenuItem ( hMenu,
  340.             IDM_COLOROWNERDR,
  341.             MF_BYCOMMAND | (fOwnerDraw ? MF_UNCHECKED : MF_CHECKED));
  342.  
  343.     if (!fOwnerDraw){
  344.     /* Change the items to owner-draw items. Pass the RGB value for the
  345.      * color as the application-supplied data. This makes it easier for
  346.      * us to draw the items.
  347.      */
  348.     ModifyMenu(hMenu,
  349.            IDM_BLACK,
  350.            MF_OWNERDRAW | MF_BYCOMMAND,
  351.            IDM_BLACK,
  352.            DWORDTOLPSTR(RGB(0,0,0)));
  353.  
  354.     ModifyMenu(hMenu,
  355.            IDM_BLUE,
  356.            MF_OWNERDRAW | MF_BYCOMMAND,
  357.            IDM_BLUE,
  358.            DWORDTOLPSTR(RGB(0,0,255)));
  359.  
  360.     ModifyMenu(hMenu,
  361.            IDM_RED,
  362.            MF_OWNERDRAW | MF_BYCOMMAND,
  363.            IDM_RED,
  364.            DWORDTOLPSTR(RGB(255,0,0)));
  365.  
  366.     ModifyMenu(hMenu,
  367.            IDM_GREEN,
  368.            MF_OWNERDRAW | MF_BYCOMMAND,
  369.            IDM_GREEN,
  370.            DWORDTOLPSTR(RGB(0,255,0)));
  371.     }
  372.     else {
  373.     /* Change the items to normal text items. */
  374.     ModifyMenu(hMenu, IDM_BLACK, MF_BYCOMMAND, IDM_BLACK, "Black");
  375.     ModifyMenu(hMenu, IDM_BLUE, MF_BYCOMMAND, IDM_BLUE, "Blue");
  376.     ModifyMenu(hMenu, IDM_RED, MF_BYCOMMAND, IDM_RED, "Red");
  377.     ModifyMenu(hMenu, IDM_GREEN, MF_BYCOMMAND, IDM_GREEN, "Green");
  378.     }
  379. }
  380.  
  381.  
  382. /****************************************************************************
  383.  *                                        *
  384.  *  FUNCTION   : HandleDrawItem ( hwnd, lpdis)                    *
  385.  *                                        *
  386.  *  PURPOSE    : Called in response to a WM_DRAWITEM message, i.e. when the *
  387.  *         colors menu is being modified to an owner-draw menu, or    *
  388.  *         one of the items is selected. It sizes the checkmark bitmap*
  389.  *         to fit next to a color band and draws the color bands and  *
  390.  *         the checkmark on the popup menu.                *
  391.  *                                        *
  392.  ****************************************************************************/
  393. void FAR PASCAL HandleDrawItem(hwnd, lpdis)
  394. HWND         hwnd;
  395. LPDRAWITEMSTRUCT lpdis;
  396.  
  397. {
  398.     HDC     hdcBitmap;
  399.     HBITMAP hbmSave;
  400.     HBRUSH  hbr;
  401.     RECT    rc;
  402.     LONG    checkMarkSize;
  403.     DWORD   textColorSave;
  404.     DWORD   bkColorSave;
  405.  
  406.     /* Get the size of the checkmark so we can leave room for it since we
  407.      * want to be able to check the selected color.
  408.      */
  409.     checkMarkSize = GetMenuCheckMarkDimensions ();
  410.  
  411.     if (lpdis->itemAction == ODA_SELECT ||
  412.     lpdis->itemAction == ODA_DRAWENTIRE){
  413.  
  414.     CopyRect ((LPRECT)&rc, (LPRECT)&lpdis->rcItem);
  415.     InflateRect ((LPRECT)&rc, (-2 - LOWORD(checkMarkSize)), -2);
  416.  
  417.     if (lpdis->itemState & ODS_SELECTED)
  418.     {
  419.         /* Item has been selected -- hilite with a gray frame */
  420.         hbr = GetStockObject (GRAY_BRUSH);
  421.         FrameRect (lpdis->hDC, (LPRECT)&rc, hbr);
  422.     }
  423.     else if (lpdis->itemAction == ODA_SELECT)
  424.     {
  425.         /* Item has been de-selected -- remove gray frame */
  426.         hbr = CreateSolidBrush (GetSysColor (COLOR_MENU));
  427.         FrameRect (lpdis->hDC, (LPRECT)&rc, hbr);
  428.         DeleteObject (hbr);
  429.     }
  430.     }
  431.  
  432.     if (lpdis->itemAction == ODA_DRAWENTIRE){
  433.  
  434.     /* Paint the color item in the color requested. */
  435.     hbr = CreateSolidBrush (lpdis->itemData);
  436.     CopyRect ((LPRECT)&rc, (LPRECT)&lpdis->rcItem);
  437.     InflateRect ((LPRECT)&rc, -10-LOWORD(checkMarkSize), -10);
  438.     FillRect (lpdis->hDC, (LPRECT)&rc, hbr);
  439.     DeleteObject (hbr);
  440.  
  441.     if (lpdis->itemState & ODS_CHECKED){
  442.         /* Draw the check mark if the item is checked. */
  443.         hdcBitmap = CreateCompatibleDC (lpdis->hDC);
  444.         hbmSave = SelectObject (hdcBitmap, hbmCheckOn);
  445.  
  446.         textColorSave = SetTextColor (lpdis->hDC, 0x00000000L);
  447.         bkColorSave   = SetBkColor (lpdis->hDC, 0x00FFFFFFL);
  448.  
  449.         /* Use Magic bitblt op so that monochrome bitmaps preserve
  450.            background and foreground colors. */
  451.         BitBlt (lpdis->hDC,
  452.             lpdis->rcItem.left,
  453.             lpdis->rcItem.top+
  454.                (MEASUREITEMHEIGHT - HIWORD (checkMarkSize)) / 2,
  455.             LOWORD (checkMarkSize),
  456.             HIWORD (checkMarkSize),
  457.             hdcBitmap,
  458.             0,
  459.             0,
  460.             ROP_PSDPxax);
  461.  
  462.         /* Restore colors and bitmap and clean up */
  463.         SetTextColor (lpdis->hDC, textColorSave);
  464.         SetBkColor (lpdis->hDC, bkColorSave);
  465.         SelectObject (hdcBitmap, hbmSave);
  466.         DeleteDC (hdcBitmap);
  467.  
  468.     }
  469.     }
  470. }
  471.  
  472. /****************************************************************************
  473.  *                                        *
  474.  *  FUNCTION   : HandlePopupMenu (hwnd, point)                    *
  475.  *                                        *
  476.  *  PURPOSE    : Handles the display of the "floating" popup that appears   *                                *
  477.  *         on a mouse click in the app's client area.                 *
  478.  *                                        *
  479.  ****************************************************************************/
  480. void FAR PASCAL HandlePopupMenu (hwnd, point)
  481. HWND   hwnd;
  482. POINT  point;
  483.  
  484. {
  485.     HMENU hMenu;
  486.     HMENU hMenuTrackPopup;
  487.  
  488.     /* Get the menu for the popup from the resource file. */
  489.     hMenu = LoadMenu (hInst, "PopupMenu");
  490.     if (!hMenu)
  491.     return;
  492.  
  493.     /* Get the first menu in it which we will use for the call to
  494.      * TrackPopup(). This could also have been created on the fly using
  495.      * CreatePopupMenu and then we could have used InsertMenu() or
  496.      * AppendMenu.
  497.      */
  498.     hMenuTrackPopup = GetSubMenu (hMenu, 0);
  499.  
  500.     /* Convert the mouse point to screen coordinates since that is what
  501.      * TrackPopup expects.
  502.      */
  503.     ClientToScreen (hwnd, (LPPOINT)&point);
  504.  
  505.     /* Draw and track the "floating" popup */
  506.     TrackPopupMenu (hMenuTrackPopup, 0, point.x, point.y, 0, hwnd, NULL);
  507.  
  508.     /* Destroy the menu since were are done with it. */
  509.     DestroyMenu (hMenu);
  510. }
  511.  
  512. /****************************************************************************
  513.  *                                        *
  514.  *  FUNCTION   : MenuWndProc (hWnd, message, wParam, lParam)            *
  515.  *                                        *
  516.  *  PURPOSE    : Window function for the main app. window. Processes all the*
  517.  *         menu selections and oter messages.                *
  518.  *                                        *
  519.  ****************************************************************************/
  520. long FAR PASCAL MenuWndProc (hWnd, message, wParam, lParam)
  521. HWND hWnd;
  522. WORD message;
  523. WORD wParam;
  524. LONG lParam;
  525.  
  526. {
  527.     FARPROC lpProc;
  528.     HMENU hMenu;
  529.     RECT rc;
  530.  
  531.     switch (message){
  532.     case WM_SYSCOMMAND:
  533.         /* Show the About ... dialog */
  534.         if (wParam == ID_ABOUT){
  535.         lpProc = MakeProcInstance (About, hInst);
  536.         DialogBox (hInst,
  537.                "AboutBox",
  538.                hWnd,
  539.                lpProc);
  540.  
  541.         FreeProcInstance (lpProc);
  542.         break;
  543.         }
  544.         else
  545.           return DefWindowProc (hWnd, message, wParam, lParam);
  546.  
  547.     case WM_COMMAND:
  548.         switch (wParam){
  549.          case IDM_EXIT:
  550.            DestroyWindow (hWnd);
  551.            break;
  552.  
  553.          case IDM_ABOUT:
  554.            /* Bring up the About.. dialog box */
  555.            lpProc = MakeProcInstance (About, hInst);
  556.            DialogBox (hInst,
  557.                   "AboutBox",
  558.                   hWnd,
  559.                   lpProc);
  560.  
  561.            FreeProcInstance (lpProc);
  562.            break;
  563.  
  564.          case IDM_COLOROWNERDR:
  565.              /* Change colors in color menu depending on state of this
  566.             menu item. */
  567.              HandleChangeColors (hWnd);
  568.              break;
  569.  
  570.          case IDM_STATE1:
  571.          case IDM_STATE2:
  572.          case IDM_STATE3:
  573.          case IDM_STATE4:
  574.               /* Get a handle to the states menu... */
  575.               hMenu = GetSubMenu (GetMenu (hWnd), IDSTATES_POS);
  576.  
  577.               /* Uncheck all the items. */
  578.               CheckMenuItem (hMenu, IDM_STATE1,
  579.                      MF_BYCOMMAND | MF_UNCHECKED);
  580.               CheckMenuItem (hMenu, IDM_STATE2,
  581.                      MF_BYCOMMAND | MF_UNCHECKED);
  582.               CheckMenuItem (hMenu, IDM_STATE3,
  583.                      MF_BYCOMMAND | MF_UNCHECKED);
  584.               CheckMenuItem (hMenu, IDM_STATE4,
  585.                      MF_BYCOMMAND | MF_UNCHECKED);
  586.  
  587.               /* ...and just check the selected one.*/
  588.               CheckMenuItem (hMenu, wParam,
  589.                      MF_BYCOMMAND | MF_CHECKED);
  590.              break;
  591.  
  592.          case IDM_BLACK:
  593.          case IDM_RED:
  594.          case IDM_BLUE:
  595.          case IDM_GREEN:
  596.               /* Get a handle to the Colors menu. */
  597.               hMenu = GetSubMenu (GetMenu (hWnd),IDCOLORS_POS);
  598.  
  599.               /* Uncheck all the items. */
  600.               CheckMenuItem (hMenu, IDM_BLACK,
  601.                      MF_BYCOMMAND | MF_UNCHECKED);
  602.               CheckMenuItem (hMenu, IDM_RED,
  603.                      MF_BYCOMMAND | MF_UNCHECKED);
  604.               CheckMenuItem (hMenu, IDM_BLUE,
  605.                      MF_BYCOMMAND | MF_UNCHECKED);
  606.               CheckMenuItem (hMenu, IDM_GREEN,
  607.                      MF_BYCOMMAND | MF_UNCHECKED);
  608.  
  609.               /* ...and just check the selected one.*/
  610.               CheckMenuItem (hMenu, wParam,
  611.                      MF_BYCOMMAND | MF_CHECKED);
  612.               break;
  613.  
  614.          case IDM_FONT:
  615.               /* Messages sent to us from TrackPopupMenu when
  616.                * items are selected from the "floating" popups
  617.                */
  618.               MessageBox (hWnd,
  619.                   "A font was selected",
  620.                   "Popup Menu Alert",
  621.                   MB_APPLMODAL|MB_OK);
  622.               break;
  623.  
  624.          case IDM_SIZE:
  625.               MessageBox (hWnd,
  626.                   "A size was selected",
  627.                   "Popup Menu Alert",
  628.                   MB_APPLMODAL|MB_OK);
  629.               break;
  630.  
  631.          case IDM_STYLE:
  632.               MessageBox (hWnd,
  633.                   "A style was selected",
  634.                   "Popup Menu Alert",
  635.                   MB_APPLMODAL|MB_OK);
  636.               break;
  637.         }
  638.         break;
  639.  
  640.     case WM_SIZE:
  641.         if (lParam){
  642.         /* If window is being sized to a non zero value...
  643.          * invalidate it's client area.
  644.          */
  645.         InvalidateRect (hWnd, NULL, TRUE);
  646.         }
  647.         break;
  648.  
  649.     case WM_PAINT:
  650.         HandlePaint (hWnd);
  651.         break;
  652.  
  653.     case WM_MEASUREITEM:
  654.         /* Use the same width for all items. We could examine the item id
  655.            and use different widths/heights for each item. */
  656.         //((LPMEASUREITEMSTRUCT)lParam)->itemWidth  = MEASUREITEMWIDTH;
  657.         //((LPMEASUREITEMSTRUCT)lParam)->itemHeight = MEASUREITEMHEIGHT;
  658.         ((LPMEASUREITEMSTRUCT)BRK_FP(lParam))->itemWidth=MEASUREITEMWIDTH;
  659.         ((LPMEASUREITEMSTRUCT)BRK_FP(lParam))->itemHeight=MEASUREITEMHEIGHT;
  660.         return TRUE;
  661.  
  662.     case WM_DRAWITEM:
  663.         /* Redraw the "colors" menu in normal/ownerdrawmode */
  664.         HandleDrawItem (hWnd, (LPDRAWITEMSTRUCT)BRK_FP(lParam));
  665.         return TRUE;
  666.  
  667.     case WM_CREATE:
  668.         /* Create the menu */
  669.         HandleCreate (hWnd);
  670.         break;
  671.  
  672.     case WM_DESTROY:
  673.         /* Delete the on/off bitmaps so that they don't waste memory. */
  674.         DeleteObject (hbmCheckOn);
  675.         DeleteObject (hbmCheckOff);
  676.  
  677.         PostQuitMessage (0);
  678.         break;
  679.  
  680.     case WM_LBUTTONDOWN:
  681.         /* Draw the "floating" popup in the app's client area */
  682.         GetClientRect (hWnd, (LPRECT)&rc);
  683.         if (PtInRect ((LPRECT)&rc, MAKEPOINT (lParam)))
  684.         HandlePopupMenu (hWnd, MAKEPOINT(lParam));
  685.         break;
  686.  
  687.     default:
  688.         return DefWindowProc(hWnd, message, wParam, lParam);
  689.     }
  690.     return(NULL);
  691. }
  692.