The following code is from a small test program used to understand how to create and use a CTreeCtrl to create a tree with a button as the image.
All works except no image of the button appears.
CTreeCtrl m_TreeView;
CImageList imgList;
The commented imgList.Create has been tried with no success
//imgList.Create((LPCTSTR)IDB_BUTTON,16, 10, RGB(255, 255, 255));
Latest try
imgList.Create((UINT)IDB_BUTTON,16,5, RGB(155,255,255));
Added the following coe when during debug the imgList shows to have three images but there is only one button bitmap
int iNumberofImages = 0;
iNumberofImages = imgList.GetImageCount();
// iNumberofImages shows a value of three yet there is only one bitmap with one picture.
m_TreeView.SetImageList(&imgList,TVSIL_NORMAL);
TVINSERTSTRUCT tvInsert;
tvInsert.hParent = NULL;
tvInsert.hInsertAfter = NULL;
tvInsert.item.mask = TVIF_TEXT;
tvInsert.item.pszText = _T("ACCEPT - Bah Bah Application");
Creates the top level node in the tree
HTREEITEM hCountry = m_TreeView.InsertItem(&tvInsert);
CString strU;
Inserts the sub nodes
strU = "ACCEPT - Microsoft .Net Framework 2.0";
m_TreeView.InsertItem(TVIF_TEXT, (LPCTSTR)strU,0,0,0,0,0,hCountry,NULL);
strU = "ACCEPT - Microsoft MDAC 2.8";
m_TreeView.InsertItem(TVIF_TEXT, (LPCTSTR)strU,0,0,0,0,0,hCountry,NULL);
strU = "ACCEPT - Microsoft SQL Express 2005";
m_TreeView.InsertItem(TVIF_TEXT, (LPCTSTR)strU,0,0,0,0,0,hCountry,NULL);
// Tree with Text appears but no image of a button.
Thank you HanneSThEGreaT for the link. I went thru the three examples of the last section as you suggested but my image still does not appear. I also notice there was guy who wrote comments saying he could not get the image to appear as well.
Oh, sorry about that, let's attempt this step by step :
I added the TreeCtl and the ImageList to my dialog
I added a Bitmap Resource ( this is used for our ImageList's pictures )
In the ClassWizard, I added a variable to the TreeCtl, named m_tree ( and it's a Control variable )
In my Dialog class's construction ( the header file ) , I added these 2 lines :
Code:
CImageList m_imageList; //image list used by the tree
CBitmap m_bitmap; //bitmap which loads bitmap
So my entire construction code looks like :
Code:
class CPictureTreesDlg : public CDialog
{
// Construction
public:
CPictureTreesDlg(CWnd* pParent = NULL); // standard constructor
CImageList m_imageList; //image list used by the tree
CBitmap m_bitmap; //bitmap which loads bitmap
// rest omitted
Then, at the end of my OnInitDialog, just above return TRUE; I added this :
Code:
// Create ImageList
m_imageList.Create (16, 16, ILC_COLOR32 , 1,1);
m_bitmap.LoadBitmap(IDB_BITMAP1);
//Add the bitmap to the ImageList
m_imageList.Add(&m_bitmap, RGB(255,0,255));
//Manage your tree items......
m_tree.SetImageList (&m_imageList, TVSIL_NORMAL);
m_tree.InsertItem("Excel",0,0,TVI_ROOT);
m_tree.InsertItem("HTML",1,1,TVI_ROOT);
m_tree.InsertItem("C++",2,2,TVI_ROOT);
m_tree.InsertItem("Word",3,3,TVI_ROOT);
m_tree.InsertItem("Outlook",4,4,TVI_ROOT);
It works, but if you need a sample, just let me know
I hope this helps!
Thank you HanneSThEGreaT for going to such trouble but I can't get your code to work either. I am attaching a couple of screen shots of what I am seeing. The first screen "JimVersion" shows what I am getting ( I am purposely making the other item be children of the first node). And the other screen shot shows what I get using your example code.
Also I had to use add recat the string with LPCTSTR in the insertItem whereas you did not have to as shown below
2. While creating the image list you must correctly set the bitmap size and bitmap mask (I have changed slightly HanneSThEGreaT's code):
Code:
m_imageList.Create (48, 16, LC_COLORDDB | ILC_MASK, 1,1);
m_bitmap.LoadBitmap(IDB_BITMAP1);
//Add the bitmap to the ImageList
m_imageList.Add(&m_bitmap, RGB(255,0,255));
//Manage your tree items......
m_tree.SetImageList (&m_imageList, TVSIL_NORMAL);
1. Note that parameters I passed in the Create() as the image size were set to (48, 16) only for example!
You must set the correct size of your )IDB_BUTTON bitmap.
2. I hope you declared CImageList imgList as class member variable.
3. If you are using CTreeCtrl::InsertItem with UINT nMask parameter:
HTREEITEM InsertItem(UINT nMask, LPCTSTR lpszItem, int nImage, int nSelectedImage, UINT nState, UINT nStateMask, LPARAM lParam, HTREEITEM hParent, HTREEITEM hInsertAfter );
you must add TVIF_IMAGE flag to this mask to cause the nImage member to be valid.
Thank you Victor. That doesn't make the image appear either (see attachment). Whatever the problem is it is strange. The code I first posted came from a MSDN site and HanneSThEGreaT's stuff works in VC6 (I should have said what I was working with VC ++ 2005 unmanaged in the first post).
Thank you Victor and HanneSThEGreaT for your time and efforts at looking at this problem!!!
I am going to call Microsoft and speak to them, as this should not be this hard to do and the documentation for this control should contain a working example including how to make use of images. The whole point of using an already existing, reuable control is to make it easier and faster to create an application and this has been just the opposite.
The code is below and the bitmap can be seen in screen shots in previous posts on this thread as well as the screen shot of the resulting tree created by this code. You will note the space between the checkbox and the text which is where the image should appear.
// m_TreeView is defined in the header file as
// CTreeCtrl m_TreeView;
CImageList imgList; // Image list object
CBitmap m_bitmap; //bitmap which loads bitmap
CString strU; // Holds node text
HTREEITEM hCountry; // Handle to tree control
TVINSERTSTRUCT tvInsert; // Tree structure
// Create the object list. The button image is 86 by 27
imgList.Create(86,27, ILC_COLORDDB | ILC_MASK, 1,1);
// Load the image bitmap into a CBitMap object
m_bitmap.LoadBitmap(IDB_BITMAP1);
//Add the bitmap to the ImageList
imgList.Add(&m_bitmap, RGB(255,0,255));
// Associate the imagelist to the tree control
m_TreeView.SetImageList(&imgList, TVSIL_NORMAL );
// Create the top level node
tvInsert.hParent = NULL;
tvInsert.hInsertAfter = NULL;
tvInsert.item.mask = TVIF_TEXT | TVIF_IMAGE;
tvInsert.item.pszText = _T("ACCEPT - PPC's Smart e-Practice Aids 2007");
// Insert the top level node
hCountry = m_TreeView.InsertItem( &tvInsert );
// Create the child nodes
// First node
strU = "ACCEPT - Microsoft .Net Framework 2.0";
m_TreeView.InsertItem(TVIF_TEXT | TVIF_IMAGE, (LPCTSTR)strU,0,0,0,0,0,hCountry,NULL);
// Second Node
strU = "ACCEPT - Microsoft MDAC 2.8";
m_TreeView.InsertItem(TVIF_TEXT | TVIF_IMAGE, (LPCTSTR)strU,0,0,0,0,0,hCountry,NULL);
// Third node
strU = "ACCEPT - Microsoft SQL Express 2005";
m_TreeView.InsertItem(TVIF_TEXT | TVIF_IMAGE, (LPCTSTR)strU,0,0,0,0,0,hCountry,NULL);
1. You should always use Code tags while posting code snippets.
2. About your code:
As I wrote in the post from 06-11-2007 07:21 PM you should (must) declare your imagelist object as a class member variable. Why didn't you do it?
If, instead, you did declare it as a member but posted the wrong snippet then you may read next questions...
What does the LoadBitmap return?
What does the imgList.Add() return?
Have you tried to use some other mask, for example, RGB(255,255,255)?
As I wrote you, at least twice, you should always use _T() macro with the text values. Why are you using it only "occasionally"?
I appreciate you going to the time and trouble on this problem but I do not need a lecture from you. Also I have recieved a sample from Microsoft that does place an image (bitmap) they created but the code will not put up my bitmap and they are not sure why yet.
To answer some your questions:
1 You should always use Code tags while posting code snippets. Victor I am not sure what you are referring to, sorry for my lack of knowledge on "Code tags"
2. About your code:
As I wrote in the post from 06-11-2007 07:21 PM you should (must) declare your imagelist object as a class member variable. Why didn't you do it?
Well I have tried doing that when using my bitmap and it made no difference but it does make a difference in the code sample Microsoft sent. If the imagelist variable is moved from the header file to the local code then their image does not appear. So your advice is correct but like I said I had already tried it and it made no difference plus Microsoft's sample with the imagelist delcared in the header also will not show my buitton image and they do not have an explanation as to why. So yes you are right but I have been banging on this problem now for some time, researching and finding no complete or even part completed examples of how to do it. Even suggestions like the ones you have given which included moving the imagelist variable to the class made no difference and it is just now occurring to me that the imagelist object had to remain around even after the tree was drawn. The problem seems to be with the bitmap I am trying to use, maybe it is too large but the MS engineer does not think that is the problem.
What does the LoadBitmap return? - It is empty and Microsoft's example code does not make use of a bitmap instead they do the same as I originally tried (m_ImgList.Create(IDB_BITMAP1, 16, 10, RGB(255,255,255)) IDB_BITMAP1 is their bitmap which is much smaller.
What does the imgList.Add() return? When I used the imgList.create like the example code in the postt I would get a image count of 3 (getImageCount) and have no idea why. the Microsoft example gets an image count of 1. Again there is somethign funny about my image. Note I did make use of a standard icon image moved to a bitmap which is about the same size of the Microsoft example and in my code it would not appear. This of course was due to the reason you point out that the imagelist object was local rather than a part of the class.
Have you tried to use some other mask, for example, RGB(255,255,255)? Yes
As I wrote you, at least twice, you should always use _T() macro with the text values. Why are you using it only "occasionally"? Becuase the text appeared whether I used _T or left it off, the main thing I always had to do and the Microsoft Example did as well is always recasting to (LPCTSTR) and I was only mixing it trying to understand what the difference is since I didn't have to use it (_T) but always had to use LPCTSTR.
Again Victor I do appreciate the help!!! But please I don't need a lecture and even Microsoft is a little puzzled on this one.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.