Using Image Lists in a Toolbar Control

OverviewSample

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 , , and .

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_TBarImages) and disabled (m_TBarDis) image list to a CToolBarCtrl object (m_wndToolBar).

CWinApp* pApp= AfxGetApp();

m_TBarImages.Create(16, 16, ILC_COLOR, 4, 4);
m_TBarImages.Add(pApp->LoadIcon(IDI_BLK));
m_TBarImages.Add(pApp->LoadIcon(IDI_RED));
m_TBarImages.Add(pApp->LoadIcon(IDI_YELL));
m_TBarImages.Add(pApp->LoadIcon(IDI_WHI));

m_TBarDis.Create(16, 16, ILC_COLOR, 4, 4);
m_TBarDis.Add(pApp->LoadIcon(IDI_DIS_BLK));
m_TBarDis.Add(pApp->LoadIcon(IDI_DIS_RED));
m_TBarDis.Add(pApp->LoadIcon(IDI_DIS_YELL));
m_TBarDis.Add(pApp->LoadIcon(IDI_DIS_WHI));

m_wndToolBar.SetImageList(&m_TBarImages);
m_wndToolBar.SetDisabledImageList(&m_TBarDis);

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.

See Also   Windows Common Controls and MFC Classes