-1

Please help! I have tried loading image to image list via icon, hbitmap and cbitmap (i am using mfc dialog based application). But the images just wont show up. But I managed to view the image when i recreate it on an empty sdi mfc application.

m_TreeInspCtrl.DeleteAllItems();

CImageList imgl_Tree;
imgl_Tree.Create(16, 16, ILC_COLOR32, 1, 1);

/*
HBITMAP hBmp = (HBITMAP)::LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_TREE_PASS), IMAGE_BITMAP, 0, 0, LR_LOADMAP3DCOLORS);
*/

imgl_Tree.Add(AfxGetApp()->LoadIcon(IDI_ICON_PASS));

/*
CBitmap m_TreePass;
//m_TreePass.Attach(hBmp);
m_TreePass.LoadBitmap(IDB_TREE_PASS);
imgl_Tree.Add(&m_TreePass, RGB(255,0,255)); 
*/

m_TreeInspCtrl.SetImageList (&imgl_Tree, TVSIL_NORMAL); 

CString s_Root = "Inspection Sequence";
HTREEITEM h_Root = m_TreeInspCtrl.InsertItem(s_Root, 0, 0, TVI_ROOT);
m_TreeInspCtrl.SetItemColor(h_Root, RGB(0, 150, 0));
Ardy Wong
  • 3
  • 2

1 Answers1

1

You have to create a CImageList that is valid throughout the existence of the dialog. The one that you created in your code is only temporary on the stack, it will be destroyed when the initialization function returns. I suggest that you create it as a member variable of the dialog class.

mfc
  • 565
  • 2
  • 6
  • This is such a common mistake that one wonders whether the design should have been changed to have controls that use image lists maintain their own internal CImageList instance. – Nik Bougalis Feb 22 '13 at 22:47
  • 1
    @NikBougalis- Yes, this is a very common mistake, but this design also allows the CImageList to be shared among multiple CListCtrl, CTreeCtrl etc. After doing it a few times, it became quite natural to add it into the CDialog class as a member variable when one is needed. – mfc Feb 22 '13 at 23:34
  • THANKS ALOT!! gdi, never noticed it at all. – Ardy Wong Feb 25 '13 at 03:50
  • @NikBougalis- Totally agreed with u a internal cimagelist instance should b maintained. I have been wasted hours for this. and u can still use a member variable in cdialog to share among multiple tree control. – Ardy Wong Feb 25 '13 at 03:53