CImageList, CBitmap and CToolbar-Collection of common programming errors


  • msdn Hello,suppose I have few bitmap files a.bmp and b.bmp which i mange to load into CBitmap instance a and b accordingly.Now suppose that I added them to a CImageList, then how can I load / set it to a CtoolBar?tnx

  • msdn1

    OK! This is a way to do it too. My way would be a little bit more complicated.
    You don’t need to use SendMessage you can execute

    _barbara.GetToolBarCtrl()->SetImageList

    You can directly load a CBitmap with the Load function. There is no need to use LoadImage.

    Do you need to load the symbols from more than one bitmap? Isn’t it possible for you to use one bitmap?


  • msdn2 Hey Martin,Since I’m new to this MFC, could you send me an example of what you ment?I did succed to make it works like this:(where my CToolBar is _barbara, and nButtonIDs is array of  commands IDs)_barbara.CreateEx(this); CString strBitmap1(“toolbar2.bmp”); CString strBitmap2(“../toolbar3.bmp”); HBITMAP hBitmap1 = static_cast( ::LoadImage( AfxGetInstanceHandle() ,strBitmap1 ,IMAGE_BITMAP , 0 , 0 , LR_LOADFROMFILE | LR_CREATEDIBSECTION )); HBITMAP hBitmap2 = static_cast( ::LoadImage(AfxGetInstanceHandle(), strBitmap2, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION)); CImageList list; list.Create(15,16,ILC_COLOR,0,10); CBitmap c1; c1.Attach(hBitmap1); list.Add(&c1,RGB(255,255,255)); if (hBitmap2){ CBitmap c2; c2.Attach(hBitmap2); list.Add(&c2,RGB(255,255,255)); } _barbara.SendMessage(TB_SETIMAGELIST, 0, reinterpret_cast(list.m_hImageList)); _barbara.SetButtons(nButtonIDs,9); list.Detach();

    tnx


  • msdn3

    OK! This is a way to do it too. My way would be a little bit more complicated.
    You don’t need to use SendMessage you can execute

    _barbara.GetToolBarCtrl()->SetImageList

    You can directly load a CBitmap with the Load function. There is no need to use LoadImage.

    Do you need to load the symbols from more than one bitmap? Isn’t it possible for you to use one bitmap?


  • msdn4 Hello again,Well, I need to allow a dynaimc number of buttons in the toolbar and also a dynamic bitmap for each one – which means that I could change them at runtime, therefor I can’t use MFC resource since it’s being compiled or something and i cant use only one bitmap. I’ll try this one: _barbara.GetToolBarCtrl()->SetImageListIt looks much nicer : )anyway,

    tnx very much