Discussion:
CTreeView/CTreeCtrl with Images
(too old to reply)
Matt
2005-10-14 21:58:01 UTC
Permalink
I'm just now getting into explorer-style SDIs and I'm making a program on my
own time with a CTreeView on the left side. I've made a 32-bit icon using
IconXP and I'm trying to display it along with the first element (root item)
on that tree. The text of the item displays perfectly fine, but the icon does
not. The wierdest part is that if I change the size of the icon in my image
list, the size of the element changes, so it's as if there's a blank icon
there. I've even tried using built-in icons that came as the wizard made my
MFC program, but those failed the same way. Could someone take a look and see
if they know what's going on? Thanks!

void CLeftView::OnInitialUpdate()
{
CTreeView::OnInitialUpdate();
wchar_t* pstrTemp; // assign and release memory as
needed!

// TODO: You may populate your TreeView with items by directly accessing
// its tree control through a call to GetTreeCtrl().
// First, add our images to be used
CImageList imageList;
imageList.Create(12,12,ILC_COLOR32,0,20);
imageList.Add(AfxGetApp()->LoadIconW(IDI_FOLDER_OPEN));
GetTreeCtrl().SetImageList(&imageList, TVSIL_NORMAL);
// Now load the structure data
TVINSERTSTRUCT tvItem;
TVITEMEXW tvItemDetails;
tvItem.hParent = TVI_ROOT; // root-level item
tvItem.hInsertAfter = TVI_FIRST; // sort as first item in list
tvItemDetails.state = TVIS_EXPANDED | // Default to expanded
0x0000; // no state image, no overlay mask
tvItemDetails.stateMask = 0x00FF; // only set item state

pstrTemp = new wchar_t[200]; // 199 chars should be enough
ZeroMemory(pstrTemp, 200*sizeof(wchar_t)); // ensure null terminator
LoadString(AfxGetInstanceHandle(), IDS_ROOT_NAME, pstrTemp, 200);
tvItemDetails.pszText = pstrTemp;
tvItemDetails.cchTextMax = 199; // # of chars, not size
tvItemDetails.lParam = NULL;
tvItemDetails.iImage = 0;
tvItemDetails.iSelectedImage=0;
tvItemDetails.iIntegral = 1; // standard height
tvItemDetails.mask = TVIF_INTEGRAL | // valid integral param
TVIF_STATE | // valid state/statemask params
TVIF_TEXT | // valid pszText/cchTextMax params
TVIF_IMAGE | // valid image params
TVIF_SELECTEDIMAGE;
tvItem.itemex = tvItemDetails;
// Finally add it to the list, let's store the handle
m_RootHandle = GetTreeCtrl().InsertItem(&tvItem);
int a,b;
GetTreeCtrl().GetItemImage(m_RootHandle, a, b);
// Now we can free the memory we allocated
delete[] pstrTemp;


// Blank script until the user tells us otherwise -- end here.
}
AliR
2005-10-17 20:51:02 UTC
Permalink
First thing, you created your image list on the stack, once OnInitialUpdate
is exits your image list is also gone.
Make the CImageList variable part of your CLeftView class.

