Both the toolbar and List View store their own images in the image list. This image list is actually an image list. It is a long bitmap image composed of many small images. 1: |
|
Figure 1 |
If you have 7 20x20 icons, they will be saved as a 140x20 Bitmap (7x20 = 140) in the image list ). You can adjust the color of the bitmap as needed. However, you must specify the number of colors to use when creating an image list. The default value is 16 colors. In addition, when MFC loads the toolbar bitmap, an internal function, AfxLoadSysColorBitmap, is assumed that the color is 16 colors. Therefore, when using MFC programming, you must process the image list to display a 256-color image. I wrote the DEMO program TBColor to show you how to display a 256-color image in the toolbar. This program is an extremely typical MFC Program-it has a beautiful toolbar. 2: |
|
Figure 2 |
Press each button in the toolbar to bring up the "about" dialog box. The specific processing details are all implemented in the CMainFrame: OnCreate function: |
MainFrm. cpp /////////////////////////////////////// /// // Set tabsize = 3 in your editor. // # include "StdAfx. h "# include" MainFrm. h "# include" Resource. h "outputs (CMainFrame, CFrameWnd) ON_WM_CREATE () END_MESSAGE_MAP () static UINT indicators [] = {ID_SEPARATOR, // status line indicator outputs, counts, counts,}; CMainFrame :: CMainFrame () {} CMainF Rame ::~ CMainFrame () {} BOOL CMainFrame: PreCreateWindow (CREATESTRUCT & cs) {cs. style | = WS_CLIPCHILDREN; return CFrameWnd: PreCreateWindow (cs);} int CMainFrame: OnCreate (LPCREATESTRUCT failed) {VERIFY (CFrameWnd: OnCreate (lpCreateStruct) = 0 ); // create and load the toolbar // VERIFY (m_wndToolBar.Create (this); VERIFY (m_wndToolBar.LoadToolBar (IDR_MAINFRAME); // load the toolbar Bitmap-required :: loadImage ing color // map (192,192,192) to COLOR_3DF ACE. // HBITMAP hbm = (HBITMAP): LoadImage (loads (), MAKEINTRESOURCE (IDR_MAINFRAME), IMAGE_BITMAP, // cx, cy LR_CREATEDIBSECTION | LR_LOADMAP3DCOLORS); CBitmap bm; bm. attach (hbm); // create an image list and set the toolbar. // The 256-color image must use ILC_COLOR8! // M_ilToolBar.Create (20, 20, ILC_COLOR8, 4, 4); m_ilToolBar.Add (& bm, (CBitmap *) NULL); m_wndToolBar.GetToolBarCtrl (). setImageList (& m_ilToolBar); VERIFY (m_wndStatusBar.Create (this); VERIFY (m_wndStatusBar.SetIndicators (indicators, sizeof (indicators)/sizeof (UINT ))); VERIFY (m_wndView.Create (_ T ("Press a button, any button. "), WS_VISIBLE | WS_CHILD | SS_CENTERIMAGE | SS_CENTER, CRect (,), this, AFX_IDW_PANE_FIRST); return 0 ;} |
The first step must use: LoadImage to load the toolbar bitmap. This function is a multi-purpose function that can load the cursor, icon, and bitmap. When a bitmap is loaded, this function can even map some colors in the bitmap to the current screen color. For example, the toolbar bitmap in the DEMO program (figure 1) uses RGB (192,192,192) as the background. If the system has a 3D color, the LR_LOADMAP3DCOLORS flag maps the background to the actual 3D (GetSysColor (COLOR_3DFACE) color of the system. |
HBITMAP hbm = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME),IMAGE_BITMAP,0,0, // cx, cyLR_CREATEDIBSECTION | LR_LOADMAP3DCOLORS ); |
LoadImage returns a bitmap handle (HBITMAP), but MFC prefers to deal with CBitmaps, so the next step is to create a CBitmap object and Attach it to your handle. |
CBitmap bm; Bm. Attach (hbm );
Now with the CBitmap object, you can create a new image list from it.
M_ilToolBar.Create (20, 20, ILC_COLOR8, 4, 4 ); M_ilToolBar.Add (& bm, (CBitmap *) NULL ); |
M_ilToolBar is a member of CMainFrame, and ILC_COLOR8 is a magic sign. It creates an 8-bit color palette Bitmap-A 256-color bitmap. If you want to, it can even create a 24-bit color Bitmap-but remember that LR_LOADMAP3DCOLORS can only be used with the color palette bitmap. The last step is to apply the newly created image list to the toolbar. This step is important and never forget it!
M_wndToolBar.GetToolBarCtrl (). SetImageList (& m_ilToolBar );
Using a 256-color image in the toolbar is that simple. This technology can also be used to control images in the List View, rebars, and other image lists. Give it a try! |