Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CTreeView/CTreeCtrl with Images

707 views
Skip to first unread message

Matt

unread,
Oct 14, 2005, 5:58:01 PM10/14/05
to
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

unread,
Oct 17, 2005, 4:51:02 PM10/17/05
to
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.

"Matt" <Ma...@discussions.microsoft.com> wrote in message
news:152F9E28-ADFC-4F41...@microsoft.com...

Matt

unread,
Oct 20, 2005, 2:56:10 PM10/20/05
to
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

Radhika S

unread,
Aug 26, 2008, 7:53:00 AM8/26/08
to
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

unread,
Aug 26, 2008, 1:52:23 PM8/26/08
to
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

"Radhika S" <Radh...@discussions.microsoft.com> wrote in message
news:39ACCC58-EC23-4D7C...@microsoft.com...

0 new messages