| Header file: | imagelist.h |
| Object name: | fIMAGELIST |
| Object index: | 1 of 1 Object |
The fIMAGELIST class is a class designed to handle a set of images from one location. By having a set of the same size images in one continuous set, the total space in memory required for all of the images can be reduced.
So, what it boils down to is that this can be a convenient way to store a lot of images that are the same size and colour depth. However, the main use of this component is to partner with the fLISTVIEW control and provide it with icons to use.
When creating an imagelist, you will need to specify a bit-depth for all of the images. You can use the constants shown below to specify that bitdepth.
| Name | Description | Value |
| bd4BPP | 4-bits-per-pixel per image (16 colours). | ILC_COLOR4 |
| bd8BPP | 8-bits-per-pixel per image (256 colours). | ILC_COLOR8 |
| bd16BPP | 16-bits-per-pixel per image (65,536 colours). | ILC_COLOR16 |
| bd24BPP | 24-bits-per-pixel per image (16,777,216 colours). | ILC_COLOR24 |
| bd32BPP | 32-bits-per-pixel per image (16,777,216 colours, plus an Aplha channel). | ILC_COLOR32 |
| bdDeviceDependant | This setting will use whatever your screen is set to. If your screen displays 256 colour, then the image list will be 8BPP. If your screen displays 16,777,216 colours plus the alpha channel, then the image list will be 32BPP. If you are not sure what to set, then use this one. If you specifically wish to save memory, then use another constant that relates to what your images are. | ILC_COLORDDB |
| bdMask | This constant, when mixed with others with the OR (|) operator, will make the image list set aside memory as a 'mask'. You can then use this mask to mask out your image. By this I mean that you can have a black-and-white mask that shows what areas of the image that you would like shown. Please note that the mask portion of the image list is only a monochrome bitmap. The white areas of the mask indicate what areas not to draw, whilst the black areas indicate what to draw. | ILC_MASK |
| Name | Description |
| HIMAGELIST Handle; | This member is the handle to the actual imagelist, as stored by Windows. You can use this handle if you need to call extra functions with the imagelist itself. However, please be careful with this, because you can read and write to this member. Writing to this member is generally not a good idea unless you are sure of what you are doing. |
| Prototype | Description |
| void Create(int Width, int Height, int Depth, int InitialImages, int GrowRate); | This function actually goes and creates the image list for you. You will need to specify the Width and Height of the images to be included into the image list. Depth is one of the constants from the table at the top of this document. Initial images is the number of images that memory is initially allocated for. GrowRate is the number of images to expand the memory by each time a resize is needed. Generally, you can just use 1 for InitialImages and GrowRate, but if you know how many images you are likely to receive, you can speed up your program by creating the imagelist with enough memory for all that you want, instead of the program having to go and re-allocate the memory later. |
| void Destroy(void); | This function destroys the imagelist currently in memory. Please note that the imagelist is destroyed automatically when the class is destroyed. |
|
void AddBitmap(HBITMAP Bitmap); void AddBitmap(char* FileName); void AddBitmap(char* Name, int ID); void AddBitmap(HINSTANCE Instance, char* Name, int ID); | All of these functions add a bitmap to the image list. Bitmap specifies a handle to a bitmap. FileName specifies a filename to load a bitmap from. The last two functions load a bitmap from a resource - the second last function loads the bitmap from your program's resources, and the last function loads the bitmap from the specified module's resources. When getting resources, you can specify either the name of the resource or it's ID. If you use ID, then Name should be NULL. Likewise, if you use Name, then ID should be 0. |
|
void AddBitmapMask(HBITMAP Bitmap, HBITMAP Mask); void AddBitmapColourMask(HBITMAP Bitmap, COLORREF Mask); | Both of these functions add a bitmap with a mask. The first function allows you to specify another bitmap as the mask. The second function allows you to specify a colour as the mask - and every pixel having the specified colour in the original image is made transparent. |
|
void AddIcon(HICON Icon); void AddIcon(char* Name, int ID); void AddIcon(HINSTANCE Instance, char* Name, int ID); void AddIcon(char* FileName); | Each of these functions add an icon to the imagelist. Icon specifies a handle to an icon to add to the imagelist. The second and third function specify that the icon is to be loaded from a resource - the second function will load the resource from the current program's resources, and the third function will load the resources from the specified module, be that a DLL or something similar. The last function will load an icon from the specified file. If you load an icon from the resources, you can either specify the Name of the reource or it's ID - and you should specify either NULL for Name if you use ID or 0 (Zero) for ID if you use Name. |
| void LoadIconFromDLL(char* DLLName, int Index); | This function loads an icon from a DLL or executable file - just specify the filename and the number of the icon that you wish to extract. The file can be either a DLL, an Executable (*.EXE), a screensaver (*.SCR), or an OCX (*.OCX). You can use the next function to determine the total number of icons inside a specific file. |
| int GetNumberIcons(char* DLLName); | This function returns the number of icons inside the specified file. |
| void LoadPremadeBitmap(HINSTANCE Instance, char* Name, int ID, int Width, int GrowRate, COLORREF Mask); |
This function loads a bitmap from resources which is seperated into smaller bitmaps and then placed into memory. When you save the images, you would save them as lots of images side-by-side. When you load them with this function, you will need to specify the width of the images, so that Windows can split up the loaded bitmap. You need to specify the module to load the bitmap from (specified by Instance), and also the Name or ID of the resource - if you use Name, ID should be 0, and if you use ID, Name should be NULL. If you are loading the bitmap from your program's resources, the specify GetModuleHandle(NULL) for Instance. GrowthRate is the number of extra images to allocate memory for should the imagelist need to be resized. Mask specifies a colour to use as the mask - any images in the list that have pixels with this colour will then have those pixels become transparent.
|
| void LoadPremadeBitmap(char* FileName, int Width, int GrowRate, COLORREF Mask); | This function is exactly like the above function, except for the fact that it loads the imagelist from file instead of from a resource. |
| void LoadPremadeIcon(HINSTANCE Instance, char* Name, int ID, int Width, int GrowthRate, COLORREF Mask); | This function is exactly like the LoadPremadeBitmap() functions, except for the fact that it loads a premade set of icons instead of a bitmap. |
| void LoadPremadeIcon(char* FileName, int Width, int GrowRate, COLORREF Mask); | This function is exactly like the above function (whose description references another function) except that it loads a premade icon set from a file. |
| void ReplaceImage(int Index, HBITMAP Bitmap, HBITMAP Mask); | This function replaces an image in the image list - the Index of the bitmap specified by Index will be replaced with Bitmap and Mask, if applicable. |
| void ReplaceImage(int Index, HICON Icon); | This function replaces the icon specified by Index with the icon that is also specified by Icon. |
| void RemoveImage(int Index); | This function removes the image at Index. All of the images after that move down one to fill the hole. For example, if you had five images in the imagelist, and you removed the third, the fourth image would become the third, and the fifth image would become the fourth. |
| HICON GetIcon(int Index, int Style); |
This function returns an icon from the imagelist - the icon specified by Index. Through Style, you can set how the icon is returned. Just use one or more of the following values (taken from the Win32 SDK Reference Manual):
|
| HBITMAP GetBitmap(int Index); | This function returns a bitmap from the imagelist. Index specifies which bitmap to return. |
| HBITMAP GetBitmapMask(int Index); | This function returns the mask associated with a bitmap. Index specifies which image to retrieve the mask for. |
| int GetListWidth(void); | This function returns the width of the images in the image list. All images are of the same width, so the value returned is applicable to all images. |
| int GetListHeight(void); | This function returns the height of the images in the imagelist. Each image is the the same height, so the value returned is applicable to all images. |
| int GetNumberImages(void); | This function returns the number of images inside the imagelist. |
| void SetBackgroundColour(COLORREF Colour); | This function sets the background colour of the image list. This colour is the colour that is used when drawing an image, as the transparent colour. To make sure that the images are drawn transparently, you might like to call this function with CLR_NONE as the colour. |
| COLORREF GetBackgroundColour(void); | This function returns the background colour for the imagelist, which is the colour that is used to specify what the colours that are transparent are. |
| void DrawImage(int Index, HDC HandleDC, int X, int Y, int Options); |
This function draws the specified image on the specified Device Context, at the specified location with the specified options. Not specific enough? Well, anyway, the following are the styles that you can use for Options: (taken from the Win32 SDK Reference Manual)
|
The following snippet gives a basic idea of how to use this component.
//Create an image list...
fIMAGELIST ImgList;
//Create a list that has images each 32x32 pixels,
//at 8-bits-per-pixel, initially 10 images, and
//resize by 10 each time a resize is needed.
ImgList.Create(32, 32, bd8BPP, 10, 10);
//Now add some images (icons in this case).
ImgList.AddIcon(Icon); //Just from a HICON you had...
ImgList.AddIcon("MAIN_ICON", 0); //Load MAIN_ICON from resources.
HINSTANCE DLLInst = LoadLibrary("MYDLL.DLL");
ImgList.AddIcon(DLLInst, "OTHER_ICON", 0); //Load from a DLL.
FreeLibrary(DLLInst); //Unload the DLL! Don't forget!
ImgList.AddIcon("c:\Windows\icon.ico"); //Load from file.
ImgList.LoadIconFromDLL("SHELL32.DLL", 10); //Load the 10th icon.
//Please note that with the above function, only the 32x32 8BPP version
//will be loaded. There are several versions of each icon in the DLL.
//Only the 32x32 8BPP will be loaded because that is the type of image
//list that you have created.
//Now return an icon or two...
HICON Icon = ImgList.GetIcon(3, ILD_NORMAL);
//When you are done, you might like to destroy the imagelist.
//However, this is done automatically for you when the class
//is destroyed.
ImgList.Destroy();
//Now here's a use for this imagelist: animation. Let's say that
//you create a bitmap that is 32 pixels high, but 320 pixels wide.
//Inside this picture is a series of 32 pixel wide frames, side by
//side. What you'd do first is to save this file as an icon,
//and then load it into the image list.
//All of the pixels that are white will be made transparent. If you
//want to keep white non-transparent, then just change the transparent
//stuff to something very close to white.
//Anyway, the next line loads the file animation.ico, and specifies that
//each image is 32 pixels wide - Windows splits up the images on the
//32 pixel border. Next, we say that whenever more memory needs to
//be allocated, allocate it in one image segments. Finally, we
//say to change all pixels that are white into transparent.
ImgList.LoadPremadeIcon("animation.ico", 32, 1, RGB(255,255,255));
//Now just cycle through the images, drawing them
//on the specified HDC.
for (int ITemp = 0; ITemp < 10; ITemp++)
{
//The next line draws: Image no ITemp, on the device context
//HandleDC, at (10,10), and draws transparently.
ImgList.DrawImage(ITemp, HandleDC, 10, 10, ILD_TRANSPARENT);
}
//And that's about it...
| Back to index | The FreeFoote Foundation Classes Documentation |