.h
CToolBar m_ToolBar; CImageList m_Imagelist; afx_msg BOOL OnToolTipText(UINT nID, NMHDR* pNMHDR, LRESULT* pResult); //ToolBar tip
.cpp
m_Imagelist.Create(32,32,ILC_COLOR24|ILC_MASK,1,1); m_Imagelist.SetBkColor(RGB(255,255,255)); m_Imagelist.Add(m_hIcon); m_Imagelist.Add(m_hIcon); m_Imagelist.Add(m_hIcon); UINT array[5]={0};//定义工具栏数组 for(int i=0;i<5;i++) { array[i]=10000+i;//为工具栏中每个按钮添加按钮索引 } m_ToolBar.Create(this);//创建工具栏窗口 m_ToolBar.SetButtons(array,5);//为工具栏添加5个按钮 m_ToolBar.SetButtonText(0,"0000"); m_ToolBar.SetButtonText(1,"1111"); m_ToolBar.SetButtonText(2,"2222"); m_ToolBar.GetToolBarCtrl().SetImageList(&m_Imagelist);//关联图片到按钮上 m_ToolBar.EnableToolTips(TRUE);//激活提示信息 ********************************************************************************************* //添加消息映射(最好写在AFX_MSG_MAP宏外,否则可能出现编译错误 ON_NOTIFY_EX_RANGE(TTN_NEEDTEXT,0,0xFFFF,OnToolTipText) BOOL C******Dlg::OnToolTipText(UINT nID, NMHDR* pNMHDR, LRESULT* pResult) { ASSERT(pNMHDR->code == TTN_NEEDTEXTA || pNMHDR->code == TTN_NEEDTEXTW); // if there is a top level routing frame then let it handle the message if (GetRoutingFrame() != NULL) return FALSE; // to be thorough we will need to handle UNICODE versions of the message also !! TOOLTIPTEXT* pTTTA = (TOOLTIPTEXT*)pNMHDR; TCHAR szFullText[512]; CString strTipText; nID = pNMHDR->idFrom; if (pNMHDR->code == TTN_NEEDTEXTA && (pTTTA->uFlags & TTF_IDISHWND) ) { // idFrom is actually the HWND of the tool nID = ::GetDlgCtrlID((HWND)nID); } if (nID != 0) // will be zero on a separator { AfxLoadString(nID, szFullText); strTipText = szFullText; if (pNMHDR->code == TTN_NEEDTEXT) { lstrcpyn(pTTT->szText, strTipText, sizeof(pTTT->szText)); } else { _mbstowcsz(pTTT->szText, strTipText, sizeof(pTTT->szText)); } *pResult = 0; // bring the tooltip window above other popup windows ::SetWindowPos(pNMHDR->hwndFrom, HWND_TOP, 0, 0, 0, 0, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOOWNERZORDER); return TRUE; } return FALSE; }
提示信息,可以在String Table中添加提示消息。。。来实现ToolBar提示消息。。
头文件中添加#include <afxpriv.h>,否则会提示AfxLoadString错误
第二种
如果自己资源中有 IDR_TOOLBAR1
if(!m_ToolBar.CreateEx(this)||!m_ToolBar.LoadToolBar(IDR_TOOLBAR1)) { return TRUE; } RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);
这样来实现ToolBar 的显示。。