Mosaic: Adding a menu

Our program should have a menu to set some options. Firstly we need to create our menu in the resource file:

5.1 - Creating a menu

A menu is fairly easy to make in your resource file. If you take a look at the resource text below you will see it's self explaining. Popup menus are menus that have submenus, a menuitem is just a menuitem with an associated ID. These IDs will be defined and can be used in your program to find out which menu item the user clicked on. Finally, the ampersands (&) will underline the character the precede, indicating a shortkey can be used.

Add this below the ICON definitions:

MAINMENU    MENU DISCARDABLE
BEGIN
    POPUP   "&File"
    BEGIN
        MENUITEM "&New Game", MI_NEWGAME
        MENUITEM "&Open Bitmap ", MI_OPENBITMAP
        POPUP "&Difficulty"
        BEGIN
            MENUITEM "&Easy", MI_EASY
            MENUITEM "&Medium", MI_MEDIUM
            MENUITEM "&Hard", MI_HARD
        END
    END
    POPUP "&Picture"
    BEGIN
        MENUITEM "Use &standard picture", MI_USESTANDARD
        MENUITEM "Use &numbers", MI_USENUMBERS
        MENUITEM "Use &bitmap file", MI_USEFILE
    END
    POPUP "&Colors"
    BEGIN
        MENUITEM "&Blue color scheme", MI_COLORBLUE
        MENUITEM "&Red color scheme", MI_COLORRED
        MENUITEM "&Green color scheme", MI_COLORGREEN
        MENUITEM "&Ugly color scheme", MI_COLORUGLY
    END
    POPUP "&Help"
    BEGIN
        MENUITEM "&About", MI_ABOUT
    END
END

Add this below the icon defines (before the code above!):

#define         MAINMENU            301

#define         MI_NEWGAME          101
#define         MI_OPENBITMAP       102
#define         MI_EASY             103
#define         MI_MEDIUM           104
#define         MI_HARD             105
#define         MI_USESTANDARD      106
#define         MI_USENUMBERS       107
#define         MI_USEFILE          108
#define         MI_COLORBLUE        109
#define         MI_COLORRED         110
#define         MI_COLORGREEN       111
#define         MI_COLORUGLY        112
#define         MI_ABOUT            113

5.2 - Defining the IDs

Now you'll have to convert the C definitions to asm equates:

MAINMENU        equ 301

MI_NEWGAME      equ 101
MI_OPENBITMAP   equ 102
MI_EASY         equ 103
MI_MEDIUM       equ 104
MI_HARD         equ 105
MI_USESTANDARD  equ 106
MI_USENUMBERS   equ 107
MI_USEFILE      equ 108
MI_COLORBLUE    equ 109
MI_COLORRED     equ 110
MI_COLORGREEN   equ 111
MI_COLORUGLY    equ 112
MI_ABOUT        equ 113

When you've converted them, place them in mosaic.inc.

The resource file and mosaic inc should now look like this: res1.zip

5.3 - Adding the menu to your window

[in your .data? section]

hMenu dd ?

[in your .code section]

invoke  RegisterClassEx, addr wc      ;AFTER THIS LINE!
invoke  LoadMenu, hInstance, MAINMENU ;load menu
mov     hMenu, eax                    ;store handle
INVOKE  CreateWindowEx,NULL,ADDR ClassName,ADDR AppName,\
        WS_OVERLAPPEDWINDOW-WS_MAXIMIZEBOX-WS_SIZEBOX,\
        CW_USEDEFAULT, CW_USEDEFAULT,258,350,NULL,hMenu,\ ;!!!!
        hInst,NULL
        ;NOTE: notice addition of the hMenu parameter
        ;      in the CreateWindowEx call.

The hMenu is added to the uninitialized data section to hold the menu handle when it's loaded. LoadMenu will load a menu resource from the instance hInstance (mosaic.exe) with a certain ID (MAINMENU). The handle is stored with 'mov hMenu, eax', then the handle is passed to CreateWindowEx to create a window with a menu. After you have run make.bat you should have a main window with a menu (that does nothing):

Menu on window Difficulty menu

Your source file should look like this now: mosaic2.txt