I recently needed to have 256 color toolbars in my app. If you create 256 color bitmaps in devstudio, and try to use them with CToolbar it won't work. The problem is in the LoadBitmap() member or CToolbar. Appwizard will create the following line in your CMainFrame class: 
m_wndToolBar.LoadBitmap(IDR_MAINFRAME) Here is the fix, just replace the load bitmap call with a call to the control itself: 
m_wndToolBar.GetToolBarCtrl().AddBitmap(iNumButtons,IDR_MAINFRAME);It really is that easy, I found this solution after writing my own toolbar class that used the tool control directly, and 256 color bitmaps worked fine. I couldn't believe it, but it works! Comments:

解决方案 »

  1.   

    用 CImageList
    CImageList *imgLst;
    imgLst = new CImageList();
    imgLst->Create(32,32,ILC_COLOR32 | ILC_MASK,1,2);
    CBitmap bmp;
    bmp.LoadBitmap(IDR_MAINFRAME);
    imgLst->Add(&bmp,(COLORREF)0xFFFFFF) //白色为透明色
    m_wndToolBar.SetImageList(&imgLst);
    TBBUTTON tbtButton[1];
    LPTBBUTTON pBtn;
    tbtButton[0].fsStyle=TBSTYLE_BUTTON; //TBSTYLE_CHECK 为选项按钮
    tbtButton[0].fsState=TBSTATE_ENABLED //设置按钮可用
    int nPos;
    CString strLabel;
    strLabel="示例\0";
    nPos = m_wndToolBar.AddStrings(strLabel);
    tbtButton[0].iString=nPos;
    tbtButton[0].iBitmap=0;
    tbtButton[0].iCommand=300001; //这个值的OnCommand事件中的LOWORD(wParam),也就是资源ID一样
    pBtn=tbtButton;
    m_wndToolBar.AddButtons(1,pBtn);
      

  2.   

    这是我原来在程序中添加IE风格的ToolBar写的代码,不知道符不符合要求。
    //设置Hot ToolBar;
    CImageList imageList;
    CBitmap bitmap;
    //Create and set the normal toolbar image list
    bitmap.LoadBitmap(IDB_TOOLBAR_HOT);
    imageList.Create(25, 20, ILC_COLORDDB, 13, 1);
    imageList.Add(&bitmap, RGB(255, 0, 255));
    m_wndToolBar.GetToolBarCtrl().SetHotImageList(&imageList);
    imageList.Detach();
    bitmap.Detach();