How to add bitmap files to CIMAGELIST

zhaozj2021-02-17  56

In the MFC, the CIMAGELIST class encapsulates the function of the image list control. The image list control is a collection of the same size image, which is stored in the application of large-scale icons in the application. This control is invisible control, usually with other controls. Use to provide icon resources for other controls. In each collection of the image, in 0 is an image index base, these icons can be regarded as being stored in an array mode, and the image list is typically constructed of a large icon or a bitmap, which contains bitmap mode, in fact, all The icons in the same image list are stored in a bitmap of a screen device.

The method of adding image elements to the CIMAGELIST object is: First transfer the icon or bitmap into the resource file, then call the CIMAGELIST: Add method to add the bitmap to the resource to increase the executable. It is not conducive to software issuance, and only other bitmaps can only be selected using limited bitmaps in resources. To make up for the use of the resource bitmap, you must use the BMP bitmap file directly.

Use the following code to implement:

Hbitmap hbitmap;

CBITMAP * PBITMAP;

CIMAGELIST * PIMAGELIST;

Pimagelist = new cimagelist;

PimageList-> Create (32, 32, ILC_COLOR4, 5, 2);

Pbitmap = new cbitmap;

Hbitmap = (hbitmap) loadImage (null, filepathname, image_bitmap, 0, 0, lr_loadfromfile); // filepathname is a bitmap file full path string

Pbitmap-> attach (hbitmap);

PimageList-> Add (Pbitmap, RGB (0, 0, 0));

Next, combine an example detailed usage:

Routine analysis:

The routine is based on a dialog box, click the 'Add BMP' button in the dialog box to pop up the file selection box, select a BMP file to add the selected BMP file to the list control. In the MFC, the CListCtrl class encapsulates the list control, mainly to display a set of data records in a variety of ways to perform various operations, and the records in the list can include multiple data items, or the size icon indicating the data content. , Used to indicate various properties of the data record, the right frame of the Windows Explorer is a very typical list control.

New MFC AppWiazrd [EXE] Project, project name imagelistaddbmp, point OK, select the Dialog Based point Next, the rest of the next step is default, it has been pressed until the completion is completed, the project is completed. Follow these steps to set a few variables, fill in the event code:

1. Add a CListCtrl control and a button in the IDD_IMAGELISTADDBMP_DIALOG dialog, press New CAPTION to "Add BMP".

2. Add Ctrllist control variables M_ListCtrl using the class wizard.

3. Add two common variables below the CImageListAddbmpdlg class: int index; cimagelist * pimagelist;

4. In the Bool CimagelistAddbmpdlg :: OnInitDialog () Initialization function, // Todo: add extra INITIZATION Here statement INDEX = 0; pimagelist-> Create (32, 32, ILC_COLOR4, 5, 2); m_listctrl.setimagelist (pimagelist, lvsil_normal); 5. In IDD_IMAGELISTADDBMP_DIALOG dialog box, double-click the 'Add bmp' button, add Onbutton1 click event code: void CImageListAddBmpDlg :: OnButton1 () {// TODO: Add your control notification handler code here CString FilePathName, FileName; CFileDialog dlg (TRUE); / // true is the open dialog, false is the Save AS dialog, = d (DLG.Domodal () == idok) {filepathname = DLG.GetPathName (); hbitmap hbitmap; cbitmap * pbitmap; pbitmap = new cbitmap; hbitmap = (Hbitmap ) Loading (null, filepathname, image_bitmap, 0, 0, lr_loadfromfile); pbitmap-> attach (hbitmap); pimagelist-> add (Pbitmap, RGB (0, 0)); filename = DLG.GetFileName (); m_listctrl .Insertitem (Index, FileName, INDEX); Index ;}}

Here you can compile, compile them under VC6.o.

The source code can be requested by email.

Email: ljf518@163.net

转载请注明原文地址:https://www.9cbs.com/read-30057.html

New Post(0)