代码如下:我新建了一个类,然后在头文件中声明了
int m_ButtonCount;
CImageList* imagelist;// CustomBar.cpp : implementation file
//#include "stdafx.h"
#include "gongjulan.h"
#include "CustomBar.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CGongjulanApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CCustomBarCCustomBar::CCustomBar()
{
imagelist=new CImageList();
}CCustomBar::~CCustomBar()
{
delete imagelist;
}
BEGIN_MESSAGE_MAP(CCustomBar, CToolBarCtrl)
//{{AFX_MSG_MAP(CCustomBar)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CCustomBar message handlersBOOL CCustomBar::Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID) 
{
// TODO: Add your specialized code here and/or call the base class
CToolBarCtrl::Create(dwStyle,rect,pParentWnd,nID);
m_ButtonCount=6;   //定义工具栏按钮数量
SetBitmapSize(CSize(32,32));
imagelist->Create(32,32,ILC_COLOR32|ILC_MASK,0,0);//创建图像列表对象
for(int n=0;n<13;n++)//添加图标
{
imagelist->Add(theApp.LoadIcon(n+IDI_ICON1));
}
SetImageList(imagelist);//将图像关联到工具栏上
TBBUTTON* m_pButtons;
m_pButtons=new TBBUTTON[m_ButtonCount];
for(int i=0;i<m_ButtonCount;i++)
{
CString string;
string.LoadString(i+IDS_DIYI);
int stringlength=string.GetLength()+1;
TCHAR* char1=string.GetBufferSetLength(stringlength);
char1[stringlength]=0;
char1[stringlength-1]=0;

VERIFY((m_pButtons[i].iString=AddStrings(char1))!=-1);
m_pButtons[i].fsState=TBSTATE_ENABLED;
m_pButtons[i].dwData=0;
m_pButtons[i].fsStyle=TBSTYLE_BUTTON;
m_pButtons[i].iBitmap=i;
m_pButtons[i].idCommand=i+IDS_DIYI;

string.ReleaseBuffer();
}
m_pButtons[i-1].idCommand=IDCANCEL;
//=========================================================================
TBBUTTON b;//设置分隔条
b.idCommand=0;
b.fsStyle=TBSTYLE_SEP;
b.fsState=TBSTATE_ENABLED;
b.iString=0;
b.iBitmap=0;
b.dwData=0;
for(int j=0;j<m_ButtonCount;j++)//在程序里添加工具栏
{
VERIFY(AddButtons(1,&m_pButtons[j]));
if(!((j+1)%3))
{
VERIFY(AddButtons(1,&b));
}
}
return true;
//return CWnd::Create(dwStyle, rect, pParentWnd, nID, pContext);
}
然后在主窗口调用:
在C******DLG头文件里声明CCustomBar toolbat;
BOOL CGongjulanDlg::OnInitDialog()
{
//………………………………………………………………
CDialog::OnInitDialog();
         toolbar.Create(TBSTYLE_FLAT|CCS_TOP|WS_CHILD|WS_VISIBLE|WS_BORDER|CCS_ADJUSTABLE|
TBSTYLE_WRAPABLE,CRect(0,0,0,0),this,IDR_TOOLBAR1);
         return TRUE;  // return TRUE  unless you set the focus to a control
}我加入了一个菜单栏,菜单栏上有三个选项,并把这三个选项添加到String Table里,分别为
ID                  Value                     Caption
IDS_DIYI            201                       第一个
IDS_DIER            202                       第二个
IDS_DISAN           203                       第三个现在编译能通过,但是运行后工具栏只有图标,图标下面没有文字!!
我的程序的意思是让图标下面显示“第一个  第二个  第三个”

