Tricks of the Windows Game Programming Gurus:Advanced Windows Programming

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

#### Responding to Menu Event Messages

The only problem with `DEMO3_3.EXE` is that it doesn’t do anything! True, my young Jedi. The problem is that you don’t know how to detect the messages that menu item selections and manipulations generate. That is the topic of this section.

The Windows menu system generates a number of messages as you slide across top-level menu items as shown in Figure 3.11.

Figure 3.11 Window menu selection message flow.

The message we are interested in is sent when a menu item is selected and then the mouse is released. This denotes a selection. Selections send a `WM_COMMAND` message to the `WinProc()` of the window that the menu is attached to. The particular menu item ID and various other data is stored in the `wparam` and `lparam` of the message, as shown here:

  `msg`—`WM_COMMAND`
  `lparam`—The window handle that the message was sent from
  `wparam`—The ID of the menu item that was selected

> Tip: Technically, you should extract the low-order `WORD` from `wparam` with the `LOWORD()` macro to be safe. This macro is part of the standard includes, so you have access to it.

So all you have to do is `switch()` on the `wparam` parameter, with the cases being the different `MENUITEM` IDs defined in your menu, and you’re in business. For example, using the menu defined in `DEMO3_3.RC`, you would add the `WM_COMMAND` message handler and end up with something like this for your `WinProc()`:

    LRESULT CALLBACK WindowProc(HWND hwnd,

                      UINT msg,

                                WPARAM wparam,

                                LPARAM lparam)

    {

    // this is the main message handler of the system

    PAINTSTRUCT        ps;    // used in WM_PAINT

    HDC            hdc;    // handle to a device context

    // what is the message

    switch(msg)

        {

        case WM_CREATE:

            {

        // do initialization stuff here

           // return success

           return(0);

        } break;

            case WM_COMMAND:

            {

            switch(LOWORD(wparam))

                  {

                  // handle the FILE menu

                  case MENU_FILE_ID_OPEN:

                  {

                  // do work here

                  } break;

                  case MENU_FILE_ID_CLOSE:

                  {

                  // do work here

                  } break;

                  case MENU_FILE_ID_SAVE:

                  {

                  // do work here

                  } break;

                  case MENU_FILE_ID_EXIT:

                  {

                  // do work here

                  } break;

                  // handle the HELP menu

                  case MENU_HELP_ABOUT:

                  {

                  // do work here

                  } break;

                  default: break;

                  } // end switch wparam

            } break; // end WM_COMMAND

    case WM_PAINT:

    {

    // simply validate the window

    hdc = BeginPaint(hwnd,&ps);

    // you would do all your painting here

       EndPaint(hwnd,&ps);

       // return success

    return(0);

       } break;

       case WM_DESTROY:

       {

       // kill the application, this sends a WM_QUIT message

       PostQuitMessage(0);

           // return success

       return(0);

       } break;

         default:break;

       } // end switch

    // process any messages that we didn’t take care of

    return (DefWindowProc(hwnd, msg, wparam, lparam));

    } // end WinProc

It’s so easy, it should be illegal! Of course, there are other messages that manipulate the top-level menus and menu items themselves, but you can look in your Win32 SDK Help for more info. (I rarely need to know more than if a menu item was clicked or not.)

As a solid example of doing something with menus, I have created a cool sound demo that allows you to exit the program via the main menu, play one of four different teleporter sound effects, and finally pop up an About box via the Help menu. Also, the `.RC` file contains the sound, icon, and cursor resources. The program is `DEMO3_4.CPP`. Let’s take a look at the resource script and header first.

Contents of `DEMO3_4RES.H`:

    // defines for sounds resources

    #define SOUND_ID_ENERGIZE   1

    #define SOUND_ID_BEAM       2

    #define SOUND_ID_TELEPORT   3

    #define SOUND_ID_WARP       4

    // defines for icon and cursor

    #define ICON_T3DX        100

    #define CURSOR_CROSSHAIR 200

    // defines for the top level menu FILE

    #define MENU_FILE_ID_EXIT               1000

    // defines for play sound top level menu

    #define MENU_PLAY_ID_ENERGIZE           2000

    #define MENU_PLAY_ID_BEAM               2001

    #define MENU_PLAY_ID_TELEPORT           2002

    #define MENU_PLAY_ID_WARP               2003

    // defines for the top level menu HELP

    #define MENU_HELP_ABOUT                 3000

Contents of `DEMO3_4.RC`:

    #include "DEMO3_4RES.H"

    // the icon and cursor resource

    ICON_T3DX  ICON t3dx.ico

    CURSOR_CROSSHAIR CURSOR crosshair.cur

    // the sound resources

    SOUND_ID_ENERGIZE   WAVE energize.wav

    SOUND_ID_BEAM       WAVE beam.wav

    SOUND_ID_TELEPORT   WAVE teleport.wav

    SOUND_ID_WARP       WAVE warp.wav

    // the menu resource

    SoundMenu MENU DISCARDABLE

    {

    POPUP "&File"

        {

        MENUITEM "E&xit", MENU_FILE_ID_EXIT

        } // end popup

    POPUP "&PlaySound"

        {

            MENUITEM "Energize!",            MENU_PLAY_ID_ENERGIZE

            MENUITEM "Beam Me Up",           MENU_PLAY_ID_BEAM

            MENUITEM "Engage Teleporter",    MENU_PLAY_ID_TELEPORT

            MENUITEM "Quantum Warp Teleport", MENU_PLAY_ID_WARP

        } // end popup

    POPUP "Help"

        {

        MENUITEM "About", MENU_HELP_ABOUT

        } // end popup

    } // end top level menu

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.