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

### Working with Menus

Menus are one of the coolest things about a Windows program and are ultimately the point of interaction between the user and your program (that is, if you’re making a word processor <BG>). Knowing how to create and work with menus is very important because you might want to design simple tools to help create your game, or you might want to have a window-based front end to start up your game. And these tools will undoubtedly have menus—millions of them if you’re making a 3D tool. Trust me! In either case, you need to know how to create, load, and respond to menus.

#### Creating a Menu

You can create an entire menu and all the associated files with the compiler’s menu editor, but we’ll do it manually because I can’t be sure which compiler you’re using. This way you’ll learn what’s in a menu description, too. But when you’re writing a real application and creating a menu, most of the time you’ll use the IDE editor because menus are just too complex to type in manually. It’s like HTML code—when the Web started, it wasn’t a big deal to make a home page with a text editor. Nowadays, it’s nearly impossible to create a Web site without using a tool. (Speaking of Web site design, my friend needs work at http://www.belmdesigngroup.com—he has 15 kids to feed!)

Anyway, let’s get started making menus! Menus are just like the other resources you have already worked with. They reside in an `.RC` resource script and must have an `.H` file to resolve any symbolic references, which are all IDs in the case of menus. (One exception: The name of the menu must be symbolic—no name strings.) Here’s the basic syntax of a `MENU` description as you would see it in an `.RC` file:

    MENU_NAME MENU DISCARDABLE

    { // you can use BEGIN instead of { if you wish

    // menu definitions

    } // you can use END instead of } if you wish

`MENU_NAME` can be a name string or a symbol, and the keyword `DISCARDABLE` is vestigial but necessary. Seems simple enough. Of course, the stuff in the middle is missing, but chill—I’m getting there!

Before I show you the code to define menu items and submenus, we need to get some terminology straight. For my little discussion, refer to the menu in Figure 3.9. It has two top-level menus, File and Help. The File menu contains four menu items: Open, Close, Save, and Exit. The Help menu contains only one menu item: About. So there are top-level menus and menu items within them. However, this is misleading because it’s possible to also have menus within menus, or cascading menus. I’m not going to create any cascading menus, but the theory is simple: You just use a menu definition for one of the menu items itself. You can do this recursively, ad infinitum.

Figure 3.9 A menu bar with two submenus.

Now that we have the terminology straight, here’s how you would implement the menu shown in Figure 3.9:

    MainMenu MENU DISCARDABLE

    {

    POPUP "File"

        {

        MENUITEM "Open", MENU_FILE_ID_OPEN

        MENUITEM "Close", MENU_FILE_ID_CLOSE

        MENUITEM "Save", MENU_FILE_ID_SAVE

        MENUITEM "Exit", MENU_FILE_ID_EXIT

        } // end popup

    POPUP "Help"

        {

        MENUITEM "About", MENU_HELP_ABOUT

        } // end popup

    } // end top level menu

Let’s analyze the menu definition section by section. To begin with, the menu is named `MainMenu`. At this point we don’t know if it’s a name string or an ID, but since I usually capitalize all constants, it’s a safe bet that it’s a plain string. So that’s what we’ll make it. Moving on, there are two top-level menu definitions, beginning with the keyword `POPUP`—this is key. `POPUP` indicates that a menu is being defined with the following ASCII name and menu items.

The ASCII name must follow the keyword `POPUP` and be surrounded by quotes. The pop-up menu definition must be contained within `{ }` or a `BEGIN END` block—whichever you like. (You Pascal people should be happy <BG>.)

Within the definition block, follow all of the menu items. To define a menu item, you use the keyword `MENUITEM` with the following syntax:

    MENUITEM "name", MENU_ID

And that’s it! Of course, in this example you haven’t defined all the symbols, but you would do so in an `.H` file something like this:

    // defines for the top level menu FILE

    #define MENU_FILE_ID_OPEN               1000

    #define MENU_FILE_ID_CLOSE              1001

    #define MENU_FILE_ID_SAVE               1002

    #define MENU_FILE_ID_EXIT               1003

    // defines for the top level menu HELP

    #define MENU_HELP_ABOUT                 2000

> Tip: Notice the values of the IDs. I have selected to start off the first top-level menu at 1000 and increment by 1 for each item. Then I increment by 1000 for the next top-level menu. Thus, each top-level menu differs by 1000, and each menu item within a menu differs by 1. This is a good convention that works well. And it’s less filling.

I didn’t define `“MainMenu”` because I want to refer to the menu by string rather than ID. This isn’t the only way to do it. For example, if I put the single line of code

    #define MainMenu 100

within the `.H` file with the other symbols, the resource compiler would automatically assume that I wanted to refer to the menu by ID. I would have to use `MAKEINTRESOURCE(MainMenu)` or `MAKEINTRESOURCE(100)` to refer to the menu resource. Get it? Alrighty, then!

You’ll notice that many menu items have hotkeys or shortcuts that you can use instead of manually selecting the top-level menu or menu item with the mouse. This is achieved by using the ampersand character (`&`). All you do is place the ampersand in front of the character that you want to be a shortcut or hotkey in a `POPUP` menu or a `MENUITEM` string. For example,

           MENUITEM "E&xit", MENU_FILE_ID_EXIT

makes the `x` a hotkey, and

                      POPUP "&File"

makes `F` a shortcut via Alt+F.

Now that you know how to create and define a menu, let’s see how to load it into your application and attach it to a window.

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.