VC + + Implementation shows transparent 256 colors above the icon

Source: Internet
Author: User
Tags bool

In the course of learning Visual C + + programming, many friends may ask how Visual C + + ensures that more than 256-color icons are not distorted after loading. Although there are articles on how to achieve more than 256 colors, most of the methods used to load a 256-color toolbar bitmap. One of the problems with this approach is how to use a simple mapping tool to do such a bitmap? I believe readers want to have a more direct way to use more than 256 colors of the icon, and can be transparent to the effect of the display of the icon. This example introduces the method to achieve the above goal, and realizes a transparent 256-color bitmap on the tool bar.

One, the realization method

In general, if we encounter a problem that requires an icon to be used in a program, the first thing to think of is to use the image list Cimlagelist class, which is a collection of images or icons of the same size, each image or icon is represented by the index number "0", so it can effectively manage a large number of icons or bitmaps. The CImageList class is often used with a list control CListCtrl, a tree control CTreeCtrl, or a Label control CTabCtrl, which is used with the Ctoobbarctrl class in this instance.

The member function of the CImageList class create () Initializes an image list and attaches it to a CImageList object, which is a prototype:

BOOL Create (int cx,int cy,bool bmask,int ninitial,int ngrow);

The first two parameters in the function CX, CY Specify the width and height of the icon/image, that is, the size definition of the icon/image. The third parameter, Bmask, is the mask flag, which specifies how to display the icon/image. If the value equals ilc_ COLOR8 The description displays an icon/image in a 256-color palette. The equal value of "TRUE" indicates that the icon/image is displayed in a transparent manner. Then, if the two are "bitwise-or", the meaning becomes: to display the 256-color icon in a transparent manner. The fourth and fifth parameters in the function are represented as: the number of initial icons and the step of the object automatically requesting the memory space when the new icon is added. If in the bulk operation of the icon, and need to continue to delete the icon, set the fifth parameter can change the performance of the program, if the fifth parameter set more moderate can avoid the application and free memory space.

Once you have created the CImageList class, you also need to load each icon into the object of the class, and the implementation process can use Windows API function LoadImage () To load icon resources, and then use the CImageList class of Add (Hicon HICON) function adds the loaded chart to the CImageList class object. The function prototypes for LoadImage () are as follows:

HANDLE LoadImage(
   HINSTANCE hinst, // handle of the instance containing the image
   LPCTSTR lpszName, // name or identifier of image
   UINT uType, // type of image
   int cxDesired, // desired width
   int cyDesired, // desired height
   UINT fuLoad // load flags
  );

The above function is used to load an icon, image, or cursor resource, and if the call succeeds, the function returns the handle of the loaded resource, otherwise it returns "NULL." Where the parameter hinst is an application handle that contains an icon/image resource; Lpszname is the name of the resource that can be used to convert the resource ID using the Makeintresource () function; parameter Utype describes the type of the current resource, whether it is an icon, an image, or a cursor ; cxdesired and cydesired are the desired target dimensions; the last parameter is the Mount flag, which is typically set to "0" when the icon resource is processed.

Finally, you need to call the cimagelist* setimagelist (cimagelist* pimagelist) function of the CToolBarCtrl class to associate an icon list object with a Toolbar object to display the mounted icon on the toolbar.

Second, programming steps

1, the start of visual c++6.0, the establishment of a single document engineering (multiple files can also be) applications, named "TB";

2, in the main frame of the program CMainFrame class header definition of a CImageList object, the code is as follows: CImageList M_ILTB;

3, add 256 color in the application of the color icon, the specific method is to start the program icon Resource Editor, click on the resource editor of the "New Device Image" button, in the pop-up dialog box to select "Custom" on it, the instance program in the size of the icon 32* 32, the color is 256 color;

4, add code, compile and run the program;

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.