您的位置:首页 > 其它

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);


//! 完成工具栏的任意停靠

至此,自定义工具条就出炉了。当然要记得在资源文件中将工具条的大小要设置的和实际图片尺寸对应。以及资源视图中工具条的按钮个数也要对应。否则显示上会不协调。下次自己要将工具条要做的更好看、更人性化。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: