C++ MFC:访问CMainFrame';来自ChildView的CImageList

C++ MFC:访问CMainFrame';来自ChildView的CImageList,c++,mfc,C++,Mfc,我正在尝试将图像添加到工具栏的imagelist,它是CMainFrame的成员 startStopPicture.LoadBitmapW(IDB_STOP_PIC); m_ToolBar.GetToolBarCtrl().GetImageList()->Add(&startStopPicture, reinterpret_cast<CBitmap*>(NULL)); startStopPicture.DeleteObject(); startStopPictur

我正在尝试将图像添加到工具栏的imagelist,它是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)); 
startStopPicture.LoadBitmapW(IDB\u STOP\u PIC);
m_ToolBar.GetToolBarCtrl().GetImageList()->添加(&startStopPicture,重新解释_转换(NULL));
startStopPicture.DeleteObject();
startStopPicture.LoadBitmapW(IDB_START_PIC);
m_ToolBar.GetToolBarCtrl().GetImageList()->添加(&startStopPicture,重新解释_转换(NULL));
然后我需要从childview访问此图像列表。我正试着这样做

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

CImageList* imList = mainFrame->m_ToolBar.GetToolBarCtrl().GetImageList();
CMainFrame*mainFrame=dynamic_cast(GetParentFrame());
CImageList*imList=mainFrame->m_ToolBar.GetToolBarCtrl().GetImageList();
但是我在大型机的方法中添加的那些图像现在不存在了。如何解决这个问题?

我假设您的
startStopPicture
是一个局部变量,因为您没有另外提到变量名,也没有在变量名前面加任何类标识符。之后,您尝试通过引用通过局部变量进行存储

您需要做的是分配-
new CBitmap
或将
startstopicture
变量作为成员添加到类中

如果选择分配变量并且不必跟踪变量,则可以使用
std::vector
作为类成员

如果将局部变量存储在中,则不会显示图像

例如:

//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));
}
//类声明
私人:
std::向量m_加载位图;
};
void CMyCtrl::SetBitmaps(CImageList和imgList)
{
CBitmap*bmpDelete=新的CBitmap();
bmpDelete->LoadBitmapW(IDB_DELETE);
m_vLoadedBitmaps.push_back(std::unique_ptr(bmpDelete));
添加(bmpDelete,static_cast(NULL));
}

我还建议将图像加载到变量的所有者类中。如果需要,仍然有。

当您需要子级中的图像列表时,请在此类中创建一个。在不同的类中访问这样的成员是糟糕的设计。这正是我需要的:)thanx!