真彩图标工具栏实现类

加载此类可以实现真彩图标工具栏

ToolBarEx.h文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#if !defined(AFX_TOOLBAREX_H__7F634227_9811_4C48_A18D_5D6C640DC6CB__INCLUDED_)
#define AFX_TOOLBAREX_H__7F634227_9811_4C48_A18D_5D6C640DC6CB__INCLUDED_
 
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// ToolBarEx.h : header file
//
/
//CToolBarEx: 真彩色工具栏
/
// CToolBarEx window
 
class  CToolBarEx :  public  CToolBar
{
// Construction
public :
     CToolBarEx();
     virtual  ~CToolBarEx();
 
// Attributes
public :
 
// Operations
public :
 
// Overrides
     // ClassWizard generated virtual function overrides
     //{{AFX_VIRTUAL(CToolBarEx)
     //}}AFX_VIRTUAL
 
// Implementation
public :
     BOOL  CreateHotToolBar(CWnd* pParentWnd, LPCTSTR  lpszResName, LPCTSTR  lpszHotResName);
     void  SetBlackMask( COLORREF    crMask);
     BOOL  LoadToolBar( LPCTSTR  lpszResourceName);
     BOOL  LoadToolBar( UINT  nIDResource);
     void  SetButtonText( const  CStringArray& strArr);
     void  LoadBitmap( UINT  *pnIDBmp, UINT  nCount,CSize sizeBmp);
     BOOL  LoadBitmap( LPCTSTR  lpszResourceName, BOOL  bHot);
     // Generated message map functions
protected :
     BOOL  LoadBitmap( LPCTSTR  lpszResourceName);
     //{{AFX_MSG(CToolBarEx)
     //}}AFX_MSG
 
     DECLARE_MESSAGE_MAP()
//
     CSize m_nSizeImage;
     CImageList m_cImageList;
     COLORREF    m_crMask;         //背景色
 
};
 
/
 
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
 
#endif // !defined(AFX_TOOLBAREX_H__7F634227_9811_4C48_A18D_5D6C640DC6CB__INCLUDED_)

ToolBarEx.cpp文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// ToolBarEx.cpp : implementation file
//
 
#include "stdafx.h"
 
#include "ToolBarEx.h"
 
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static  char  THIS_FILE[] = __FILE__;
#endif
 
/
// CToolBarEx
 
CToolBarEx::CToolBarEx()
{
     m_crMask = RGB(255,0,255);
}
 
CToolBarEx::~CToolBarEx()
{
}
 
 
BEGIN_MESSAGE_MAP(CToolBarEx, CToolBar)
     //{{AFX_MSG_MAP(CToolBarEx)
     //}}AFX_MSG_MAP
END_MESSAGE_MAP()
 
/
// CToolBarEx message handlers
 
void  CToolBarEx::LoadBitmap( UINT  *pnIDBmp,  UINT  nCount,CSize sizeBmp)
{
     CImageList imgBmp;
 
     //设定位图
     imgBmp.Create(sizeBmp.cx ,sizeBmp.cy,ILC_COLORDDB,4,4);
     for ( UINT  n=0;n<nCount;n++)
     {
         CBitmap Bitmap;
         Bitmap.LoadBitmap (pnIDBmp[n]);
         imgBmp.Add (&Bitmap,RGB(0,0,0));
     }
     GetToolBarCtrl( ).SetImageList ( &imgBmp ) ;
     imgBmp.Detach( ) ;
     
}
 
void  CToolBarEx::SetButtonText( const  CStringArray& strArr)
{
     int  nCount = GetToolBarCtrl( ).GetButtonCount();
 
     ASSERT(nCount>strArr.GetUpperBound());
     int  nItem = 0;
     for ( int  n = 0; n < nCount; n++)
     {
         ASSERT(nItem <= strArr.GetUpperBound());
         UINT  unStyle = GetButtonStyle(n);
         if (unStyle != TBBS_SEPARATOR)
             CToolBar::SetButtonText (n, strArr.GetAt (nItem++) ) ;
     }
     //
     CRect rt ;
     GetItemRect ( 0, &rt ) ;
     CSize sizeItem = rt.Size ();
//    sizeItem.cy +=3;
     SetSizes ( sizeItem, m_nSizeImage ) ;
 
}
 
