用下面函数创建可以直接指定位图: HWND CreateToolbarEx( HWND hwnd, DWORD ws, UINT wID, int nBitmaps, HINSTANCE hBMInst, UINT wBMID, //Resource identifier for the bitmap resource. LPCTBBUTTON lpButtons, int iNumButtons, int dxButton, int dyButton, int dxBitmap, int dyBitmap, UINT uStructSize );
需要包含#include "commctrl.h" ,工程连接要用到comctl32.lib.
to laiyiling(【龙工一号●CSDN】) : CreateToolbarEx只能加载指定的位图而不是资源里做好的TOOLBAR
看了一下MFC,SDK加载资源中的工具条需要做如下手脚:struct CToolBarData { WORD wVersion; WORD wWidth; WORD wHeight; WORD wItemCount; //WORD aItems[wItemCount]
HWND CreateToolbarEx(
HWND hwnd,
DWORD ws,
UINT wID,
int nBitmaps,
HINSTANCE hBMInst,
UINT wBMID, //Resource identifier for the bitmap resource.
LPCTBBUTTON lpButtons,
int iNumButtons,
int dxButton,
int dyButton,
int dxBitmap,
int dyBitmap,
UINT uStructSize
);
CreateToolbarEx只能加载指定的位图而不是资源里做好的TOOLBAR
{
WORD wVersion;
WORD wWidth;
WORD wHeight;
WORD wItemCount;
//WORD aItems[wItemCount]
WORD* items()
{ return (WORD*)(this+1); }
};BOOL LoadToolBar(LPCTSTR lpszResourceName)
{
if(lpszResourceName == NULL)
return FALSE;
HWND hWnd == CreateToolbarEx(...);//创建一个空的TOOLBAR
HBITMAP hbm = LoadBitmap(hInst,lpszResourceName); HRSRC hRsrc = FindResource(hInst,lpszResourceName,RT_TOOLBAR);
if(hRsrc == NULL)
return FALSE;
HGLOBAL hGlobal = LoadResource(hInst, hRsrc);
if(hGlobal == NULL)
return FALSE;
CToolBarData* pData = (CToolBarData*)LockResource(hGlobal);
if(pData == NULL || pData->wVersion != 1)
return FALSE;
BOOL bResult = FALSE;
if(hbm)
{
//置位图图象
bResult = TRUE;
SendMessage(hWnd,TB_SETBITMAPSIZE,0,MAKELONG(pData->wWidth,pData->wHeight))
TBADDBITMAP tbab;
tbab.hInst = hInst;
tbab.nID = (UINT)hbm;
SendMessage(hWnd,TB_ADDBITMAP,1,(LPARAM)&tbab);
DeleteObject(hbm);
} TBBUTTON tb;
memset(&tb,0,sizeof(TBBUTTON));
tb.iString = -1;
int iImg = 0;
//依次加入按钮
for(int i = 0; i < pData->wItemCount; i++)
{
tb.fsState = TBSTATE_ENABLED;
if((tb.idCommand = pData->items()[i]) == 0)
{
// separator
tb.fsStyle = TBSTYLE_SEP;
// width of separator includes 8 pixel overlap
if ((GetStyle() & TBSTYLE_FLAT))
tb.iBitmap = 6;
else
tb.iBitmap = 8;
}
else
{
// a command button with image
tb.fsStyle = TBSTYLE_BUTTON;
tb.iBitmap = iImg ++;
}
if(!SendMessage(m_hWnd,TB_ADDBUTTONS,1,(LPARAM)&tb))
return FALSE;
}
SendMessage(hWnd,TB_SETBUTTONSIZE,0,MAKELPARAM(pData->wWidth + 7, pData->wHeight + 7);
UnlockResource(hGlobal);
FreeResource(hGlobal); return bResult;
}