Image list control (cimagelist)

Source: Internet
Author: User

From http://yf8426.blog.sohu.com/132750747.html

The image list control (cimagelist) is a set of images of the same size. Each set uses 0 as the index sequence number base of the image. (These icons can be viewed as stored in arrays ), the image list is usually composed of large icons or bitmaps, which contain transparent bitmap mode (in fact, all the icons in another image list are stored in the bitmap of a screen device ). You can use Windows
API to draw, create, and delete images, and add, delete, replace, and drag images. The image list control provides basic methods for controlling the image list amount. These methods can be implemented in Windows 95 and later versions. This control is invisible and is usually used with other products such as clistbox, ccombobox, ccomboboxex, and ctabctrl to provide them with ICON resources.

I. Object Structure of image controls

1. Data Member of the image control

M_himagelist: control handle connecting image objects

2. How to create an image control

Cimagelist & imagelist: creates an image control object structure. Create initializes the image list and binds the object. The method for creating an image control is as follows:

Bool create (int cx, int cy, uint nflags, int ninitial, int ngrow );

Bool create (uint nbitmapid, int CX, int ngrow, colorref crmask );

Bool create (maid, int CX, int ngrow, colorref crmask );

Bool create (cimagelist & imagelist1, int nimage1, cimagelist & imagelist2, int nimage2, int dx, int Dy );

The parameter definitions are as follows: Cx defines the image width, in pixels; CY defines the Image Height, in pixels; nflags determines the type of image list creation, it can be a combination of the following values: ilc_color, ilc_color4, ilc_color8, ilc_color16, ilc_color24, ilc_color32, ilc_colorddb, and ils_mask; ninitial is used to determine the images contained in the image list; ngrow is used to determine the number of images that can be controlled by the image list.

Nbitmapid is used to determine the bitmap flag value associated with the image list; crmask indicates the color shielding bit; lpszbitmapid is used to determine the identifier string containing the bitmap resource; imagellist1 points to a pointer to the image list control object; number of images included in nimage1 image list 1; imagelist2 points to a pointer to the image list control object.

Two ways to add images

Generally, bitmap and ICON resources can be added to the cimagelist object. The resources can be resources in the source file of the program, or directly transferred from the external file.

(1) Add icons and bitmap images from the source files in the program as follows:

// Create a cimagelist class pointer variable

Cimagelist * pimagelist;

Pimagelist = new cimagelist ();

Pimagelist-> Create (32, 32, ilc_color32, 0, 4 );

 

// Add an icon that can be automatically stretched along with the size of the image list

Pimagelist-> Add (afxgetapp ()-> loadicon (idi_icon1 ));

Pimagelist-> Add (afxgetapp ()-> loadicon (idi_icon2 ));

 

// Add a bitmap and make the black color transparent. If the size of the image list is inconsistent with the bitmap size, the addition may fail in days.

Cbitmap BMP;

BMP. loadbitmap (idb_bitmap1)

Pimagelist-> Add (& BMP, RGB (0, 0, 0 ));

BMP. deleteobject ();

BMP. loadbitmap (idb_bitmap2)

Pimagelist-> Add (& BMP, RGB (0, 0, 0 ));

(2) adding icons and bitmap images from external files

Hbitmap;

Cbitmap * pbitmap;

Cstring filepathname = "E: \ time.bmp ";

Pbitmap = new cbitmap;

// Import bitmap from a file

Hbitmap = (hbitmap) LoadImage (AfxGetInstanceHandle (), filepathname, image_bitmap, 0, 0, lr_loadfromfile );

Pbitmap-> attach (hbitmap );

M_pimagelist-> Add (pbitmap, RGB (0, 0 ));

You can create an image list control and add an image. Then you can use the image in other controls. Generally, you need to call setimagelist (m_pimagelist) to set the current image list as a reference image source.

3. Attributes of image controls

The attribute classes of the image control include returning m_himagelist control handle getsafehandle, obtaining the number of images in the image list getimagecount, setting the background color setbkcolor of the image list, and obtaining the background color getbkcolor of the image list.

4. Image Control Operations

Image control operations include binding an image list to an object attach, unbinding the image list on the object, returning the handle detach, deleting an image list deleteimagelist, and adding an image add or remove an image from the image list.

Another method for loading BMP bitmap is:

Cimagelist * m_pimagelist = new cimagelist ();

Hbitmap;

Cbitmap * pbitmap;

Cstring strfilename;

Strfilename. Format ("% s \ res \ root.bmp", szpath );

Hbitmap = (hbitmap): LoadImage (: AfxGetInstanceHandle (), (lpctstr) strfilename, image_bitmap, 0, lr_loadfromfile );

Pbitmap = new cbitmap;

Pbitmap-> attach (hbitmap );

M_pimagelist-> Add (pbitmap, RGB (0, 0 ));

Delete pbitmap;

2. Application Skills of image controls

Image controls cannot be used independently. They must be used together with List controls, tree controls, and label controls. The following describes their specific application skills.

1. Application Skills of image controls in List controls

1.1 set the image control clistctrl: setimagelist in the following call format:

Cimagelist * setimagelist (cimagelist * pimagelist, int nimagelist );

The returned value is a pointer to the previous image list control. If the previous image list does not exist, it is null. The pimagelist parameter indicates the ID of the image list, And nimagelist indicates the type of the image list, it can be the following values: lvsil_normal uses a large icon to list images; lvsil_samll uses a small icon to list images; lvsil_state uses an image status to list images.

1.2 The call format for obtaining the image control clistctrl: getimagelist is as follows:

