visual-c++mfccmfctoolbar

MFC: What method is used to set the toolbar buttons to be a subset of the total toolbar buttons by default?


I want to add all menu items to the toolbar but only have a subset of them showing by default (without user having to customize). This way all menu items will have icons next to them, but the toolbar won't contain the seldom used items unless users adds them.

If possible, how is that done with CMFCToolBar?


Solution

  • If possible, how is that done with CMFCToolBar?

    You can define any number of 'dummy' toolbar resources in your program's resource script, like the below example, where ID_DUMMY1 is the toolbar/bitmap resource ID (must be available to both the resource compiler and the C++ compiler) and the three ID_COMMAND_x IDs define the menu commands that the images correspond to:

    ID_DUMMY1 BITMAP   L"DummyToolbar.bmp" // 16 x 48 bitmap for three 'buttons
    ID_DUMMY1 TOOLBAR  16, 16
    {
        BUTTON    ID_COMMAND_A // Three commands corresponding to the three button
        BUTTON    ID_COMMAND_B // images in the above bitmap
        BUTTON    ID_COMMAND_C
    }
    

    Then, in your program (typically, just after you have initialized your main frame window), you can call the static member of the CMFCToolBar class, AddToolBarForImageCollection to load the images from those dummy toolbars. The following will load those image in the above-defined resource:

        CMFCToolBar::AddToolBarForImageCollection(ID_DUMMY1, ID_DUMMY1);
    

    After this, every menu item with the given command(s) will show the associated image as defined in the dummy toolbar resource(s). You can have as many such toolbar resources as required, and just call the AddToolBarForImageCollection for each one.


    Note: Although I have used the same resource ID for both toolbar and bitmap resources, you can also use different IDs. So long as the arguments given in the call to AddToolBarForImageCollection are properly coordinated, the method will still work.