mfc load image into imagelist for tree control

1.5k views Asked by At

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));
1

There are 1 answers

4
mfc On BEST ANSWER

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.