MFC下加载自定义真彩色工具条
2008-03-19 16:33
330 查看
这两天给自己的一个软件增加自定义工具条,走了不少弯路。在网上也找了很多资料,下了不少例子。可是我怎么对照资料,都没办法把自己的工具条放到街面上。两天下来几乎没有任何进展。今天换了个思路,在资源视图中添加了一个TOOLBAR后,发现竟然OK了。
加载真彩色图片类的头文件:
class CTrueColorToolBar : public CToolBar
...{
// Construction
public:
CTrueColorToolBar();
// Operations
public:
BOOL LoadTrueColorToolBar(int nBtnWidth,UINT uToolBar,
UINT uToolBarHot,UINT uToolBarDisabled = 0);
private:
BOOL SetTrueColorToolBar(UINT uToolBarType,
UINT uToolBar,
int nBtnWidth);
// Implementation
public:
virtual ~CTrueColorToolBar();
protected:
//{{AFX_MSG(CTrueColorToolBar)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
实现文件:
#include "TrueColorToolBar.h"
/**//////////////////////////////////////////////////////////////////////////////
// CTrueColorToolBar
CTrueColorToolBar::CTrueColorToolBar()
...{
}
CTrueColorToolBar::~CTrueColorToolBar()
...{
}
BEGIN_MESSAGE_MAP(CTrueColorToolBar, CToolBar)
//{{AFX_MSG_MAP(CTrueColorToolBar)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/**//////////////////////////////////////////////////////////////////////////////
// CTrueColorToolBar message handlers
BOOL CTrueColorToolBar::LoadTrueColorToolBar(int nBtnWidth, UINT uToolBar,UINT uToolBarHot,UINT uToolBarDisabled)
...{
if (!SetTrueColorToolBar(TB_SETIMAGELIST, uToolBar, nBtnWidth))
return FALSE;
if (!SetTrueColorToolBar(TB_SETHOTIMAGELIST, uToolBarHot, nBtnWidth))
return FALSE;
if (uToolBarDisabled) ...{
if (!SetTrueColorToolBar(TB_SETDISABLEDIMAGELIST, uToolBarDisabled, nBtnWidth))
return FALSE;
}
return TRUE;
}
BOOL CTrueColorToolBar::SetTrueColorToolBar(UINT uToolBarType,UINT uToolBar,int nBtnWidth)
...{
CImageList cImageList;
CBitmap cBitmap;
BITMAP bmBitmap;
CSize cSize;
int nNbBtn;
if (!cBitmap.Attach(LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(uToolBar), IMAGE_BITMAP, 0, 0,LR_DEFAULTSIZE|LR_CREATEDIBSECTION)) ||
!cBitmap.GetBitmap(&bmBitmap))
return FALSE;
cSize = CSize(bmBitmap.bmWidth, bmBitmap.bmHeight);
nNbBtn = cSize.cx/nBtnWidth;
RGBTRIPLE* rgb = (RGBTRIPLE*)(bmBitmap.bmBits);
COLORREF rgbMask = RGB(rgb[0].rgbtRed,
rgb[0].rgbtGreen,
rgb[0].rgbtBlue);
if (!cImageList.Create(nBtnWidth, cSize.cy,
ILC_COLOR24|ILC_MASK, //! 因为我的BMP文件是24位的,所以此处为ILC_COLOR24
nNbBtn, 0))
return FALSE;
if (cImageList.Add(&cBitmap, rgbMask) == -1)
return FALSE;
SendMessage(uToolBarType, 0, (LPARAM)cImageList.m_hImageList);
cImageList.Detach();
cBitmap.Detach();
return TRUE;
}
在MainFrm.cpp的OnCreate函数中增加如下代码:
if (!m_wndToolBar.Create(this, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC)
|| !m_wndToolBar.LoadToolBar(IDR_TOOLBOX))
...{
TRACE0("未能创建工具栏 ");
return -1; // 未能创建
}
m_wndToolBar.LoadTrueColorToolBar(32,
IDB_TOOLBOX_NORMAL, IDB_TOOLBOX_HOT, IDB_TOOLBOX_DISABLE);
m_wndToolBar是在头文件MainFrm.h中声明的CTrueColorToolBar的对象。在OnCreate函数的返回语句之前加上:
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
//! 完成工具栏的任意停靠
至此,自定义工具条就出炉了。当然要记得在资源文件中将工具条的大小要设置的和实际图片尺寸对应。以及资源视图中工具条的按钮个数也要对应。否则显示上会不协调。下次自己要将工具条要做的更好看、更人性化。
加载真彩色图片类的头文件:
class CTrueColorToolBar : public CToolBar
...{
// Construction
public:
CTrueColorToolBar();
// Operations
public:
BOOL LoadTrueColorToolBar(int nBtnWidth,UINT uToolBar,
UINT uToolBarHot,UINT uToolBarDisabled = 0);
private:
BOOL SetTrueColorToolBar(UINT uToolBarType,
UINT uToolBar,
int nBtnWidth);
// Implementation
public:
virtual ~CTrueColorToolBar();
protected:
//{{AFX_MSG(CTrueColorToolBar)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
实现文件:
#include "TrueColorToolBar.h"
/**//////////////////////////////////////////////////////////////////////////////
// CTrueColorToolBar
CTrueColorToolBar::CTrueColorToolBar()
...{
}
CTrueColorToolBar::~CTrueColorToolBar()
...{
}
BEGIN_MESSAGE_MAP(CTrueColorToolBar, CToolBar)
//{{AFX_MSG_MAP(CTrueColorToolBar)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/**//////////////////////////////////////////////////////////////////////////////
// CTrueColorToolBar message handlers
BOOL CTrueColorToolBar::LoadTrueColorToolBar(int nBtnWidth, UINT uToolBar,UINT uToolBarHot,UINT uToolBarDisabled)
...{
if (!SetTrueColorToolBar(TB_SETIMAGELIST, uToolBar, nBtnWidth))
return FALSE;
if (!SetTrueColorToolBar(TB_SETHOTIMAGELIST, uToolBarHot, nBtnWidth))
return FALSE;
if (uToolBarDisabled) ...{
if (!SetTrueColorToolBar(TB_SETDISABLEDIMAGELIST, uToolBarDisabled, nBtnWidth))
return FALSE;
}
return TRUE;
}
BOOL CTrueColorToolBar::SetTrueColorToolBar(UINT uToolBarType,UINT uToolBar,int nBtnWidth)
...{
CImageList cImageList;
CBitmap cBitmap;
BITMAP bmBitmap;
CSize cSize;
int nNbBtn;
if (!cBitmap.Attach(LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(uToolBar), IMAGE_BITMAP, 0, 0,LR_DEFAULTSIZE|LR_CREATEDIBSECTION)) ||
!cBitmap.GetBitmap(&bmBitmap))
return FALSE;
cSize = CSize(bmBitmap.bmWidth, bmBitmap.bmHeight);
nNbBtn = cSize.cx/nBtnWidth;
RGBTRIPLE* rgb = (RGBTRIPLE*)(bmBitmap.bmBits);
COLORREF rgbMask = RGB(rgb[0].rgbtRed,
rgb[0].rgbtGreen,
rgb[0].rgbtBlue);
if (!cImageList.Create(nBtnWidth, cSize.cy,
ILC_COLOR24|ILC_MASK, //! 因为我的BMP文件是24位的,所以此处为ILC_COLOR24
nNbBtn, 0))
return FALSE;
if (cImageList.Add(&cBitmap, rgbMask) == -1)
return FALSE;
SendMessage(uToolBarType, 0, (LPARAM)cImageList.m_hImageList);
cImageList.Detach();
cBitmap.Detach();
return TRUE;
}
在MainFrm.cpp的OnCreate函数中增加如下代码:
if (!m_wndToolBar.Create(this, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC)
|| !m_wndToolBar.LoadToolBar(IDR_TOOLBOX))
...{
TRACE0("未能创建工具栏 ");
return -1; // 未能创建
}
m_wndToolBar.LoadTrueColorToolBar(32,
IDB_TOOLBOX_NORMAL, IDB_TOOLBOX_HOT, IDB_TOOLBOX_DISABLE);
m_wndToolBar是在头文件MainFrm.h中声明的CTrueColorToolBar的对象。在OnCreate函数的返回语句之前加上:
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
//! 完成工具栏的任意停靠
至此,自定义工具条就出炉了。当然要记得在资源文件中将工具条的大小要设置的和实际图片尺寸对应。以及资源视图中工具条的按钮个数也要对应。否则显示上会不协调。下次自己要将工具条要做的更好看、更人性化。
相关文章推荐
- MFC使用自定义资源加载PNG
- MFC使用自定义资源加载PNG
- MFC 标题栏动态加载自定义图标
- MFC使用自定义资源加载PNG
- MFC使用自定义资源加载PNG
- MFC使用自定义资源加载PNG
- MFC自定义消息
- 仿百度壁纸客户端(四)——自定义上拉加载实现精选壁纸墙
- MFC的自定义消息(用户消息)的定义与使用
- 自定义BitmapTransformation完美解决Glide加载圆角或者圆形图片
- 用ListView来展示自定义圆形控件(加载的是本地图片)
- MFC窗口创建以及工具条的浮动
- MFC加载exe资源,程序调用exe功能
- javawebday69(类加载器 自定义 Tomcat类加载器)
- halcon加载图片到mfc
- Android UI--自定义ListView(实现下拉刷新+加载更多)
- C#中自定义消息,与MFc对比
- MFC中,如何自定义用户消息
- Android 自定义加载中
- android界面之美---自定义网络请求进度加载对话框