How to add submenu to a CMenu in MFC?

会有一股神秘感。 提交于 2021-02-07 10:30:41

问题


I have an MFC app that uses CMenu for the main menu bar.
I haven't been able to create submenus successfully.

I can have the first level of File, Edit, View, etc and their sub menus, but I can't create a submenu off of one of those menus.

For example, I would like to be able to go File->Recent Items->list of items in submenu
I can do this easily enough with the resource editor in VS, but this needs to be done dynamically.

Am I using the right class in CMenu? Any suggestions on what to try?
I haven't found any decent tutorials. Even pointing me towards the right one would be helpful.


回答1:


Use your resource editor to add a submenu containing one placeholder item. You can then programatically grab a reference to this submenu, add items to it and delete the placeholder item:

CMenu *subMenu = mainMenu.GetSubMenu( menuPosition );

if( subMenu )
{
    for( unsigned i = 0; i < stringArray.size(); i++ )
    {
        subMenu->AppendMenu( MF_STRING, 400 + i, stringArray[i]);
    }

    subMenu->DeleteMenu( ID_SUBMENU_PLACEHOLDER, MF_BYCOMMAND );
}



回答2:


I had to do the same thing today, I'm on VS2008 with the Feature Pack (new UI stuff), and was looking at the samples, and there's sample on how to add menu items dynamically (http://msdn.microsoft.com/en-us/library/bb983167.aspx)

You need to override the CFrameWndEx::OnShowPopupMenu method.

Max.



来源:https://stackoverflow.com/questions/1155989/how-to-add-submenu-to-a-cmenu-in-mfc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!