
Load and image (bitmap or icon, below we use a sample icon file, 16x16)Ĭonst CSize menuimgsize = CMFCToolBar::GetMenuImageSize() Get the size of the images in the menu image list (e.g. Int imgindex = GetCmdMgr()->GetCmdImage(cmdid, FALSE) ĬMFCToolBarImages * images = CMFCToolBar::GetMenuImages()

Check whether the command already have an associated index into the menu image list.
MFC SET ICON ON BUTTON CODE
Now, let me show you the code to add one extra image into the image list used for the menu item images: unsigned int cmdid =. IDR_MENUTOOLBAR_24x24x32 they will appear with a menu item image coming from either IDR_APP_TOOLBAR_16x16x32 or IDR_MENUTOOLBAR_16x16x32. This establishes the static/fixed part of the popup menu and if the ID_ITEM1 and ID_ITEM2 commands are mapped into any of the toolbar resources IDR_APP_TOOLBAR_24x24x32 or window CWnd wnd at position CPoint pos. Creates the popup and will display it on the screen relative to the parent Popup->InsertItem(CMFCToolBarMenuButton(ID_ITEM1, NULL, -1, EBC_TEXT("Item 2"))) Popup->InsertItem(CMFCToolBarMenuButton(ID_ITEM1, NULL, -1, EBC_TEXT("Item 1"))) With MFC you create a popup menu by allocating a CMFCPoupMenu andĬreate it, as illustrated below: CMFCPopupMenu* popup = new CMFCPopupMenu() // This will auto-delete Well at some point in time you want to create and show your context sensitive popup menu with dynamic created submenu. The static part of the context popup menu Note that none of these mightĬontain the menu item images that you really want to appear in your dynamic submenu of the context popup menu.

The resource id's IDR_APP_TOOLBAR_16x16x32 and IDR_MENUTOOLBAR_16x16x32 are however representing a corresponding bitmap image list with smaller images used for menus, i.e. Load menu item images (not placed on any standard toolbars):ĬMFCToolBar::AddToolBarForImageCollection(IDR_MENUTOOLBAR_24x24x32, IDR_MENUTOOLBAR_24x24x32,īoth the IDR_APP_TOOLBAR_24x24x32 and the IDR_MENUTOOLBAR_24x24x32 are resource id's for both a TOOLBAR resource as well for a corresponding bitmap representing the image list of the toolbar button images. | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) || If( !m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER and IDR_APP_TOOLBAR_16x16x32 used below.ĬMFCToolBar::SetMenuSizes( CSize( 24, 24 ), CSize( 16, 16 ) ) Note that these sizes should match the height found in respectively IDR_APP_TOOLBAR_24x24x32 Your code might look like something as illustrated below: // Setup that we want toolbar images in size 24x24 and menu bar images in size 16x16. "miraculous" itself ensure that all menu items, both in your main menu and in any context menus, are displayed with the appropriate menu item image. If you've created your application main menu and toolbar and loaded these with initial bitmaps representing the image listed for your toolbar buttons and corresponding menu buttons MFC will In the layout Item 1 and 2 are menu items for fixed commands where as Item- 3, A, B, 4 and separators are added dynamically (depending on the context). The layout of a sample possible context menu look like below: Item 1 Using MFC, especially the classes CMFCPopupMenu and CMFCToolBarMenuButton, this was not easy. That is, the submenus' items are created dynamically depending on the currentĬontext and I also wanted menu item images in those. So the application have a fixed set of commands for a context popup menu but a submenu of common dynamic nature. Hence the need for different menu item images depending on the actual allowed document types.

The context menuĬontains some static/fixed menu items (commands) and the submenu should contain only the menu items (create document commands) for the documents the user can create. The specific use case here is an application with an explorer like user interface where the user only can create specific kinds of new types of documents in different folders. Namely images in menu items appearing in a dynamic submenu to a context popup menu. While this now function well, there was just one small
MFC SET ICON ON BUTTON FULL
Recently I've spent considerable time to add updated - full RGB/A - bitmaps/icons to an applications' toolbar buttons and menu items (in newest MFC these are also buttons). And yes, MFC is still used -) Background and use case Least), I had hoped it would had been a somewhat easier. This article describes my solution to this. Submenu images in MFC Wednesday October 9th, 2013ĭo you have problems in creating a submenu with menu item images in a context popup menu in MFC ? Well, so had I.
MFC SET ICON ON BUTTON ARCHIVE
News Archive 2013 > Submenu Images in MFC |