解决方案 »

  1.   

    TCHAR* char1=string.GetBufferSetLength(stringlength);
    这句话有问题。怎么能这样赋值给一个TCHAR指针的。首先你TCHAR* char1 要new 几个值出来 new m_ButtonCount个吧。
    TCHAR* char1 = new TCHAR[250];
    ...然后你要这样赋值给char1
    lstrcpy(TCHAR* char1, string.GetBufferSetLength(stringlength));CCustomBar 销毁或者析构的时候,记得delete掉那几个new 出来的指针。
      

  2.   

    我先声明TCHAR* char1 ,然后在构造函数里加入char1 = new TCHAR[250]; 
    析构函数里加入delete char1;在代码里用lstrcpy(char1, string.GetBufferSetLength(stringlength)); 替换掉你说的那句~~
    lstrcpy(TCHAR* char1,这个地方有点问题,编译不通过,char1已经在上面定义过了,我删除了TCHAR*才能编译通过~~
    但是执行后~~和没改的时候一样~~还是不显示我要的文字啊~~~
      

  3.   


    CString string;
    //string.LoadString(i+IDS_DIYI);//可能问题出在这句上
    /*
    int stringlength=string.GetLength()+1;
    TCHAR* char1=string.GetBufferSetLength(stringlength);

    char1[stringlength]=0;
    char1[stringlength-1]=0;*/
                    //注释掉上面那些,把下面这句改成这样就能显示eeee                m_pButtons[i].iString=AddStrings("eeee");难道要我一句一句的网上写么?//string.LoadString(i+IDS_DIYI)这句有问题是不是没有载入IDS_DIYI的字串啊?
      

  4.   

    CString string;
    string.LoadString(i+IDS_DIYI);m_pButtons[i].iString=AddStrings(string);这样不行吗?
      

  5.   

      
    for(int i=0;i<m_ButtonCount;i++)
    {
    /*CString string;
    //string.LoadString(i+IDS_DIYI);
    string.Format("asdf");
    int stringlength=string.GetLength()+1;
    TCHAR* char1=string.GetBufferSetLength(stringlength);

    char1[stringlength]=0;
    char1[stringlength-1]=0;*/

    m_pButtons[0].iString=AddStrings("第一个");
    m_pButtons[1].iString=AddStrings("第二个");
    m_pButtons[i].iString=AddStrings("其他");
    m_pButtons[i].fsState=TBSTATE_ENABLED;
    m_pButtons[i].dwData=0;
    m_pButtons[i].fsStyle=TBSTYLE_BUTTON;
    m_pButtons[i].iBitmap=i;
    m_pButtons[i].idCommand=i+IDS_DIYI;

    //string.ReleaseBuffer();
    }我改成这样了……必须每个都设置一下……我在哪出现错误了?
    为什么string.LoadString(i+IDS_DIYI);这句没有加载到IDS_DIYI里的字串?我在字串表里加入它了啊!
      

  6.   

    CString string;
    string.LoadString(i+IDS_DIYI);m_pButtons[i].iString=AddStrings(string);这样不行,还是没有文字~
      

  7.   

    你rebuilt all 一下试试吧。如果是你后来改的资源。需要rebuilt all 重新编译所有文件,才会连接到资源的。
      

  8.   

    rebuilt all了…………
    还是不显示文字……
      

  9.   

    You must ensure that the last string has two null terminators. To properly format a constant string, you might write it as:// one null added automatically
    lpszStrings = "Only one string to add\0";or:// adds three strings with one call
    lpszStrings = "String 1\0String 2\0String 3\0";You should not pass a CString object to this function since it is not possible to have more than one null character in a CString.看来传CString是不行的了。
      

  10.   

    CString string;
            string.LoadString(i+IDS_DIYI);
            int stringlength=string.GetLength()+1;
            TCHAR* char1 = new[stringlength];
            lstrcpy(char1, string.GetBufferSetLength(stringlength));
            char1[stringlength-1]=0;
            char1[stringlength]=0;
            
            VERIFY((m_pButtons[i].iString=AddStrings(char1))!=-1);
            m_pButtons[i].fsState=TBSTATE_ENABLED;
            m_pButtons[i].dwData=0;
            m_pButtons[i].fsStyle=TBSTYLE_BUTTON;
            m_pButtons[i].iBitmap=i;
            m_pButtons[i].idCommand=i+IDS_DIYI;
            
            delete[] char1;
            string.ReleaseBuffer();这样应该可以吧
      

  11.   

    我很郁闷啊~~~楼上兄弟给的代码编译不通过,TCHAR* char1 = new[stringlength];
    这句,提示语法错误~~我看不是他没有显示文字,而是我看不到。因为我把字串表的字串变长以后,比如“第一个”改成
    “第一个alsjf阿隆索房间里是老大开发家属楼法律所附近”编译运行后,工具栏的按钮也跟着变长了~~一开始是“____”一下子变“________________________________________”这么长~~
    这样该怎么解决呢?
      

  12.   

    TCHAR* char1 = new[stringlength]; 
    语法是错误了。我打太快,没留意。TCHAR* char1 = new TCHAR[stringlength]; 这样才对。