【问题标题】:MFC : accessing CMainFrame's CImageList from ChildViewMFC:从 ChildView 访问 CMainFrame 的 CImageList
【发布时间】:2015-08-11 19:12:11
【问题描述】:

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

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

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

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

但是我在大型机方法中添加的那些图像现在没有了。如何解决这个问题?

【问题讨论】:

  • 当您需要child中的图像列表时,在这个类中创建一个。跨类访问此类成员是糟糕的设计。

标签: c++ mfc


【解决方案1】:

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

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

如果您选择分配变量并且不必跟踪CBitmap,则可以使用std::vector&lt;std::unique_ptr&lt;CBitmap&gt; &gt; 作为类成员。

如果将局部变量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

【讨论】:

  • 正是我需要的:)thanx!
猜你喜欢
  • 1970-01-01
  • 2015-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多