BOOL  CToolBarEx::LoadToolBar( UINT  nIDResource)
{
 
     BOOL  bResult = CToolBar::LoadToolBar(nIDResource);
     if (!bResult)
         return  bResult;
 
     return  LoadToolBar(MAKEINTRESOURCE(nIDResource));
     return  bResult;
}
 
BOOL  CToolBarEx::LoadToolBar( LPCTSTR  lpszResourceName)
{
     ASSERT_VALID( this );
     ASSERT(lpszResourceName != NULL);
 
 
     BOOL  bResult = CToolBar::LoadToolBar (lpszResourceName);
     if (!bResult)
         return  bResult;
     
     CRect Rect;
//    CToolBar::GetToolBarCtrl().GetItemRect(0,&Rect);
//    DWORD dsBtnSize=GetToolBarCtrl().GetButtonSize();
//    int nWidth=LOWORD(dsBtnSize);    //按钮外型尺寸
//    int nHeight=HIWORD(dsBtnSize);
     int  nImageCount = CToolBar::GetToolBarCtrl().GetImageList ()->GetImageCount ();
     VERIFY(nImageCount > 0);
     //位图大小
     IMAGEINFO ImageInfo;
     CToolBar::GetToolBarCtrl().GetImageList ()->GetImageInfo (0,&ImageInfo);
     int  nWidth  = ImageInfo.rcImage .right - ImageInfo.rcImage.left ;
     int  nHeight = ImageInfo.rcImage .bottom - ImageInfo.rcImage.top ;
     m_nSizeImage.cx = nWidth;
     m_nSizeImage.cy = nHeight;
 
     
     //*
     // 加载工具栏位图 - 必须使用::LoadImage映射颜色
     // 将(192,192,192) 映射到 COLOR_3DFACE.
     //
     HBITMAP  hbm = ( HBITMAP )::LoadImage(AfxGetInstanceHandle(),
         lpszResourceName,
         IMAGE_BITMAP,
         0,0,  // cx,cy
         LR_CREATEDIBSECTION | LR_LOADMAP3DCOLORS );
 
     CBitmap Bitmap;
     Bitmap.Attach(hbm);
 
     // Create image list and set in toolbar.
     // Must use ILC_COLOR24 for 256-color images!
     // 创建图像清单并设置工具栏
     // 256色图像必须使用ILC_COLOR8!
 
     //方法:
     CImageList cImage; //
     //1           
     cImage.Create(nWidth,nHeight, TRUE|ILC_COLOR24, 4, 4);     //TRUE|ILC_COLOR24 ILC_COLOR8//TRUE-----透明模式
     cImage.Add(&Bitmap,m_crMask);
     //2
     GetToolBarCtrl().SetImageList(&cImage);
     //3
     Bitmap.Detach ();
     cImage.Detach ();
//*/
     return  bResult;
}
 
BOOL  CToolBarEx::LoadBitmap( LPCTSTR  lpszResourceName)
{
//*
     CBitmap Bitmap;
     BOOL  bResult = Bitmap.LoadBitmap (lpszResourceName);
     if (bResult)
         SetBitmap (Bitmap.operator  HBITMAP ());
     Bitmap.Detach();
//*/
/*
     // 加载工具栏位图 - 必须使用::LoadImage映射颜色
     // 将(192,192,192) 映射到 COLOR_3DFACE.
     //
     HBITMAP hbm = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),
//        MAKEINTRESOURCE(nIDResource),
         lpszResourceName,
         IMAGE_BITMAP,
         0,0, // cx,cy
         LR_CREATEDIBSECTION | LR_LOADMAP3DCOLORS );//Gray, RGB(192,192,192) COLOR_3DFACE 
     CBitmap Bitmap;
     Bitmap.Attach(hbm);
     SetBitmap (Bitmap.operator HBITMAP());
     Bitmap.Detach();
     BOOL bResult=TRUE;
 
     // 创建图像清单并设置工具栏
     // 256色图像必须使用ILC_COLOR8!
     //
//    m_cImageList.Create(20,20, ILC_COLOR8, 4, 4);
//    m_cImageList.Create(20,20, ILC_COLOR24, 4, 4);
//    bResult=m_cImageList.Add(&bm,(CBitmap*)NULL);
//    GetToolBarCtrl().SetImageList(&m_cImageList);
 
//*/
 
     return  bResult;
}
 
