在 Toolbar 上加上汉字显示,却始终显示不出来,但我用GetButtonText可以获得那些汉字。请问可能原因是什么?

解决方案 »

  1.   

    不能直接使用Toolbar 加汉字,要从Toolbar 上继承一个类扩充功能,才能显示汉字
      

  2.   

    class CImageToolBar : public CToolBar
    {
    public:
    int GetHeight();
    void AttachToolBarImages(UINT nNormalImageID,
     UINT nDisabledImageID,
     UINT nHotImageID);
    void AddButtonText(const std::vector<CString>& vecButtonText, BOOL bRecalc = TRUE);private:
    void _MakeToolBarImageList(UINT nBitmapID, CImageList& outImageList);
    void _ReplaceBackgroundColor(CBitmap& ioBM);private:
    CImageList _imageListNormal;
    CImageList _imageListDisabled;
    CImageList _imageListHot;

    // Construction
    public:
    CImageToolBar();// Attributes
    public:// Operations
    public:// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CImageToolBar)
    //}}AFX_VIRTUAL// Implementation
    public:
    virtual ~CImageToolBar(); // Generated message map functions
    protected:
    //{{AFX_MSG(CImageToolBar)
    // NOTE - the ClassWizard will add and remove member functions here.
    //}}AFX_MSG DECLARE_MESSAGE_MAP()
    };
      

  3.   

    /////////////////////////////////////////////////////////////////////////////
    //
    static const int kImageWidth (35);
    static const int kImageHeight (35);
    static const int kNumImages (11);
    static const UINT kToolBarBitDepth (ILC_COLOR24);
    static const RGBTRIPLE kBackgroundColor = {192, 192, 192};
    //////////////////////////////////////////////////////////////////////////
    CImageToolBar::CImageToolBar()
    {
    }CImageToolBar::~CImageToolBar()
    {
    }int CImageToolBar::GetHeight()
    {
    CRect rect;
    GetToolBarCtrl().GetWindowRect(&rect); return rect.Height();
    }void CImageToolBar::AttachToolBarImages(UINT nNormalImageID, UINT nDisabledImageID, UINT nHotImageID)
    {
    // make high-color image lists for each of the bitmaps
    _MakeToolBarImageList (nNormalImageID, _imageListNormal);
    _MakeToolBarImageList (nDisabledImageID, _imageListDisabled);
    _MakeToolBarImageList (nHotImageID, _imageListHot); // get the toolbar control associated with the CToolbar object
    CToolBarCtrl& barCtrl = GetToolBarCtrl(); // attach the image lists to the toolbar control
    barCtrl.SetImageList (&_imageListNormal);
    barCtrl.SetDisabledImageList (&_imageListDisabled);
    barCtrl.SetHotImageList (&_imageListHot);
    }void CImageToolBar::AddButtonText(const std::vector<CString>& vecButtonText, BOOL bRecalc /* = TRUE */)
    {
    CSize sizeMax;
    if(bRecalc)
    {
    sizeMax.cx = 0;
    sizeMax.cy = 0; // 取得最大尺寸
    CRect rect;
    for (int i = 0; i < GetCount(); ++i)
    {
    SetButtonText(i, vecButtonText[i]);
    GetItemRect(i, rect);

    sizeMax.cx = __max(rect.Size().cx, sizeMax.cx);
    sizeMax.cy = __max(rect.Size().cy, sizeMax.cy);
    }
    }

    //设置按钮尺寸
    SIZE sizeButton, sizeImage;
    sizeButton.cx = sizeMax.cx;
    sizeButton.cy = sizeMax.cy;
    sizeImage.cx = 32;
    sizeImage.cy = 32;
    SetSizes(sizeButton, sizeImage); RedrawWindow();
    }void CImageToolBar::_MakeToolBarImageList(UINT nBitmapID, CImageList& outImageList)
    {
    CBitmap bm; // if we use CBitmap::LoadBitmap() to load the bitmap, the colors
    // will be reduced to the bit depth of the main screen and we won't
    // be able to access the pixels directly. To avoid those problems,
    // we'll load the bitmap as a DIBSection instead and attach the
    // DIBSection to the CBitmap.
    VERIFY (bm.Attach (::LoadImage (::AfxFindResourceHandle(
    MAKEINTRESOURCE (nBitmapID), RT_BITMAP),
    MAKEINTRESOURCE (nBitmapID), IMAGE_BITMAP, 0, 0,
    (LR_DEFAULTSIZE | LR_CREATEDIBSECTION)))); // replace the specified color in the bitmap with the user's
    // button color
    //::_ReplaceBackgroundColor (bm); // create a 24 bit image list with the same dimensions and number
    // of buttons as the toolbar
    VERIFY (outImageList.Create (
    kImageWidth, kImageHeight, kToolBarBitDepth |ILC_MASK, kNumImages, 0));
    outImageList.SetBkColor(::GetSysColor(COLOR_BTNFACE)); // attach the bitmap to the image list
    VERIFY (outImageList.Add (&bm, RGB(214, 209, 198)) != -1);
    }void CImageToolBar::_ReplaceBackgroundColor(CBitmap& ioBM)
    {
    // figure out how many pixels there are in the bitmap
    BITMAP bmInfo; VERIFY (ioBM.GetBitmap (&bmInfo));

    // add support for additional bit depths here if you choose
    VERIFY (bmInfo.bmBitsPixel == 8);
    VERIFY (bmInfo.bmWidthBytes == bmInfo.bmWidth); const UINT numPixels (bmInfo.bmHeight * bmInfo.bmWidth); // get a pointer to the pixels
        DIBSECTION  ds;    VERIFY (ioBM.GetObject (sizeof (DIBSECTION), &ds) == sizeof (DIBSECTION)); RGBTRIPLE* pixels = reinterpret_cast<RGBTRIPLE*>(ds.dsBm.bmBits);
    VERIFY (pixels != NULL); // get the user's preferred button color from the system
    const COLORREF buttonColor (::GetSysColor (COLOR_BTNFACE));
    const RGBTRIPLE userBackgroundColor = {
    GetBValue (buttonColor), GetGValue (buttonColor), GetRValue (buttonColor)}; // search through the pixels, substituting the user's button
    // color for any pixel that has the magic background color
    for (UINT i = 0; i < numPixels; ++i)
    {
    if (pixels [i].rgbtBlue == kBackgroundColor.rgbtBlue &&
    pixels [i].rgbtGreen == kBackgroundColor.rgbtGreen &&
    pixels [i].rgbtRed == kBackgroundColor.rgbtRed)
    {
    pixels [i] = userBackgroundColor;
    }
    }
    }BEGIN_MESSAGE_MAP(CImageToolBar, CToolBar)
    //{{AFX_MSG_MAP(CImageToolBar)
    // NOTE - the ClassWizard will add and remove mapping macros here.
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()
      

  4.   

    生成toolbar的create函数,指定TB_AUTOSIZE 或者TBSTYLE_LIST属性了没有
    是style属性设置的问题吧,呵呵