如何为 ToolBar 里的按扭 设置文字很多程序的ToolBar 里的按扭 都有说明文字 (说明文字一般都在按扭左边 OR 下边 )请问如何实现啊给代码;

解决方案 »

  1.   

    请问你说的是什么文字?如果是ToolTip可以直接设置啊。如果是显式的文字,画ToolBar按钮的时候把文字画上去。
      

  2.   

    //工具条中加入文本名称
    主要函数
    1.CToolBar::SetSizes(SIZE sizeButton,SIZE sizeImage)
    用于设置工具按钮的大小以及它上面的位图的大小.这个函数有两个参数,其中,sizeButton用于指定按钮的大小,sizeImage用于指定位图的大小.需注意的是:这两个参数的cx域和cy域都必须大于零,并且,sizeButton的宽度(cx)至少要比sizeImage的宽度大7,高度(cy)至少大6.2.CToolBar::SetButtonText(int nIndex , LPCTSTR lpstText)
    这个函数用于设置工具按钮上所显示的文字,nIndex用于指定所设置的工具按钮在工具栏中基于0的索引值(包括分割符),即第nIndex+1个工具按钮将被设置,lpstText就是指定所要显示的文字了.3.CToolBar::EnableToolTips(BOOL bEnable)
    这个函数用于设置/取消工具按钮的ToolTips功能.4.CMainFrame::ShowControlBar(CControlBar * pBar,BOOL bShow, BOOL bDelay)
    这个函数用于设置工具栏或状态栏的显示与否.
    pBar: 指向被设置工具栏或状态栏对象的指针
    bShow: 为TRUE,则显示工具栏或状态栏 否则隐藏;
    bDelay: 为TRUE,则延迟显示时间,否则立刻显示工具栏或状态栏实现
    1.添加变量
    SIZE类型的成员变量m_sizeMax,用于存储按钮的最大尺寸2.在CMainFrame类的实现文件开始部分加如静态数组,用于存放文本标签,如下:
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    static TCHAR *TextTips[] = {"新建","打开","存盘","","剪切","复制","粘贴","","打印","","关于",""};//此句为添加3.用ClassWizard为CMainFrame添加一成员函数:void CMainFrame::UpdateToolBar();用于更新工具条
    void CMainFrame::UpdateToolBar()
    {
            CRect rect;
    SIZE sizeButton,sizeImage;
    m_sizeMax.cx = 0;
    m_sizeMax.cy = 0;
    //取得最大尺寸
    for (int i=0;i<m_wndToolBar.GetCount();i++)
    {
    m_wndToolBar.SetButtonText(i,TextTips[i]);
    m_wndToolBar.GetItemRect(i,rect);
    m_sizeMax.cx = __max(rect.Size().cx, m_sizeMax.cx);
    m_sizeMax.cy = __max(rect.Size().cy, m_sizeMax.cy);
    }//设置按钮尺寸sizeButton.cx = m_sizeMax.cx;
    sizeButton.cy = m_sizeMax.cy;
    sizeImage.cx = 16;
    sizeImage.cy = 15;
    m_wndToolBar.SetSizes(sizeButton,sizeImage);
    ShowControlBar(&m_wndToolBar,FALSE,FALSE);
    ShowControlBar(&m_wndToolBar,TRUE,FALSE);
    m_wndToolBar.RedrawWindow();}4.在CMainFrame::OnCreate()函数中,就在return 0;之前加入如下代码:
    UpdateToolBar();///更新工具条
      

  3.   

    #include "stdafx.h"
    #include "E_Speaking_Client.h"
    #include "MainFrm.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #endifIMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
    ON_WM_CREATE()
    END_MESSAGE_MAP()static UINT indicators[] =
    {
    ID_SEPARATOR,           
    ID_INDICATOR_CAPS,
    ID_INDICATOR_NUM,
    ID_INDICATOR_SCRL,
    };CMainFrame::CMainFrame()
    {
    }CMainFrame::~CMainFrame()
    {
    }int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
    CImageList img; if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
    return -1; if(!m_wndReBar.Create(this))
    {
    return -1;
    } if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_BOTTOM
    | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
    !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
    {
    TRACE0("未能创建工具栏\n");
    return -1;      
    } //m_wndToolBar.GetToolBarCtrl().SetButtonWidth(40, 80);
    //m_wndToolBar.SetSizes( CSize( 33,38 ), CSize( 32,32 ) );
    img.Create( 32, 32, ILC_COLOR8|ILC_MASK,3,3 );
    img.SetBkColor( ::GetSysColor(COLOR_BTNFACE) );
    img.Add( AfxGetApp()->LoadIcon(IDI_ICON5) );
    img.Add( AfxGetApp()->LoadIcon(IDI_ICON3) );
    img.Add( AfxGetApp()->LoadIcon(IDI_ICON1) ); m_wndToolBar.GetToolBarCtrl().SetHotImageList( &img );
    img.Detach();

    img.Create( 32, 32, ILC_COLOR8|ILC_MASK,3,3 );
    img.SetBkColor( ::GetSysColor(COLOR_BTNFACE) );
    img.Add( AfxGetApp()->LoadIcon(IDI_ICON5) );
    img.Add( AfxGetApp()->LoadIcon(IDI_ICON3) );
    img.Add( AfxGetApp()->LoadIcon(IDI_ICON1) ); m_wndToolBar.GetToolBarCtrl().SetImageList( &img );
    img.Detach(); /*if (!m_wndStatusBar.Create(this) ||
    !m_wndStatusBar.SetIndicators(indicators,
      sizeof(indicators)/sizeof(UINT)))
    {
    TRACE0("未能创建状态栏\n");
    return -1;      
    }*/ UpdateToolBar();
    return 0;
    }void CMainFrame::UpdateToolBar()
    {
    static TCHAR *TextTips[] = { "设置","查询","喜欢" };
    CRect rect;
    SIZE sizeButton,sizeImage;
    static CSize m_sizeMax;
    m_sizeMax.cx = 0;
    m_sizeMax.cy = 0; for( int i=0; i < m_wndToolBar.GetCount(); i++ )
    {
    m_wndToolBar.SetButtonText( i, TextTips[i] );
    m_wndToolBar.GetItemRect( i, rect);
    m_sizeMax.cx = __max( rect.Size().cx, m_sizeMax.cx );
    m_sizeMax.cy = __max( rect.Size().cy, m_sizeMax.cy );
    } sizeButton.cx = m_sizeMax.cx+3;
    sizeButton.cy = m_sizeMax.cy;
    sizeImage.cx = 32;
    sizeImage.cy = 32;

    m_wndToolBar.SetSizes( sizeButton, sizeImage );
    ShowControlBar( &m_wndToolBar,FALSE,FALSE );
    ShowControlBar( &m_wndToolBar,TRUE,FALSE );
    m_wndToolBar.RedrawWindow();
    }BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
    {
    if( !CFrameWnd::PreCreateWindow(cs) )
    return FALSE;
    cs.x = 830;
    cs.y = 0;
        cs.cx = 133;
    cs.cy = 690;
    cs.style = WS_OVERLAPPED | WS_CAPTION | FWS_ADDTOTITLE
         | WS_THICKFRAME | WS_SYSMENU;
        cs.dwExStyle = WS_EX_STATICEDGE;
    return TRUE;
    }#ifdef _DEBUG
    void CMainFrame::AssertValid() const
    {
    CFrameWnd::AssertValid();
    }void CMainFrame::Dump(CDumpContext& dc) const
    {
    CFrameWnd::Dump(dc);
    }#endif 
      

  4.   

    http://www.codeguru.com/Cpp/controls/toolbar/miscellaneous/article.php/c2517/
    http://www.codeguru.com/Cpp/controls/toolbar/placingcontrolsintoolbars/article.php/c5493/