CCommandbar and AppendMenu with PPC2003 app in VS2005

Hi

I'm porting my PPC2003 application to VS2005. I've managed to make it
compile, but my dynamic menus no longer work. The menu I'm updating was in a
CCeCommandbar when compiled in eVC4++, but in VS2005 I'm using CCommandbar.
What I do is use AppendMenu to add items to my menu. I do a printout of the
items in the menu after appending them, so I know they are there, but they
never become visible. I've tried DrawMenubar but it doesn't make a
difference.


Answer this question

CCommandbar and AppendMenu with PPC2003 app in VS2005

  • Robertwang.tw

    Hi,

    Can you please try the following code to get the menu handle for the menubar:

    HWND hMenuMB = (HWND)SHFindMenuBar(this->m_hWnd);

    HMENU hMenu = (HMENU) ::SendMessage(hMenuMB, SHCMBM_GETSUBMENU,0,IDM_MENU);

    CMenu *submenu = CMenu::FromHandle(hMenu);

    Please have a look at the answer post on http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=147307&SiteID=1, I suspect this is the same issue.

    Hope this helps

    Thanks



  • BilalShahzad

    Hi,

    CCommandBar::GetMenu method doesn't give you the actual menu used in the commandbar's toolbar. In fact is give you the menu that exists as a resource in you binary, that is why chaging it doesn't change the way menu gets displayed. Although the same menu gets displayed on non PPC and samrtphone devices.

    Here is the code that worked for me. If you aren't using smartphone style menus, then certainly you are using the traditional style menus. The code snippet in my last post works just fine on WM 5.0 PPC. But on PPC 2003 we have to change the implementationa bit since TBIF_BYINDEX isn't supported. So here is what is needed to obtain the command of the menu button.

    On WM 5.0

    TBBUTTONINFO tbbi = {0};
    tbbi.cbSize = sizeof(tbbi);

    tbbi.dwMask = TBIF_LPARAM | TBIF_BYINDEX;

    ::SendMessageW(m_wndCommandBar.m_hCommandBar, TB_GETBUTTONINFO,0, (LPARAM)&tbbi);

    HMENU hMenu = (HMENU)tbbi.lParam;

    On PPC 2003: ( should work for WM 5.0 too)
    /* get the command id first */
    TBBUTTON tbb;
    ::SendMessageW(m_wndCommandBar.m_hCommandBar, TB_GETBUTTON,0,(LONG) (LPARAM)&tbb);

    TBBUTTONINFO tbbi = {0};
    tbbi.cbSize = sizeof(tbbi);

    tbbi.dwMask = TBIF_LPARAM | TBIF_COMMAND;

    ::SendMessageW(m_wndCommandBar.m_hCommandBar,TB_GETBUTTONINFO,
    tbb.idCommand, (LPARAM)&tbbi);

    HMENU hMenu = (HMENU)tbbi.lParam;

    Hope this helps.


  • Khan

    MainFrame:

    if(m_wndCommandBar.Create(this) &&

    m_wndCommandBar.InsertMenuBar(IDR_MAINFRAME_NOR)

    && m_wndCommandBar.LoadToolBar(IDR_MAINFRAME))

    {

    m_wndCommandBar.SetBarStyle(m_wndCommandBar.GetBarStyle() |

    CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_FIXED);

    m_ActiveToolbar=IDR_MAINFRAME;

    }

    InsertMenubar + InsertToolbar to load the resourses.

     

    One of my toolbars:

    IDR_TB_ORDERNUM1_BIG TOOLBAR  18, 17
    BEGIN
        BUTTON      ID_F2_Or
        BUTTON      ID_F3_Tr
        BUTTON      ID_F4_Pa
        BUTTON      ID_ADD
        BUTTON      ID_SUBTRACT
        BUTTON      ID_MENU_TYPE
        BUTTON      ID_TOTAL_MENU
    END

     

    One of my menubars:

    IDR_MENUBAR_ORDER_ENG MENU DISCARDABLE
    BEGIN
        POPUP "More"
        BEGIN
            POPUP "List Box Layout"
            BEGIN
                MENUITEM "Report List",                 ID_REPORT
                MENUITEM "Big Buttons",                 ID_BIG_ICONS
                MENUITEM "Small Buttons",               ID_SMALL_ICONS
            END
            MENUITEM SEPARATOR
            MENUITEM "Print trial check",           ID_TRIAL_CHECK, GRAYED
            MENUITEM "Divide orders on table into checks", ID_DIVIDE_TABLE, GRAYED
            MENUITEM SEPARATOR
            MENUITEM "Total Menu",                  ID_TOTAL_MENU
            MENUITEM "Numerical Menu Choice Input", ID_MORE_NUMERICALINPUT
            MENUITEM SEPARATOR
            MENUITEM "Take Away",                   ID_TAKE_AWAY
            MENUITEM "Credit Note",                 ID_CREDIT_NOTE
            MENUITEM "Change Menu Type",            ID_CHANGE_MENUTYPE
            MENUITEM SEPARATOR
            MENUITEM "Start-Finish(SF)",            ID_F1_SF
        END
    END

     

    Appending a popup after menuitem Start-Finish(SF) is the problem. I run append, and the new item is there when I enumerate the items afterwards, but it is not visible. (I also had to use AddBitmap() to make the toolbar visible, which was not neccecary in eVC4+, but that is another issue)

    Appending code:

    //if(CMenu* mmenu = pFrm->GetMenu())//Tools (worked in EVC4++)

    if(HMENU hmenu = pFrm->m_wndCommandBar.GetMenu())//More (VS2005)

    {

    CMenu *mmenu = CMenu::FromHandle(hmenu);

    CMenu *smenu;

    //DEBUG

    //if(smenu = mmenu->GetSubMenu(0))//the main menu

    //{

    //int count = smenu->GetMenuItemCount();

    //for (int i = 0; i < count; i++)

    //{

    // CString str;

    // smenu->GetMenuString(i, str, MF_BYPOSITION);

    // AfxMessageBox(CString(str));

    //}

    //}

    smenu = mmenu->GetSubMenu(0);

    HMENU MFMenu = CreatePopupMenu();

    AppendMenu(MFMenu, MF_STRING | MF_ENABLED, (UINT)countMF, name);

    smenu->AppendMenu(MF_STRING | MF_ENABLED | MF_POPUP, (UINT)MFMenu, L"Multi Functions(MF)");

     

    Have tried with various versions of DrawMenuBar, but it makes no difference.


  • ShaktiSingh

    I will try out your code and get back to you ASAP. I assume this you are using this with MFC 8.0 Doc/View acrchitecture and that the command bar is associated with the framewindow.

    Thanks

    Avinash



  • BlisteringIce

    Hi,

    Can you please post the code snippets used to access the menu bar and the resources that you are using

    Thanks

    Avinash.



  • L. Gordon Curtis

    Allmost works.

    My Menu:

    ->List Box Layout -> Report List

    -> Big Buttons

    -> Small Buttons

    ->Print Trial Check

    ->Divide orders on table into checks

    ->Total Menu

    ->Numerical Menu Choice input

    ->Take Away

    ->Credit Note

    ->Change Menu Type

    ->SF

    More

    With the code you suggested, my new popups were added under Listbox Layout after small buttons, but I want them after SF. I assum this will be quite easy to fix. Thanks so much for the help. Just out of curiosity, do you know why my code worked when compiling for Pocket PC WM2003 using eVC4, but not when using VS2005 for the same platform


  • aspx

    I have a rc2 file, but my menus are as shown above. From what I have seen of SHMENUBAR examples on this forum, I do not think thats what I'm using. If the example above is Traditional menus, I'm definitely not using that in my code. But I'll give it a try. Right now I'm willing to try almost anything. It's so frustrating to have code that used to work, and suddenly you have to invent the wheel all over again.

    This is ok, or the other way around. I have several toolbar and menu resources. The one that is called IDR_MENUBAR_ORDER_ENG is the one that I'm actually trying to append something on. From my debug you can see that I've been able to find More and print out all the elements + new elements added after Start/Finish - SF.

    IDR_MAINFRAME >IDR_TB_ORDERNUM1_BIG

    IDR_MENUBAR_ORDER_ENG > IDR_MAINFRAME_NOR


  • OldDaemon

    Hi,

    Did it solve the porblem



  • Nyro

    Hi,

    The change has been brought about to support smartphone style menus with PPC plaform ( we talked about SHMENUBAR resource that gets used for these menu above). To support these the new CCommandBar class adds the commandbar with a call to SHCreateMenuBar instead of adding the menus to individual button with TB_INSERTBUTTON, as it was done in CCeCommandBar class's InsertMenubar member. A quick look at the sources shall tell you all.

    Now as far as the question of new menu appearing in the first menu is concerned, we need to understand that the 3rd parameter here is the button number and thus at the end of the snippet above we land up with the handle to List box layout menu as it gets associated with the button number zero. This is the reason that inserting the new item at the end of this menu makes it appear after small buttons.

    ::SendMessageW(m_wndCommandBar.m_hCommandBar, TB_GETBUTTON,button_index,(LONG) (LPARAM)&tbb);

    In your case to add another menu at the end we need to insert another button here, with the corresponding menu attached to it.

    I shall always recommend the smartphone style menus with just two menu since it makes operation with one hand possible using the hardware buttons on WM 5.0 platfrom.

    Thanks



  • Lowkus

    I have tried this, but it is even less sucessfull than my original code. hMenu is NULL with the above code. With my code I get the menu I'm looking for.....see debug in my code. And I'm able to append items. My problem is that they are not visible! Any idea My code worked perfectly for eVC++ for PPC2003, so whats new in VS2005 for PPC2003 that makes my code unusable

    By the way, I run through the appending code twice when doing debug, that is why I know that I get the new item into the menu. First run I get the items you see in my resource example. Secound run I also get my appended popup. But it will not show.


  • Yole

    HI,

    Some more questions are you using SHMNEUBAR resource ( located in rc2 file ), or the traditional style menus, similar to what you have provided above. One more thing the resources that you have provided aren't the same as theones you have used in the creation. I shall assume they are the same and change them as followed:

    IDR_MAINFRAME >IDR_TB_ORDERNUM1_BIG

    IDR_MENUBAR_ORDER_ENG > IDR_MAINFRAME_NOR

    In case you are using tradiional style menus you can also try code such as given below to modify the menus

    TBBUTTONINFO tbbi = {0};

    tbbi.cbSize = sizeof(tbbi);

    tbbi.dwMask = TBIF_LPARAM | TBIF_BYINDEX;

    ::SendMessageW(commandbar_ptr->m_hCommandBar, TB_GETBUTTONINFO,0, (LPARAM)&tbbi);

    HMENU hMenu = (HMENU)tbbi.lParam;

    I shall get back to you, but in the mean time you can try using the code given above to get the menu handle.

    Thanks

    Avinash



  • GOAT v2b

    I found that "On WM 5.0" code worked.

    The other item that I am having a problem with is dynamically disabling toolbar buttons on a CCommandBar (attached to a CDialog). I am wondering if there is a similar "trick" that I have to use to access the "live" CToolBarCtrl in the CCommandBar. What I have tried (and does not work) is the following:

    Code Snippet

    void EnableCommandButton(int iButtonInddex, BOOL bEnable)

    {

    UINT nState = 0;

    if (bEnable) {

    nState = TBSTATE_ENABLED;

    }

    m_CommandBar.GetToolBarCtrl().SetState(iButtonIndex, nState);

    }

    What I have been doing instead is destroy the existing CCommandBar and recreate a new CCommandBar and use the following:

    Code Snippet

    m_CommandBar.GetToolBarCtrl().AddButtons(2, m_tbToolBar);

    My app will generally have a menu and a Cancel button or two buttons.

    Thanks, Brad


  • gokaraju

    I'm not sure about the MFC version, but the rest of it is correct. This is pFrm in my code:

    CMainFrame * pFrm = (CMainFrame *)AfxGetMainWnd();


  • CCommandbar and AppendMenu with PPC2003 app in VS2005