怎样制作工具按钮


工具按钮:

怎样将工具条放入普通对话框中,并且还能访问所有一般的工具条特征,如工具提示和工具条编辑器呢?
这是我自制的一个工具棒的源程序,“通用电脑语音系统”就是用的这个文件,用CmyToolBar类仅用两个函数就可以在普通对话框中使用工具条。调用工具条的过程如下:

先用VC5资源管理器定制工具条,并设置ID为:IDR_TOOLBAR_WAV,

CMyToolBar m_toolbar; //声明类实例

CDialog::OnInitDialog()//在对话框初始化时运行
{
CRect r(0,0,0,0);
m_toolbar.Create(WS_BORDER|WS_VISIBLE|WS_CHILD|CCS_TOP|
CCS_ADJUSTABLE|TBSTYLE_TOOLTIPS,r,this,IDR_TOOLBAR_WAV);
m_toolbar.LoadToolBar(IDR_TOOLBAR_WAV);
m_toolbar.AutoSize();
}


以下是源程序--MyToolBar.h:

/*自生成工具棒类,可以用VC资源编辑外形,然后在任何对话框中使用*/
//////////////////////////////////////////////////////////////////////////////
// CMyToolBar window

class CMyToolBar : public CToolBarCtrl
{
// Construction
public:
CMyToolBar();
~CMyToolBar();

// Implementation
public:
BOOL SetButtons(const UINT* lpIDArray, int nIDCount);
BOOL LoadToolBar(UINT nIDResource);

// Generated message map functions
protected:
//{{AFX_MSG(CMyToolBar)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG
afx_msg void OnNeedText(NMHDR * pNotifyStruct,LRESULT* result);
DECLARE_MESSAGE_MAP()
};


/////////////////////////////////////////////////////////////////////////////
以下是源程序--MyToolBar.cpp:
/////////////////////////////////////////////////////////////////////////////
MyToolBar.cpp

// MyToolBar.cpp : implementation file
//

#include "stdafx.h"
#include "MyToolBar.h"
#include <afxpriv.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMyToolBar
CMyToolBar::CMyToolBar()
{
}

CMyToolBar::~CMyToolBar()
{
}
struct CToolBarData
{
WORD wVersion;
WORD wWidth;
WORD wHeight;
WORD wItemCount;
//WORD aItems[wItemCount]

WORD* items()
{ return (WORD*)(this+1); }
};


BEGIN_MESSAGE_MAP(CMyToolBar, CToolBarCtrl)
//{{AFX_MSG_MAP(CMyToolBar)
ON_NOTIFY (TTN_NEEDTEXT,0,OnNeedText)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyToolBar message handlers

BOOL CMyToolBar::LoadToolBar(UINT nIDResource)
{
ASSERT_VALID(this);
ASSERT(nIDResource!=0);
HINSTANCE hInst=AfxGetInstanceHandle();
HRSRC hRsrc=::FindResource(hInst,MAKEINTRESOURCE(nIDResource),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)
return FALSE;
ASSERT(pData->wVersion==1);
if(AddBitmap(pData->wItemCount,nIDResource)==-1)
{
UnlockResource(hGlobal);
FreeResource(hGlobal);
return FALSE;
}
UINT* pItems=new UINT[pData->wItemCount];
for(int i=0;i<pData->wItemCount;i++)
pItems[i]=pData->items()[i];
BOOL bResult=SetButtons(pItems,pData->wItemCount);
delete[] pItems;
if(bResult)
{
CSize sizeImage(pData->wWidth,pData->wHeight);
CSize sizeButton(pData->wWidth+7,pData->wHeight+7);
SetBitmapSize(sizeImage);
SetButtonSize(sizeButton);
}
UnlockResource(hGlobal);
FreeResource(hGlobal);
return bResult;
}
BOOL CMyToolBar::SetButtons(const UINT * lpIDArray, int nIDCount)
{
ASSERT_VALID(this);
ASSERT(nIDCount >= 1); // must be at least one of them
ASSERT(lpIDArray == NULL ||
AfxIsValidAddress(lpIDArray, sizeof(UINT) * nIDCount, FALSE));

// delete all existing buttons
int nCount = (int)DefWindowProc(TB_BUTTONCOUNT, 0, 0);
while (nCount--)
VERIFY(DeleteButton(0));

if (lpIDArray != NULL)
{
// add new buttons to the common control
TBBUTTON button; memset(&button, 0, sizeof(TBBUTTON));
int iImage = 0;
for (int i = 0; i < nIDCount; i++)
{
button.fsState = TBSTATE_ENABLED;
if ((button.idCommand = *lpIDArray++) == 0)
{
// separator
button.fsStyle = TBSTYLE_SEP;
// width of separator includes 8 pixel overlap
button.iBitmap = 8;
}
else
{
// a command button with image
button.fsStyle = TBSTYLE_BUTTON;
button.iBitmap = iImage++;
}
if (!DefWindowProc(TB_ADDBUTTONS, 1, (LPARAM)&button))
return FALSE;
}
}
else
{
// add 'blank' buttons
TBBUTTON button; memset(&button, 0, sizeof(TBBUTTON));
button.fsState = TBSTATE_ENABLED;
for (int i = 0; i < nIDCount; i++)
{
ASSERT(button.fsStyle == TBSTYLE_BUTTON);
if (!DefWindowProc(TB_ADDBUTTONS, 1, (LPARAM)&button))
return FALSE;
}
}
return TRUE;
}
void CMyToolBar::OnNeedText(NMHDR * pNotifyStruct,LRESULT* result)
{
LPTOOLTIPTEXT lpTipText=(LPTOOLTIPTEXT) pNotifyStruct;
UINT nStringID=lpTipText->hdr.idFrom;
TCHAR szFullText[256];
CString StrTipText;
AfxLoadString(nStringID,szFullText);
AfxExtractSubString(StrTipText,szFullText,1,'\n');
strcpy(lpTipText->lpszText,StrTipText);
*result=TRUE;
}