图像列表控价与其它控件不同,不能从工具箱的控件面板添加到程序界面中。图像列表控件是一个类:CImageList。要使用该控件,需通过编辑代码实现。该控件用以存储大小相同的一组图像。常使用图像列表控件来为工具条、树状控件、列表控价等的内容设置图标。当然也用可以图像列表将图像绘制到程序中。本文将详细介绍图像列表控件,并以一个实例演示如何使用图像列表。
创建图像列表是通过Create方法,MSDN中对Create方法做了详细说明,列出了一下几个终止函数:
BOOL Create( int cx, int cy, UINT nFlags, int nInitial, int nGrow );
BOOL Create( UINT nBitmapID, int cx, int nGrow, COLORREF crMask );
BOOL Create( LPCTSTR lpszBitmapID, int cx, int nGrow, COLORREF crMask );
BOOL Create( CImageList& imagelist1, int nImage1, CImageList& imagelist2, int nImage2, int dx, int dy );
BOOL Create( CImageList* pImageList );
如果生成图像列表成功返回非零值,否则返回值为零。MSDN中对函数参数做了如下说明:
Parameters
cx
Dimensions of each image, in pixels.
cy
Dimensions of each image, in pixels.
nFlags
Specifies the type of image list to create. This parameter can be a combination of the following values, but it can include only one of the ILC_COLOR values.
Value | Meaning |
ILC_COLOR | Use the default behavior if none of the other ILC_COLOR* flags is specified. Typically, the default is ILC_COLOR4; but for older display drivers, the default is ILC_COLORDDB. |
ILC_COLOR4 | Use a 4-bit (16 color) device-independent bitmap (DIB) section as the bitmap for the image list. |
ILC_COLOR8 | Use an 8-bit DIB section. The colors used for the color table are the same colors as the halftone palette. |
ILC_COLOR16 | Use a 16-bit (32/64k color) DIB section. |
ILC_COLOR24 | Use a 24-bit DIB section. |
ILC_COLOR32 | Use a 32-bit DIB section. |
ILC_COLORDDB | Use a device-dependent bitmap. |
ILC_MASK | Uses a mask. The image list contains two bitmaps, one of which is a monochrome bitmap used as a mask. If this value is not included, the image list contains only one bitmap. |
nInitial
Number of images that the image list initially contains.
nGrow
Number of images by which the image list can grow when the system needs to resize the list to make room for new images. This parameter represents the number of new images the resized image list can contain.
nBitmapID
Resource IDs of the bitmap to be associated with the image list.
crMask
Color used to generate a mask. Each pixel of this color in the specified bitmap is changed to black, and the corresponding bit in the mask is set to one.
lpszBitmapID
A string containing the resource IDs of the images.
imagelist1
A reference to a CImageList object.
nImage1
Index of the first existing image.
imagelist2
A reference to a CImageList object.
nImage2
Index of the second existing image.
dx
Offset of the x-axis of the second image in relationship to the first image, in pixels.
dy
Offset of the y-axis of the second image in relationship to the first image, in pixels.
pImageList
A pointer to a CImageList object.
CImageList有一个成员变量m_hImageList。
CImageList有一下成员函数:
GetSafeHandle | Retrieves m_hImageList. |
operator HIMAGELIST | Returns the HIMAGELIST attached to the CImageList. |
FromHandle | Returns a pointer to a CImageList object when given a handle to a device context. If a CImageList object is not attached to the handle, a temporary CImageList object is created and attached. |
FromHandlePermanent | Returns a pointer to a CImageList object when given a handle to an image list. If a CImageList object is not attached to the handle, NULL is returned. |
DeleteTempMap | Called by the CWinApp idle-time handler to delete any temporary CImageList object created by FromHandle. |
GetImageCount | Retrieves the number of images in an image list. |
SetBkColor | Sets the background color for an image list. |
GetBkColor | Retrieves the current background color for an image list. |
GetImageInfo | Retrieves information about an image. |
Attach | Attaches an image list to a CImageList object. |
Detach | Detaches an image list object from a CImageList object and returns a handle to an image list. |
DeleteImageList | Deletes an image list. |
SetImageCount | Resets the count of images in an image list. |
Add | Adds an image or images to an image list. |
Remove | Removes an image from an image list. |
Replace | Replaces an image in an image list with a new image. |
ExtractIcon | Creates an icon based on an image and mask in an image list. |
Draw | Draws the image that is being dragged during a drag-and-drop operation. |
SetOverlayImage | Adds the zero-based index of an image to the list of images to be used as overlay masks. |
Copy | Copies an image within a CImageList object. |
DrawIndirect | Draws an image from an image list. |
SetDragCursorImage | Creates a new drag image. |
GetDragImage | Gets the temporary image list that is used for dragging. |
Read | Reads an image list from an archive. |
Write | Writes an image list to an archive. |
BeginDrag | Begins dragging an image. |
DragEnter | Locks updates during a drag operation and displays the drag image at a specified position. |
EndDrag | Ends a drag operation. |
DragLeave | Unlocks the window and hides the drag image so that the window can be updated. |
DragMove | Moves the image that is being dragged during a drag-and-drop operation. |
DragShowNolock | Shows or hides the drag image during a drag operation, without locking the window. |
接下来以一个实例来演示图像控件的创建及其应用。
1. 先用VC2017创建一个对话框程序。
2.从下面图标中挑选10个图标加入资源中 ,图标大小都是24*24 像素
加入后如下图示:
3. 用加入的图标资源来创建工具栏,先在对话框头文件中,声明两个变量,如下:
// CToolbarTestDlg 对话框
class CToolbarTestDlg : public CDialogEx
{
// 构造
public:
CToolbarTestDlg(CWnd* pParent = nullptr); // 标准构造函数
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_TOOLBARTEST_DIALOG };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
HICON m_hIcon;
CToolBar m_ToolBar;
CImageList m_ImageList;
// 生成的消息映射函数
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
};
其中m_ToolBar,m_ImageList就是声明的变量,再在对话框的源文件的OnInitDialog()函数中添加如下代码:
整个函数代码如下:
BOOL CToolbarTestDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != nullptr)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
m_ImageList.Create(32,32,ILC_COLOR24||ILC_MASK,1,1);
for (int i = 0; i < 10; i++)
{
m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON1 + i));
}
UINT array[10];
for (int i = 0; i < 10; i++)
{
array[i] = i + 1001;
}
m_ToolBar.Create(this);
m_ToolBar.SetButtons(array, 10);
m_ToolBar.GetToolBarCtrl().SetImageList(&m_ImageList);
m_ToolBar.SetSizes(CSize(40,40),CSize(32,32));
RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
Ctrl+F5运行程序,结果如下:
点击第一个图标,效果如下图:
4.修改图像列表Create()函数的参数,观看效果变化
将m_ImageList.Create(32,32,ILC_COLOR24||ILC_MASK,1,1);修改为:
m_ImageList.Create(24,24,ILC_COLOR24||ILC_MASK,1,1); 即改变了图标大小,按Ctrl+F5运行程序,结果如下:
工具栏中的图标变小了。
将m_ImageList.Create(32,32,ILC_COLOR24||ILC_MASK,1,1);修改为:
m_ImageList.Create(32,32,ILC_COLOR24,1,1);按Ctrl+F5运行程序,结果如下:
图标多了一个黑色背景。 点击第一个图标,结果如下:
将m_ImageList.Create(32,32,ILC_COLOR24||ILC_MASK,1,1);修改为:
m_ImageList.Create(32,32,ILC_COLOR24 || ILC_MASK,0,0);按Ctrl+F5运行程序,结果如下:
效果并没变化。