Cimagelist * getimagelist (INT nimagelist) const;

The returned value is a pointer to the image list control. nimagelist is used to determine the value of the List of returned images. The value is the same as that of the image list function.

Cimagelist cil1, cil2; // defines the size icon image list.

Cvclistapp * PAPP = (cvclistapp *) afxgetapp (); // retrieves the list control program

Cil1.create (32, 32, true,); // create a 32-bit image control

Cil1.add (PAPP-> loadicon (idi_gj); // Add the selected State Image

Cil1.add (PAPP-> loadicon (idi_xs); // Add an unselected Image

Cil2.create (16, 16, true,); // create a 16-bit image control

Cil2.add (PAPP-> loadicon (idi_gj); // Add the selected State Image

Cil2.add (PAPP-> loadicon (idi_xs); // Add an unselected Image

M_listctrl.setimagelist (& cil1, lvsil_normal); // set large icon Control

M_listctrl.setimagelist (& cil2, lvsil_small); // set small icon Control

2 Application Skills of image controls in tree controls

2.1 set the call format of the image control ctreectrl: setimagelist as follows:

Cimagelist * setimagelist (cimagelist * pimagelist, int nimagelisttype );

The returned value is the pointer to the previous image list. Otherwise, it is null. The pimagelist parameter is the identifier pointing to the image list, if pimagelist is null, all images are cleared from the tree control. nimagelisttype is the type set in the image list. It can be one of the following values: tvsil_normal, the two icons are selected and unselected. tvsil_state sets the image list status, which is the custom status.

2.2 call format for obtaining the image control ctreectrl: getimagelist is as follows:

Cimagelist * getimagelist (uint nimage );

If the call succeeds, the pointer to the image list control is returned. Otherwise, null is returned. nimage is the type of the image list that gets the returned value. The value of nimage is the same as that of the image list control.

Cimagelist cil1, cil2; // defines the size icon image list.

Cvctreeapp * PAPP = (cvctreeapp *) afxgetapp (); // obtain the application pointer

Cil1.create (, ilc_color,); // create an image control

Cil1.add (PAPP-> loadicon (idi_pm); // Add the selected State Image

Cil1.add (PAPP-> loadicon (idi_cj); // Add an unselected Image

M_treectrl.setimagelist (& cil1, tvsil_normal); // sets the image control list.

Then, configure the following settings in the structure definition of the tree control:

Tcitem. item. iimage = 0; // you can specify the index number of an unselected image.

Tcitem. item. iselectedimage = 1; // sets image quotation marks when selected

 

Msdn

Since the image list is not a window, they are not controls, but they are used together with several different types of controls, including list controls (clistctrl) and tree controls (ctreectrl) and tab control (ctabctrl ). All images in the image list are included in a single wide bitmap in the screen device format. The image list may also contain a monochrome bitmap that is used to transparently draw images (icon styles. The member functions provided by cimagelist allow you to draw images, create and destroy image lists, add and remove images, replace images, merge images, and drag images.

Image list type

There are two types of image lists (cimagelist): unshielded and shielded. A list of unshielded images consists of color bitmaps that contain one or more images. The shielded image list consists of two bitmaps of the same size. The first is the color bitmap that contains the image, and the second is a series of shielded monochrome bitmaps (one shielding corresponds to one of the first bitmaps ).

One overload of the create member function, Cao Yong, indicates whether the image list is blocked. (You can create a list of blocked images in other ways ).

When creating an unshielded image, simply copy it to the Target DC. When a shielded image is drawn, the image bit and the shielding bit are combined to generate a transparent area that shows the Target DC background color in the in-place image. You can specify several drawing styles when creating shielded images. For example, you can specify the image to perform image shake to indicate the selected object.

Use Image list

Construct a cimagelist object and call an overload of its create function to create an image list and attach the image list to the cimagelist object;

If no image is added when the image list is created, the Add or read member function is called to add an image to the image list;

Call the appropriate member function of the control to associate the image list with this control, or you can use the draw member function of the image list to draw the image in the image list;

You may be allowed to drag images with built-in drag in the image list.

Note: If the image list is created using the new operator, it must be destroyed after the cimagelist object is operated.

Operation image list

The replace member function replaces the image in the image list (cimagelist) with the new image. This function is also useful if you need to dynamically increase the number of images in the image list. The setimagecount function dynamically changes the number of images stored in the image list. To increase the size of the image list, call replace to add an image to the new image. If the size of the image list is reduced, images larger than the new size will be released.

Remove member functions remove images from the image list. The copy member function can copy or exchange images in the image list. This function enables you to indicate whether the source image should be copied to the target index, or whether the source image and target image should be exchanged.

To create a new image list by merging the two image lists, use the appropriate reload of the create member function. The create overload merges the first image in the existing image list and stores the obtained image in the new image list object. Create a new image by creating the second image transparently on the first image. The shielding of the new image is the result of performing the logic or meta calculation on the shielding bit of the two existing images.

This operation repeats until all images are merged and added to the new image list.

You can call the write member function to write the image information to the archive, and call the read member function to read it back.

The getsafehandle, attach, and detach member functions allow you to operate the image list handle attached to the cimagelist object. The deleteimagelist member function deletes the image list but does not destroy the cimagelist object.



Draw images in the image list

To draw an image, use the cimagelist: draw member function. You will need to specify the pointer to DC, the index of the image to be drawn, the position of the image to be drawn in DC, and a set of flags that indicate the drawing style.

After the ild_transparent style is specified, draw uses a two-step process to draw shielded images.

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.