Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MFC : accessing CMainFrame's CImageList from ChildView

Tags:

c++

mfc

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?

like image 550
goodking Avatar asked Dec 08 '25 07:12

goodking


1 Answers

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.

like image 96
Blacktempel Avatar answered Dec 10 '25 19:12

Blacktempel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!