AliR.
Post by Matt
I'm just now getting into explorer-style SDIs and I'm making a program on my
own time with a CTreeView on the left side. I've made a 32-bit icon using
IconXP and I'm trying to display it along with the first element (root item)
on that tree. The text of the item displays perfectly fine, but the icon does
not. The wierdest part is that if I change the size of the icon in my image
list, the size of the element changes, so it's as if there's a blank icon
there. I've even tried using built-in icons that came as the wizard made my
MFC program, but those failed the same way. Could someone take a look and see
if they know what's going on? Thanks!
void CLeftView::OnInitialUpdate()
{
CTreeView::OnInitialUpdate();
wchar_t* pstrTemp; // assign and release memory as
needed!
// TODO: You may populate your TreeView with items by directly accessing
// its tree control through a call to GetTreeCtrl().
// First, add our images to be used
CImageList imageList;
imageList.Create(12,12,ILC_COLOR32,0,20);
imageList.Add(AfxGetApp()->LoadIconW(IDI_FOLDER_OPEN));
GetTreeCtrl().SetImageList(&imageList, TVSIL_NORMAL);
// Now load the structure data
TVINSERTSTRUCT tvItem;
TVITEMEXW tvItemDetails;
tvItem.hParent = TVI_ROOT; // root-level item
tvItem.hInsertAfter = TVI_FIRST; // sort as first item in list
tvItemDetails.state = TVIS_EXPANDED | // Default to expanded
0x0000; // no state image, no overlay mask
tvItemDetails.stateMask = 0x00FF; // only set item state
pstrTemp = new wchar_t[200]; // 199 chars should be enough
ZeroMemory(pstrTemp, 200*sizeof(wchar_t)); // ensure null terminator
LoadString(AfxGetInstanceHandle(), IDS_ROOT_NAME, pstrTemp, 200);
tvItemDetails.pszText = pstrTemp;
tvItemDetails.cchTextMax = 199; // # of chars, not size
tvItemDetails.lParam = NULL;
tvItemDetails.iImage = 0;
tvItemDetails.iSelectedImage=0;
tvItemDetails.iIntegral = 1; // standard height
tvItemDetails.mask = TVIF_INTEGRAL | // valid integral param
TVIF_STATE | // valid state/statemask params
TVIF_TEXT | // valid pszText/cchTextMax params
TVIF_IMAGE | // valid image params
TVIF_SELECTEDIMAGE;
tvItem.itemex = tvItemDetails;
// Finally add it to the list, let's store the handle
m_RootHandle = GetTreeCtrl().InsertItem(&tvItem);
int a,b;
GetTreeCtrl().GetItemImage(m_RootHandle, a, b);
// Now we can free the memory we allocated
delete[] pstrTemp;
// Blank script until the user tells us otherwise -- end here.
}
Matt
2005-10-20 18:56:10 UTC
Permalink
Wow, I can't believe I made such a stupid mistake. Made a m_imageList inside
CLeftView and it works like a charm. Thanks for the help, and good eye.

-- Matt
Post by AliR
First thing, you created your image list on the stack, once OnInitialUpdate
is exits your image list is also gone.
Make the CImageList variable part of your CLeftView class.
AliR.
Post by Matt
I'm just now getting into explorer-style SDIs and I'm making a program on
my
Post by Matt
own time with a CTreeView on the left side. I've made a 32-bit icon using
IconXP and I'm trying to display it along with the first element (root
item)
Post by Matt
on that tree. The text of the item displays perfectly fine, but the icon
does
Post by Matt
not. The wierdest part is that if I change the size of the icon in my
image
Post by Matt
list, the size of the element changes, so it's as if there's a blank icon
there. I've even tried using built-in icons that came as the wizard made
my
Post by Matt
MFC program, but those failed the same way. Could someone take a look and
see
Post by Matt
if they know what's going on? Thanks!
void CLeftView::OnInitialUpdate()
{
CTreeView::OnInitialUpdate();
wchar_t* pstrTemp; // assign and release memory
as
Post by Matt
needed!
// TODO: You may populate your TreeView with items by directly accessing
// its tree control through a call to GetTreeCtrl().
// First, add our images to be used
CImageList imageList;
imageList.Create(12,12,ILC_COLOR32,0,20);
imageList.Add(AfxGetApp()->LoadIconW(IDI_FOLDER_OPEN));
GetTreeCtrl().SetImageList(&imageList, TVSIL_NORMAL);
// Now load the structure data
TVINSERTSTRUCT tvItem;
TVITEMEXW tvItemDetails;
tvItem.hParent = TVI_ROOT; // root-level item
tvItem.hInsertAfter = TVI_FIRST; // sort as first item in list
tvItemDetails.state = TVIS_EXPANDED | // Default to expanded
0x0000; // no state image, no overlay
mask
Post by Matt
tvItemDetails.stateMask = 0x00FF; // only set item state
pstrTemp = new wchar_t[200]; // 199 chars should be
enough
Post by Matt
ZeroMemory(pstrTemp, 200*sizeof(wchar_t)); // ensure null terminator
LoadString(AfxGetInstanceHandle(), IDS_ROOT_NAME, pstrTemp, 200);
tvItemDetails.pszText = pstrTemp;
tvItemDetails.cchTextMax = 199; // # of chars, not size
tvItemDetails.lParam = NULL;
tvItemDetails.iImage = 0;
tvItemDetails.iSelectedImage=0;
tvItemDetails.iIntegral = 1; // standard height
tvItemDetails.mask = TVIF_INTEGRAL | // valid integral param
TVIF_STATE | // valid state/statemask
params
Post by Matt
TVIF_TEXT | // valid pszText/cchTextMax
params
Post by Matt
TVIF_IMAGE | // valid image params
TVIF_SELECTEDIMAGE;
tvItem.itemex = tvItemDetails;
// Finally add it to the list, let's store the handle
m_RootHandle = GetTreeCtrl().InsertItem(&tvItem);
int a,b;
GetTreeCtrl().GetItemImage(m_RootHandle, a, b);
// Now we can free the memory we allocated
delete[] pstrTemp;
// Blank script until the user tells us otherwise -- end here.
}
Radhika S
2008-08-26 11:53:00 UTC
Permalink
Hi,

