Using Image Lists in a Toolbar Control
By default, the images used by the buttons in a toolbar control are stored as a single bitmap. However, you can also store button images in a set of image lists. The toolbar control object can use up to three separate image lists:
Enabled image list Contains images for toolbar buttons that are currently enabled.
Disabled image list Contains images for toolbar buttons that are currently disabled.
Highlighted image list Contains images for toolbar buttons that are currently highlighted. This image list is used only when the toolbar uses the TBSTYLE_FLAT style.
These image lists are used by the toolbar control when you associate them with the CToolBarCtrl
object. This association is accomplished by making calls to CToolBarCtrl::SetImageList, SetDisabledImageList, and SetHotImageList.
By default, MFC uses the CToolBar
class to implement MFC application toolbars. However, the GetToolBarCtrl
member function can be used to retrieve the embedded CToolBarCtrl
object. You can then make calls to CToolBarCtrl
member functions using the returned object.
The following example demonstrates this technique by assigning an enabled (m_ToolBarImages
) and disabled (m_ToolBarDisabledImages
) image list to a CToolBarCtrl
object (m_ToolBarCtrl
).
CWinApp* pApp = AfxGetApp();
m_ToolBarImages.Create(16, 16, ILC_COLOR, 4, 4);
m_ToolBarImages.Add(pApp->LoadIcon(IDI_BLK));
m_ToolBarImages.Add(pApp->LoadIcon(IDI_RED));
m_ToolBarImages.Add(pApp->LoadIcon(IDI_YELL));
m_ToolBarImages.Add(pApp->LoadIcon(IDI_WHI));
m_ToolBarDisabledImages.Create(16, 16, ILC_COLOR, 4, 4);
m_ToolBarDisabledImages.Add(pApp->LoadIcon(IDI_DIS_BLK));
m_ToolBarDisabledImages.Add(pApp->LoadIcon(IDI_DIS_RED));
m_ToolBarDisabledImages.Add(pApp->LoadIcon(IDI_DIS_YELL));
m_ToolBarDisabledImages.Add(pApp->LoadIcon(IDI_DIS_WHI));
m_ToolBarCtrl.SetImageList(&m_ToolBarImages);
m_ToolBarCtrl.SetDisabledImageList(&m_ToolBarDisabledImages);
Note
The image lists used by the toolbar object must be permanent objects. For this reason, they are commonly data members of an MFC class; in this example, the main frame window class.
Once the image lists are associated with the CToolBarCtrl
object, the framework automatically displays the proper button image.