Click here to Skip to main content
Click here to Skip to main content

Disabling top-level popup menus in the PocketPC 2002

By , 19 Mar 2003
Rate this:
Please Sign up or sign in to vote.

Sample Image - CeMultiBarMenu.jpg

Introduction

This article shows a technique to disable top-level popup menus embedded in the Command Bar of PocketPC 2002 applications built with MFC. This is the result of my continuing discussions with John Simmons / outlaw programmer about the "ease of use" of the PocketPC 2002 User Interface API under MFC. On our last take at these issues, we found that disabling top-level popup menus is not a trivial task. Here is one solution.

Top-level popup menus

They are not popup menus. They are fakes. They are toolbar buttons! I reached this conclusion by looking at how CCeCommandBar::LoadToolBar is implemented. The menu resource you supply is broken into its popups, and a text toolbar button is created for each one of them. So it is useless to try and use CMenu methods to enable or disable these, you have to manipulate the underlying buttons.

Popup menu buttons are created with standard IDs starting with 0xF000 for the first one, and are incremented by 1 for the next ones. So, in order to know what button you want to access, you have to know its ordinal position. If you are using the "shared new" button, 0xF000 will be the first one to the right of it.

The solution

My first approach was to use the TB_ENABLEBUTTON message. It seemed to work until John Simmons noticed that some of the button's texts were being truncated when grayed. In fact, when the toolbar disables a text button, it paints it in gray with a white shadow, making the text one pixel wider. Needless to say that the rectangle that holds this text is not changed and the text is clipped...

The next approach was based on the GetButtonInfo and SetButtonInfo methods of the CToolBarCtrl that supports all toolbars. With these, I set a number of properties of the TBBUTTONINFO structure, namely fsState and cx. The first one is used to set the button state (enabled or disabled), where the second is used to increase the button size when it is disabled, and to decrease it back when it is enabled.

I encapsulated the solution in the CCeCommandBarEx class that has only one method (besides standard constructor and destructor): EnablePopupMenu. The implementation is as follows:

BOOL CCeCommandBarEx::EnablePopupMenu(int iIndex, BOOL bEnable)
{
    CToolBarCtrl&    rToolBar = GetToolBarCtrl();
    TBBUTTONINFO    tbi;
    int                nID = 0xf000 + iIndex;
    CSize            sizeImage(16,15),
                    sizeButton(23, 21);
    BOOL            bRval;

    tbi.cbSize        = sizeof(tbi);
    tbi.dwMask        = TBIF_STATE | TBIF_SIZE | TBIF_COMMAND;
    tbi.idCommand    = nID;
    bRval            = rToolBar.GetButtonInfo(nID, &tbi);
    if(bRval)
    {
        if(tbi.fsState & TBSTATE_ENABLED)
            tbi.cx += bEnable ? 0 : 1;
        else
            tbi.cx -= bEnable ? 1 : 0;

        if(bEnable)
            tbi.fsState |= TBSTATE_ENABLED;
        else
            tbi.fsState &= ~TBSTATE_ENABLED;

        rToolBar.SetButtonInfo(nID, &tbi);
        SetSizes(sizeButton, sizeImage);
    }

    return bRval;
}

Note that I have to call SetSizes to avoid an incomprehensible bug I found: when increasing the popup menu button size, all other buttons would change their sizes as well.

The demo project

You will find that the demo project is an adaptation of the same project I used in my other article Multiple Toolbars in the PocketPC 2002 , but with some changes namely:

  • Tool tips on all toolbars
  • Toolbars are now exclusively shown

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

João Paulo Figueira
Software Developer Frotcom International
Portugal Portugal
I work on R&D for Frotcom International, a company that develops web-based fleet management solutions.
Follow on   Twitter   LinkedIn

Comments and Discussions

 
Generaledit control in the toolbar PinmemberShabrawy91125-Aug-05 6:21 
GeneralRe: edit control in the toolbar PinmemberJoão Paulo Figueira25-Aug-05 6:32 
GeneralRe: edit control in the toolbar PinmemberShabrawy91126-Aug-05 2:17 
GeneralRe: edit control in the toolbar PinmemberJoão Paulo Figueira26-Aug-05 3:00 
GeneralRe: edit control in the toolbar PinmemberShabrawy91127-Aug-05 5:14 
GeneralRe: edit control in the toolbar PinmemberShabrawy91127-Aug-05 6:29 
GeneralExcellent! PinsussJac Goudsmit13-Nov-03 16:54 
GeneralHi It's great. I want to increase the height of the command bar PinmemberK.Senthil Kumar12-Nov-03 19:24 
GeneralRe: Hi It's great. I want to increase the height of the command bar PinmemberJoão Paulo Figueira13-Nov-03 6:24 
GeneralRe: Hi It's great. I want to increase the height of the command bar PinmemberCarmen_Panturu18-Apr-04 20:07 
GeneralDisable toolbar button in PocketPC 2002 Pinmemberannamammen1-Oct-03 22:00 
GeneralRe: Disable toolbar button in PocketPC 2002 PinmemberJoão Paulo Figueira1-Oct-03 22:29 
GeneralGreat work! PinmemberDaniel S.20-Mar-03 6:15 
GeneralGood detective work... PinmemberCodin' Carlos20-Mar-03 5:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 20 Mar 2003
Article Copyright 2003 by João Paulo Figueira
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid