Fun with menus!

I received a comment on one of my previous posts, correcting me on what I had mentioned. The menu bar resource was defined as below:



IDR_MENU SHMENUBAR DISCARDABLE

BEGIN

    IDR_MENU,

    2,



    I_IMAGENONE, IDM_OK, TBSTATE_ENABLED, TBSTYLE_BUTTON | TBSTYLE_AUTOSIZE,

    IDS_OK, 0, NOMENU,

   

    I_IMAGENONE, IDM_HELP, TBSTATE_ENABLED, TBSTYLE_DROPDOWN | TBSTYLE_AUTOSIZE,

    IDS_HELP, 0, 0,

END

And I had said, "Everything in between BEGIN and END defines the menu bar. The first line under BEGIN should be the same as the menu bar identifier, IDR_MENU". Well, I was wrong there. The first line under BEGIN could be same as the menu bar identifier but it need not be. Thanks Binary for noticing that! The first line under BEGIN is the identifier of the popup menu that appears when the user clicks on one of the popup menus.  If both the softkeys in your menu bar are buttons then the popup menu identifier could be specified as zero. So the correct way of doing it would be to define the menu bar as:

IDR_MENU SHMENUBAR DISCARDABLE

BEGIN

    IDR_MENU_POPUP,

    2,



    I_IMAGENONE, IDM_OK, TBSTATE_ENABLED, TBSTYLE_BUTTON | TBSTYLE_AUTOSIZE,

    IDS_OK, 0, NOMENU,

   

    I_IMAGENONE, IDM_HELP, TBSTATE_ENABLED, TBSTYLE_DROPDOWN | TBSTYLE_AUTOSIZE,

    IDS_HELP, 0, 0,

END

and IDR_MENU_POPUP is defined as a popup menu,

IDR_MENU_POPUP MENU

BEGIN

    POPUP ""

    BEGIN

        MENUITEM "System Metric",               IDM_SYSTEM_METRIC

        MENUITEM "About",                              IDM_HELP_ABOUT

    END

END

In the above menu bar, the left softkey is an "OK" button and right softkey is a popup menu with "System Metric" and "About" as its menu items. Just remember that IDR_MENU is the identifier of the softkey bar which is created and IDR_MENU_POPUP is the identifier of the popup menu, which appears when the user clicks on the right softkey "Menu"

 

Binary also raised another interesting point. How do we get popup menus on both right softkey and left softkey? Of course, the first thing I did was to remove the NOMENU option from the part defining the left softkey. You can see the problem there can’t you? Both left softkey and right softkey become popup menus, but both the popup menus show the same menu items because we specified a single popup menu identifier, IDR_MENU_POPUP, and that is why the same popup menu shows up for both the softkeys.

I was looking into how to get different popup menus for the 2 softkeys when Mr (or Ms) Binary replied again. Binary had figured out how.

 

You define the popup menu as usual but with two popup entries,

IDR_MENU_POPUP MENU

BEGIN

    POPUP "Menu L"

    BEGIN

        MENUITEM "Menu Item1",                        0

        MENUITEM "Menu Item2",                        0

    END



    POPUP "Menu R"

    BEGIN

        MENUITEM "System Metric",               IDM_SYSTEM_METRIC

        MENUITEM "About",                              IDM_HELP_ABOUT

    END

END
 

and while creating the menubar in WM_CREATE message, specify the following options,

        case WM_CREATE:

            SHMENUBARINFO mbi;



            memset(&mbi, 0, sizeof(SHMENUBARINFO));

            mbi.cbSize     = sizeof(SHMENUBARINFO);

            mbi.hwndParent = hWnd;

            mbi.nToolBarId = IDR_MENU_POPUP;

            mbi.hInstRes   = g_hInst;

            mbi.dwFlags = SHCMBF_HMENU;



            if (!SHCreateMenuBar(&mbi))

            {

                g_hWndMenuBar = NULL;

            }

            else

            {

                g_hWndMenuBar = mbi.hwndMB;

            }

The only difference is we specify the popup menu identifier for nToolBarId member instead of the menu bar identifier and we also set the SHCMBF_HMENU flag. When SHCMBF_HMENU flag is not specified then the nToolBarId value is treated as a toolbar identifer which creates our usual softkey bar with left and right softkeys. If the flag is set then nToolBarId is treated as a menu identifier. Just a regular menu bar with menus and submenus. So now when you run the application you get,

Be sure to specify the message value if you want to handle these menu clicks. I have specified zero above because I am not really interested in handling these items.

 

So now a question pops up (pun intended), what if I have several popup menus and submenus in the popup identifier. Will it take only the first two and display them or will it do something strange? See for yourself, I used the following popup menu just for the sake of it,

IDR_MENU_POPUP MENU

BEGIN



    POPUP "File"

    BEGIN

        MENUITEM "&Open",               0

        MENUITEM "&Save",                0

        MENUITEM "E&xit",                  0

    END



    POPUP "Edit"

    BEGIN

        MENUITEM "&Copy",               0

        MENUITEM "C&ut",                 0

        MENUITEM "&Paste",              0

    END



    POPUP "View"

    BEGIN

        MENUITEM "&Full Screen",       0

        MENUITEM "&Options",            0

    END



    POPUP "Help"

    BEGIN

        MENUITEM "Abou&t",              0

        MENUITEM "He&lp",                0

    END



END



here’s what is does,

and,

I have never seen that on a ppc! I was surprised that it actually works and creates a full fledged menu bar with several menus. And notice the SIP button has moved to the right. The color of the menu bar has changed as well. A pretty interesting find, I would say.

Update:
You might also be interested in this post, Fun with menus – part II

Leave a Reply

Your email address will not be published. Required fields are marked *