13.2.4 为工具栏添加图像资源关联
实现一个函数,在这个函数中将根据串口开关状态为工具栏载入位图资源。
- 01 void CComElvesDlg::SetOpenButton()
- 02 {
- 03 CImageList imageList;
- 04 CBitmap bitmap;
- 05 if(!m_bOpened)
- 06 {
- 07 //创建并设置串口关闭时工具栏位图链表
- 08 bitmap.LoadBitmap(IDB_TOOL_CLOSE);
- 09 imageList.Create(32, 32, ILC_COLORDDB|ILC_MASK, 13, 1);
- 10 imageList.Add(&bitmap, RGB(192,192,192));
- 11 }
- 12 else
- 13 {
- 14 //创建并设置串口打开时工具栏位图链表
- 15 bitmap.LoadBitmap(IDB_TOOL_OPEN);
- 16 imageList.Create(32, 32, ILC_COLORDDB|ILC_MASK, 13, 1);
- 17 }
- 18 imageList.Add(&bitmap, RGB(192,192,192));
- 19 //向工具栏发送消息添加位图链表
- 20 m_ToolBar.SendMessage(TB_SETIMAGELIST, 0,
- 21 (LPARAM)imageList.m_hImageList);
- 22 imageList.Detach();
- 23 bitmap.Detach();
- 24 m_ToolBar.Invalidate();
- 25 }
【代码解析】
第5~11行载入串口关闭时的位图资源,第12~18行载入串口打开时的位图资源。第19行将位图插入图像列表,第20行将向工具栏发送插入图像列表的消息,将刚刚插入位图资源的图像列表作为工具栏位图。如此便完成了自定义位图的工具栏。