How to make the toolbar display a 256-color image

Source: Internet
Author: User
Tags resource

Problem:

Many people in the forum suggest that windows-based programs such as Resource Manager (Explorer.exe), ie, and so on can display beautiful toolbar images and icons. However, applications developed with MFC typically display only 16-color toolbar images and list view (such as CListView) images, and cannot display the 256-color icons and bitmaps created in the resource. Why is that?

Answer:

Both toolbars and list views store their own images in the image list. This image list is actually an image list. It is a long strip of bitmap image made up of many small images. As shown in Figure one:

Figure I

Assuming you have 7 20x20 icons, they will be saved as a 140x20 bitmap (7x20=140) in the image list. You can adjust the color characteristics of this bitmap as needed, but you must indicate how many colors to use when creating an image manifest. is 16 colors by default. In addition, an internal function that is used when MFC loads a toolbar bitmap, Afxloadsyscolorbitmap, also assumes a color of 16 colors. So when programming with MFC, in order to display 256-color images, you have to deal with the image list.

I wrote a demo program Tbcolor to show you how to display 256-color images in a toolbar, which is an extremely typical MFC program-it has a nice toolbar. As shown in Figure II:

Figure II

Pressing each button on the toolbar pops up the About dialog box. The details of the processing 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::P Recreatewindow (createstruct& CS)
{
Cs.style |= Ws_clipchildren;
return CFrameWnd::P Recreatewindow (CS);
}
int CMainFrame::OnCreate (lpcreatestruct lpcreatestruct)
{
VERIFY (Cframewnd::oncreate (lpcreatestruct) ==0);
  
Create and load a toolbar
//
VERIFY (M_wndtoolbar.create (this));
VERIFY (M_wndtoolbar.loadtoolbar (idr_mainframe));
Load Toolbar bitmap-must use:: LoadImage map 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 manifest and set the toolbar
256-color images 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,0), this, Afx_idw_pane_first));
return 0;
}

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.