How to display 256-color images on toolbar

Source: Internet
Author: User
How to display 256-color images on toolbar
Compilation: hangwire

Download sample source code

Problem:

Many people in the Forum suggested that Windows-basedProgramSuch as the resource manager (assumer.exe) and IE can display beautiful toolbar images and icons. However, it is developed using MFC
Generally, only 16-Color toolbar images and list views (such as clistview) images can be displayed, but the 256-color icons and bitmaps created in resources cannot be displayed. Why?
Answer:
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
Saved as a 140x20 Bitmap (7x20 = ). 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. In the default
The color is 16. In addition, when MFC loads the toolbar bitmap, an internal function, afxloadsyscolorbitmap, is assumed that the color is 16 colors. Therefore, use MFC to compile
To display a 256-color image, you must process the image list.

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"

Begin_message_map (cmainframe, cframewnd)
On_wm_create ()
End_message_map ()

Static uint indicators [] = {
Id_separator, // status line indicator
Id_indicator_caps,
Id_indicator_num,
Id_indicator_scrl,
};

Cmainframe: cmainframe ()
{
}

Cmainframe ::~ Cmainframe ()
{
}

Bool cmainframe: precreatewindow (createstruct & CS)
{
CS. Style | = ws_clipchildren;
Return cframewnd: precreatewindow (CS );
}

Int cmainframe: oncreate (maid)
{
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-must use: LoadImage ing color
// Map (192,192,192) to color_3dface.
//
Hbitmap HBM = (hbitmap): LoadImage (AfxGetInstanceHandle (),
Makeintresource (idr_mainframe ),
Image_bitmap,
0, 0, // CX, Cy
Lr_createdibsection | lr_loadmap3dcolors );
Cbitmap bm;
BM. Attach (HBM );

// Create an image list and set the toolbar
// 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 (0, 0, 0), this, afx_idw_pane_first ));

Return 0;
}
The first step must be: LoadImage to load the toolbar bit
Graph. 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, in the demo program
Toolbar Bitmap (figure 1) uses RGB (192,192,192) as the background. If the system has a 3D Color, The lr_loadmap3dcolors flag will
LoadImage 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, Cy
Lr_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 cmainframe member, and ilc_color8 is a magic symbol, it creates an 8-bit color palette bitmap, which is a 256-color bitmap. If
you want to, it can even create a 24-Bit Bitmap-but remember that lr_loadmap3dcolors can only be used with the color palette bitmap. The last step is to use the newly created image list on the
toolbar. This step is important and never forget it!

m_wndtoolbar.gettoolbarctrl (). setimagelist (& m_iltoolbar);

it is easy to use a 256-color image in the toolbar. This technology can also be used to control images in the List View, rebars, and other image lists. Give it a try!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.