cimagelist:MFC高级控件之 图像列表控件

图像列表控价与其它控件不同,不能从工具箱的控件面板添加到程序界面中。图像列表控件是一个类:CImageList。要使用该控件,需通过编辑代码实现。该控件用以存储大小相同的一组图像。常使用图像列表控件来为工具条、树状控件、列表控价等的内容设置图标。当然也用可以图像列表将图像绘制到程序中。本文将详细介绍图像列表控件,并以一个实例演示如何使用图像列表。

创建图像列表是通过Create方法,MSDN中对Create方法做了详细说明,列出了一下几个终止函数:

BOOLCreate(intcx,intcy,UINTnFlags,intnInitial,intnGrow);

BOOLCreate(UINTnBitmapID,intcx,intnGrow,COLORREFcrMask);

BOOLCreate(LPCTSTRlpszBitmapID,intcx,intnGrow,COLORREFcrMask);

BOOLCreate(CImageList&imagelist1,intnImage1,CImageList&imagelist2,intnImage2,intdx,intdy);

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 theILC_COLORvalues.

ValueMeaning
ILC_COLORUse the default behavior if none of the otherILC_COLOR* flags is specified. Typically, the default isILC_COLOR4; but for older display drivers, the default isILC_COLORDDB.
ILC_COLOR4Use a 4-bit (16 color) device-independent bitmap (DIB) section as the bitmap for the image list.
ILC_COLOR8Use an 8-bit DIB section. The colors used for the color table are the same colors as the halftone palette.
ILC_COLOR16Use a 16-bit (32/64k color) DIB section.
ILC_COLOR24Use a 24-bit DIB section.
ILC_COLOR32Use a 32-bit DIB section.
ILC_COLORDDBUse a device-dependent bitmap.
ILC_MASKUses 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 aCImageListobject.

nImage1

Index of the first existing image.

imagelist2

A reference to aCImageListobject.

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 aCImageListobject.

CImageList有一个成员变量m_hImageList。

CImageList有一下成员函数:

Attributes

GetSafeHandleRetrievesm_hImageList.
operator HIMAGELISTReturns theHIMAGELISTattached to theCImageList.
FromHandleReturns a pointer to aCImageListobject when given a handle to a device context. If aCImageListobject is not attached to the handle, a temporaryCImageListobject is created and attached.
FromHandlePermanentReturns a pointer to aCImageListobject when given a handle to an image list. If aCImageListobject is not attached to the handle,NULLis returned.
DeleteTempMapCalled by the CWinApp idle-time handler to delete any temporaryCImageListobject created byFromHandle.
GetImageCountRetrieves the number of images in an image list.
SetBkColorSets the background color for an image list.
GetBkColorRetrieves the current background color for an image list.
GetImageInfoRetrieves information about an image.

Operations

AttachAttaches an image list to aCImageListobject.
DetachDetaches an image list object from aCImageListobject and returns a handle to an image list.
DeleteImageListDeletes an image list.
SetImageCountResets the count of images in an image list.
AddAdds an image or images to an image list.
RemoveRemoves an image from an image list.
ReplaceReplaces an image in an image list with a new image.
ExtractIconCreates an icon based on an image and mask in an image list.
DrawDraws the image that is being dragged during a drag-and-drop operation.
SetOverlayImageAdds the zero-based index of an image to the list of images to be used as overlay masks.
CopyCopies an image within aCImageListobject.
DrawIndirectDraws an image from an image list.
SetDragCursorImageCreates a new drag image.
GetDragImageGets the temporary image list that is used for dragging.
ReadReads an image list from an archive.
WriteWrites an image list to an archive.
BeginDragBegins dragging an image.
DragEnterLocks updates during a drag operation and displays the drag image at a specified position.
EndDragEnds a drag operation.
DragLeaveUnlocks the window and hides the drag image so that the window can be updated.
DragMoveMoves the image that is being dragged during a drag-and-drop operation.
DragShowNolockShows 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_TIMEenum { IDD = IDD_TOOLBARTEST_DIALOG };#endifprotected: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运行程序,结果如下:

 效果并没变化。

 

相关推荐

相关文章