一个MFC单文档界面程序,如何为工具栏使用更多颜色的位图作近钮?缺省好像最多只能16色的位图,不够我用:(

解决方案 »

  1.   

    可以这样:
    static UINT BASED_CODE MyToolBar[] = {
    ID_BUTTON_OPEN,
    ID_SEPARATOR,
    ID_BUTTON_A,
    ID_BUTTON_B,
    ID_BUTTON_C,
    };int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct){
    if (!m_myToolBar.CreateEx(this, TBSTYLE_FLAT , WS_CHILD | WS_VISIBLE | CBRS_TOP
    | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
    !m_myToolBar.LoadBitmap(IDB_BITMAP1) ||
    !m_myToolBar.SetButtons(MyToolBar, sizeof(MyToolBar)/sizeof(UINT)))
    {
    TRACE0("Failed to create the light toolbar.\n");
    return -1;      // if failed to create
    }//##########################################################
    CWinApp* pApp = AfxGetApp();
    imgList.Create(16, 16, ILC_COLOR8,5, 4);
    imgList.SetBkColor(0xCCCCCC);
    imgList.Add(pApp->LoadIcon(IDI_ICON3));
    imgList.Add(pApp->LoadIcon(IDI_ICON3));
    imgList.Add(pApp->LoadIcon(IDI_ICON3));
    imgList.Add(pApp->LoadIcon(IDI_ICON1));
    imgList.Add(pApp->LoadIcon(IDI_ICON2));
    m_myToolBar.GetToolBarCtrl().SetImageList(&imgList);
     ...
    }
      

  2.   

    参考如下代码,把他们存成相应的.h和.cpp文件,
    如有不详请给你的信箱,并给我消息。
    -----------------------------------------------------
    TrueColorToolBar.h#if !defined(AFX_TRUECOLORTOOLBAR_H__4D431A79_0E34_4E0C_BE1C_387E90FCA1F3__INCLUDED_)
    #define AFX_TRUECOLORTOOLBAR_H__4D431A79_0E34_4E0C_BE1C_387E90FCA1F3__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    // TrueColorToolBar.h : header file
    ///////////////////////////////////////////////////////////////////////////////
    // CTrueColorToolBar windowclass CTrueColorToolBar : public CToolBar
    {
    // Construction
    public:
    CTrueColorToolBar();// Attributes
    private:
    // Operations
    public:
    BOOL LoadTrueColorToolBar(int nBtnWidth, UINT uToolBar, UINT uToolBarHot, UINT uToolBarDisabled = 0);private:
    BOOL SetTrueColorToolBar(UINT uToolBarType, UINT uToolBar, int nBtnWidth);// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CTrueColorToolBar)
    //}}AFX_VIRTUAL// Implementation
    public:
    virtual ~CTrueColorToolBar(); // Generated message map functions
    protected:
    //{{AFX_MSG(CTrueColorToolBar)
    // NOTE - the ClassWizard will add and remove member functions here.
    afx_msg LRESULT OnIdleUpdateCmdUI(WPARAM wParam, LPARAM);
    //}}AFX_MSG DECLARE_MESSAGE_MAP()
    };///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_TRUECOLORTOOLBAR_H__4D431A79_0E34_4E0C_BE1C_387E90FCA1F3__INCLUDED_)---------------------------------------------------------------------------
    TrueColorToolBar.cpp// TrueColorToolBar.cpp : implementation file
    //#include "stdafx.h"
    #include "TrueColorToolBar.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CTrueColorToolBarCTrueColorToolBar::CTrueColorToolBar()
    {
    }CTrueColorToolBar::~CTrueColorToolBar()
    {
    }
    BEGIN_MESSAGE_MAP(CTrueColorToolBar, CToolBar)
    //{{AFX_MSG_MAP(CTrueColorToolBar)
    // NOTE - the ClassWizard will add and remove mapping macros here.
    ON_MESSAGE(WM_IDLEUPDATECMDUI, OnIdleUpdateCmdUI)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CTrueColorToolBar message handlers
    BOOL CTrueColorToolBar::LoadTrueColorToolBar(int nBtnWidth, UINT uToolBar, UINT uToolBarHot, UINT uToolBarDisabled)
    {
    if (!SetTrueColorToolBar(TB_SETIMAGELIST, uToolBar, nBtnWidth)) return FALSE;

    if (!SetTrueColorToolBar(TB_SETHOTIMAGELIST, uToolBarHot, nBtnWidth)) return FALSE; if (uToolBarDisabled) 
    {
    if (!SetTrueColorToolBar(TB_SETDISABLEDIMAGELIST, uToolBarDisabled, nBtnWidth))
    return FALSE;
    } return TRUE;
    }
    BOOL CTrueColorToolBar::SetTrueColorToolBar(UINT uToolBarType, UINT uToolBar, int nBtnWidth)
    {
    CImageList cImageList;
    CBitmap cBitmap;
    BITMAP bmBitmap;
    CSize cSize;
    int nNbBtn;

    if (!cBitmap.Attach(LoadImage(AfxGetInstanceHandle(),
      MAKEINTRESOURCE(uToolBar),
      IMAGE_BITMAP, 0, 0,
                              LR_DEFAULTSIZE|LR_CREATEDIBSECTION)) ||
        !cBitmap.GetBitmap(&bmBitmap))
    return FALSE; cSize  = CSize(bmBitmap.bmWidth, bmBitmap.bmHeight); 
    nNbBtn = cSize.cx/nBtnWidth;
    RGBTRIPLE* rgb = (RGBTRIPLE*)(bmBitmap.bmBits);
    COLORREF   rgbMask = RGB(rgb[0].rgbtRed,
                          rgb[0].rgbtGreen,
      rgb[0].rgbtBlue);

    if (!cImageList.Create(nBtnWidth, cSize.cy,
                       ILC_COLOR24|ILC_MASK,
       nNbBtn, 0))
    return FALSE;

    if (cImageList.Add(&cBitmap, rgbMask) == -1)
    return FALSE;/*
    if (uToolBarType == TB_SETIMAGELIST)
    GetToolBarCtrl().SetImageList(&cImageList);
    else if (uToolBarType == TB_SETHOTIMAGELIST)
    GetToolBarCtrl().SetHotImageList(&cImageList);
    else if (uToolBarType == TB_SETDISABLEDIMAGELIST)
    GetToolBarCtrl().SetDisabledImageList(&cImageList);
    else 
    return FALSE;
    */ SendMessage(uToolBarType, 0, (LPARAM)cImageList.m_hImageList);
    cImageList.Detach(); 
    cBitmap.Detach();

    return TRUE;
    }/////////////////////////////////////////////////////////////////////////////
    // CTrueColorToolBar::OnIdleUpdateCmdUI
    //      OnIdleUpdateCmdUI handles the WM_IDLEUPDATECMDUI message, which is
    //      used to update the status of user-interface elements within the MFC
    //      framework.
    //
    //      We have to get a little tricky here: CToolBar::OnUpdateCmdUI
    //      expects a CFrameWnd pointer as its first parameter.  However, it
    //      doesn't do anything but pass the parameter on to another function
    //      which only requires a CCmdTarget pointer.  We can get a CWnd pointer
    //      to the parent window, which is a CCmdTarget, but may not be a
    //      CFrameWnd.  So, to make CToolBar::OnUpdateCmdUI happy, we will call
    //      our CWnd pointer a CFrameWnd pointer temporarily.LRESULT CTrueColorToolBar::OnIdleUpdateCmdUI(WPARAM wParam, LPARAM)
    {
    if (IsWindowVisible())
    {
    CFrameWnd *pParent = (CFrameWnd *)GetParent();
    if (pParent)
    OnUpdateCmdUI(pParent, (BOOL)wParam);
    }
    return 0L;
    }------------------------------------------------
    结束
      

  3.   

    http://expert.csdn.net/Expert/topic/2466/2466037.xml?temp=9.437197E-02