gpt4 book ai didi

c++ - MFC : accessing CMainFrame's CImageList from ChildView

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:17:09 24 4
gpt4 key购买 nike

我正在尝试将图像添加到工具栏的图像列表中,它是 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));

然后我需要从 subview 访问这个图像列表。我正在尝试这样做

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

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

但是我在主机方法中添加的那些图像现在不存在了。如何解决这个问题?

最佳答案

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

您需要做的是分配 CBitmap - new CBitmap或添加 startStopPicture作为成员添加到您的类(class)的变量。

如果您选择分配变量并且不必跟踪 CBitmap , 你可以使用 std::vector<std::unique_ptr<CBitmap> >作为类(class)成员。

如果你存储一个局部变量 CBitmap CImageList , 图像将不会显示。

例子:

//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));
}

我还建议在变量的所有者类中加载图像。如果需要,还有 SendMessage .

关于c++ - MFC : accessing CMainFrame's CImageList from ChildView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31950063/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com