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

15 篇文章 5 订阅

图像列表控价与其它控件不同,不能从工具箱的控件面板添加到程序界面中。图像列表控件是一个类: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.

ValueMeaning
ILC_COLORUse 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_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 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有一下成员函数:

Attributes

GetSafeHandleRetrieves m_hImageList.
operator HIMAGELISTReturns the HIMAGELIST attached to the CImageList.
FromHandleReturns 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.
FromHandlePermanentReturns 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.
DeleteTempMapCalled by the CWinApp idle-time handler to delete any temporary CImageList object created by FromHandle.
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 a CImageList object.
DetachDetaches an image list object from a CImageList object 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 a CImageList object.
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_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运行程序,结果如下:

 效果并没变化。

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bill66

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值