3

I'm trying to add images to imagelist of toolbar, which is a member of CMainFrame

startStopPicture.LoadBitmapW(IDB_STOP_PIC);
m_ToolBar.GetToolBarCtrl().GetImageList()->Add(&startStopPicture, reinterpret_cast<CBitmap*>(NULL)); 

startStopPicture.DeleteObject();

startStopPicture.LoadBitmapW(IDB_START_PIC);
m_ToolBar.GetToolBarCtrl().GetImageList()->Add(&startStopPicture, reinterpret_cast<CBitmap*>(NULL)); 

then I need to access this imagelist from childview. I'm trying do to it like this

CMainFrame* mainFrame = dynamic_cast<CMainFrame*>(GetParentFrame());

CImageList* imList = mainFrame->m_ToolBar.GetToolBarCtrl().GetImageList();

But those images I added in the mainframe's method are absent now. How to solve this promlem?

goodking
  • 145
  • 1
  • 12
  • When you need the image list in the child, create one in this class. It is bad design to access such members accross classes. – xMRi Aug 12 '15 at 06:19

1 Answers1

1

I assume your CBitmap startStopPicture is a local variable, since you neither mentioned otherwise or preceded the variable name with any class-like-identifier. Afterwards you try to store via CImageList::Add a local variable by reference.

What you have to do is either allocating the CBitmap - new CBitmap or add the startStopPicture variable to your class as a member.

If you choose to allocate the variable and do not have to keep track of the CBitmap, you could use a std::vector<std::unique_ptr<CBitmap> > as class-member.

If you store a local variable CBitmap in a CImageList, the image will not display.

Example:

//class declaration
private:
    std::vector<std::unique_ptr<CBitmap> > m_vLoadedBitmaps;
};

void CMyCtrl::SetBitmaps(CImageList &imgList)
{
    CBitmap *bmpDelete = new CBitmap();
    bmpDelete->LoadBitmapW(IDB_DELETE);
    m_vLoadedBitmaps.push_back(std::unique_ptr<CBitmap>(bmpDelete));

    imgList.Add(bmpDelete, static_cast<CBitmap*>(NULL));
}

Also I would recommend loading the images in the owner class of the variable. If needed, there's still SendMessage.

Blacktempel
  • 3,935
  • 3
  • 29
  • 53