Am also facing a similar problem. Am having a listctrl and adding a
imagelist to it. But the image is noe being displayed. I am creating the app
as a MFC smart Device app.
Please ee my code below. PLease tell mewhats the mistake and why the icon is
not displayed though i get no error. As specified in this the post i tried to
initialize the imagelist in the OnInitDialog but am not able to access it in
the eventhandler. Someone help me please. Here is the code.

BOOL CMySampleDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here
mylistobj.InsertColumn(0,L"Name",LVCFMT_LEFT,50);
mylistobj.InsertColumn(1,L"Date",LVCFMT_LEFT,50);
mylistobj.InsertColumn(2,L"Size",LVCFMT_LEFT,50);


return TRUE; // return TRUE unless you set the focus to a control
}

void CMySampleDlg::OnBnClickedButton1()
{

CImageList il;
il.Create(IDI_ICON1,20,1,CLR_NONE);
mylistobj.ModifyStyle(LVS_TYPEMASK, LVS_REPORT);
mylistobj.SetImageList(&il,LVSIL_HEADER );
static CString details[5][3]={
L"aaaaaaaaaaaaaaaaaa",L"1stAug",L"20KB",
L"bbb",L"2ndAug",L"20KB",
L"ccc",L"3rdAug",L"20KB",
L"ddd",L"4thAug",L"20KB",
L"eee",L"5thAug",L"20KB",
};

int i=0;

// TODO: Add your control notification handler code here
for(i=0;i<5;i++)
{
//mylistobj.InsertItem(&lvi);
mylistobj.InsertItem(i,(LPCTSTR)details[i][0],0);
mylistobj.SetItemText(i,1,(LPCTSTR)details[i][1]);
mylistobj.SetItemText(i,2,(LPCTSTR)details[i][2]);
}

}

When a button is clicked a displaying the items in the listview mylistobj.

Thanks in Advance.
Regards,
Radhika
Tom Serface
2008-08-26 17:52:23 UTC
Permalink
My guess is that LVIF_IMAGE is not set for your item in the mask telling it
that you are using an image. I don't know what is in your imagelist either
(from the example here) so it could just be blank if the icon is not filled
out. Did you try with CLR_DEFAULT just to see if perhaps something is not
blending right with the image.

Tom
Post by Radhika S
Hi,
Am also facing a similar problem. Am having a listctrl and adding a
imagelist to it. But the image is noe being displayed. I am creating the app
as a MFC smart Device app.
Please ee my code below. PLease tell mewhats the mistake and why the icon is
not displayed though i get no error. As specified in this the post i tried to
initialize the imagelist in the OnInitDialog but am not able to access it in
the eventhandler. Someone help me please. Here is the code.
BOOL CMySampleDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
mylistobj.InsertColumn(0,L"Name",LVCFMT_LEFT,50);
mylistobj.InsertColumn(1,L"Date",LVCFMT_LEFT,50);
mylistobj.InsertColumn(2,L"Size",LVCFMT_LEFT,50);
return TRUE; // return TRUE unless you set the focus to a control
}
void CMySampleDlg::OnBnClickedButton1()
{
CImageList il;
il.Create(IDI_ICON1,20,1,CLR_NONE);
mylistobj.ModifyStyle(LVS_TYPEMASK, LVS_REPORT);
mylistobj.SetImageList(&il,LVSIL_HEADER );
static CString details[5][3]={
L"aaaaaaaaaaaaaaaaaa",L"1stAug",L"20KB",
L"bbb",L"2ndAug",L"20KB",
L"ccc",L"3rdAug",L"20KB",
L"ddd",L"4thAug",L"20KB",
L"eee",L"5thAug",L"20KB",
};
int i=0;
// TODO: Add your control notification handler code here
for(i=0;i<5;i++)
{
//mylistobj.InsertItem(&lvi);
mylistobj.InsertItem(i,(LPCTSTR)details[i][0],0);
mylistobj.SetItemText(i,1,(LPCTSTR)details[i][1]);
mylistobj.SetItemText(i,2,(LPCTSTR)details[i][2]);
}
}
When a button is clicked a displaying the items in the listview mylistobj.
Thanks in Advance.
Regards,
Radhika
Loading...