void  CToolBarEx::SetBlackMask( COLORREF    crMask)
{
     m_crMask = crMask;
}
 
BOOL  CToolBarEx::CreateHotToolBar(CWnd* pParentWnd, LPCTSTR  lpszResName, LPCTSTR  lpszHotResName)
{
     BOOL  bResult = FALSE;
     
     CToolBar cToolBar;
     cToolBar.CreateEx (pParentWnd);
     cToolBar.LoadToolBar (lpszResName);
     //位图大小
     IMAGEINFO ImageInfo;
     cToolBar.GetToolBarCtrl().GetImageList ()->GetImageInfo (0,&ImageInfo);
     int  nWidth = ImageInfo.rcImage .right - ImageInfo.rcImage.left ;
     int  nHeight = ImageInfo.rcImage .bottom - ImageInfo.rcImage.top ;
     m_nSizeImage.cx = nWidth;
     m_nSizeImage.cy = nHeight;
 
     //
     CreateEx (pParentWnd);
     LoadBitmap(lpszResName,FALSE);
     LoadBitmap(lpszHotResName,TRUE);
 
     //
     ModifyStyle ( 0, TBSTYLE_FLAT | TBSTYLE_TRANSPARENT ) ;
 
     int  nCount = cToolBar.GetCount ();
     SetButtons ( NULL,nCount ) ;
     for ( int  i = 0; i < nCount; i++)
     {
         UINT  nID;
         UINT  nStyle;
         int  iImage;
         cToolBar.GetButtonInfo(i,nID,nStyle,iImage);
         SetButtonInfo ( i, nID, nStyle, iImage ) ;
     }
     //设定按钮尺寸
     CRect rt ;
     GetItemRect ( 0, &rt ) ;
     SetSizes( rt.Size( ), m_nSizeImage ) ;
 
     return  bResult;
}
 
BOOL  CToolBarEx::LoadBitmap( LPCTSTR  lpszResourceName, BOOL  bHot)
{
     BOOL  bResult = TRUE;
     //
     // 加载工具栏位图 - 必须使用::LoadImage映射颜色
     // 将(192,192,192) 映射到 COLOR_3DFACE.
     //
     HBITMAP  hbm = ( HBITMAP )::LoadImage(AfxGetInstanceHandle(),
         lpszResourceName,
         IMAGE_BITMAP,
         0,0,  // cx,cy
         LR_CREATEDIBSECTION | LR_LOADMAP3DCOLORS );
 
     CBitmap Bitmap;
     Bitmap.Attach(hbm);
 
     // Create image list and set in toolbar.
     // Must use ILC_COLOR24 for 256-color images!
     // 创建图像清单并设置工具栏
     // 256色图像必须使用ILC_COLOR8!
 
     //方法:
     CImageList cImage; //
     //1           
     cImage.Create(m_nSizeImage.cx ,m_nSizeImage.cy , TRUE|ILC_COLOR24, 4, 4);     //TRUE|ILC_COLOR24 ILC_COLOR8//TRUE-----透明模式
     cImage.Add(&Bitmap,m_crMask);
     //2
     if (!bHot)
         GetToolBarCtrl().SetImageList(&cImage);
     else
         GetToolBarCtrl().SetHotImageList(&cImage);
 
     //3
     Bitmap.Detach ();
     cImage.Detach ();
//*/
     return  bResult;
 
}
调用方式:
仅在MainFrm.h文件中

CToolBar  m_wndToolBar;
修改为
CToolBarEx  m_wndToolBar;

注意工具栏的背景色
效果:


位图:


此文章引用自【http://bbs.csdn.net/topics/390627052】

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值