-
Help with Tree View
Hi!
I don't seem to get my Tree View running...
every time I try to compile my (very simple) Prog with the Treeview in it, I get the error
Error E2451 simplewin2.c 169: Undefined symbol 'item' in function AddItemToTree
allthough "item" definitely IS a member variable of the TVINSERTITEM struct... I don't know what I'm doing wrong.
(FYI: I'm using the free Borland Commandlinetools, not MSVC.)
And when I get this to work (hopefully, with your help...) I have the next problem right following: I want the Tree View to display the directory structure of the PC... so I need to scan the drives for all directory names and add 'em to the Tree View... how can I do this?
(FYI 2: I'm NOT using MFC, just the "normal" Win32 API)
-
Of course, it's "TVINSERTSTRUCT" not "TVINSERTITEM" ... but it's typed right in my code so that wasnt the prob, I just made a mistake when writing this post. ;)
-
Need to see the code to realy help.
Mainly how have you declared the TV_ITEM structure 'item'. It could be that item is another struct and can its members have to be refrenced not the whole ie TVInsert.item.mask (unless you are doing a CopyMemory() )
Or it could be that MS have changed it from TVITEM to TV_ITEM to maintain structure naming conventions and you have old files in your compiler.
-
It's basically the original example code from msdn:
Code:
// AddItemToTree - adds items to a tree-view control.
// Returns the handle to the newly added item.
// hwndTV - handle to the tree-view control.
// lpszItem - text of the item to add.
// nLevel - level at which to add the item.
HTREEITEM AddItemToTree(HWND hwndTV, LPSTR lpszItem, int nLevel)
{
TVITEM tvi;
TVINSERTSTRUCT tvins;
static HTREEITEM hPrev = (HTREEITEM)TVI_FIRST;
static HTREEITEM hPrevRootItem = NULL;
static HTREEITEM hPrevLev2Item = NULL;
HTREEITEM hti;
tvi.mask = TVIF_TEXT | TVIF_IMAGE
| TVIF_SELECTEDIMAGE | TVIF_PARAM;
// Set the text of the item.
tvi.pszText = lpszItem;
tvi.cchTextMax = sizeof(tvi.pszText)/sizeof(tvi.pszText[0]);
// Assume the item is not a parent item, so give it a
// document image.
tvi.iImage = g_nDocument;
tvi.iSelectedImage = g_nDocument;
// Save the heading level in the item's application-defined
// data area.
tvi.lParam = (LPARAM)nLevel;
tvins.item = tvi;
tvins.hInsertAfter = hPrev;
// Set the parent item based on the specified level.
if (nLevel == 1)
tvins.hParent = TVI_ROOT;
else if (nLevel == 2)
tvins.hParent = hPrevRootItem;
else
tvins.hParent = hPrevLev2Item;
// Add the item to the tree-view control.
hPrev = (HTREEITEM)SendMessage(hwndTV,
TVM_INSERTITEM,
0,
(LPARAM)(LPTVINSERTSTRUCT)&tvins);
// Save the handle to the item.
if (nLevel == 1)
hPrevRootItem = hPrev;
else if (nLevel == 2)
hPrevLev2Item = hPrev;
// The new item is a child item. Give the parent item a
// closed folder bitmap to indicate it now has child items.
if (nLevel > 1)
{
hti = TreeView_GetParent(hwndTV, hPrev);
tvi.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE;
tvi.hItem = hti;
tvi.iImage = g_nClosed;
tvi.iSelectedImage = g_nClosed;
TreeView_SetItem(hwndTV, &tvi);
}
return hPrev;
}
If you are right with your idea that I may have not-up-to-date files in my compiler, what would I have to change (in the commctrl.h??)
Thanks for helping :)
-
Try this
In the commctrl.h file, the 'item' member of TVINSERTSTRUCT is declared as TV_ITEMA, or TVITEM. Find this in your commctrl.h file:
typedef struct _TV_INSERTSTRUCTA {
HTREEITEM hParent;
HTREEITEM hInsertAfter;
TV_ITEMA item;
} TV_INSERTSTRUCTA,*LPTV_INSERTSTRUCTA;
typedef struct _TV_INSERTSTRUCTW {
HTREEITEM hParent;
HTREEITEM hInsertAfter;
TV_ITEMW item;
} TV_INSERTSTRUCTW,*LPTV_INSERTSTRUCTW;
and be sure that the TV_ITEMA and TV_ITEMW lines are in there.
-
Yes it seems to be quite up-to-date, I think... here's what I got in the commctrl.h of Borland C++ 5.5.1
Code:
typedef struct tagTVINSERTSTRUCTA {
HTREEITEM hParent;
HTREEITEM hInsertAfter;
#if (_WIN32_IE >= 0x0400)
union
{
TVITEMEXA itemex;
TV_ITEMA item;
} DUMMYUNIONNAME;
#else
TV_ITEMA item;
#endif
} TVINSERTSTRUCTA, FAR *LPTVINSERTSTRUCTA;
typedef struct tagTVINSERTSTRUCTW {
HTREEITEM hParent;
HTREEITEM hInsertAfter;
#if (_WIN32_IE >= 0x0400)
union
{
TVITEMEXW itemex;
TV_ITEMW item;
} DUMMYUNIONNAME;
#else
TV_ITEMW item;
#endif
} TVINSERTSTRUCTW, FAR *LPTVINSERTSTRUCTW;
And, later in the file, there are many declare's that make the use of TV_ITEMA, TVITEMA, TVITEM etc. equivalent... so that doesn't seem to be the cause of my problem... :(
-
You can't do
tvins.item = tvi;//one whole structure equals another whole structure
must do something like
CopyMemory(&(tvins.item),&tvi,sizeof(TVITEM));
(which is why you should post the code first to stop us guessing at the problem and sending you off after red herrings)
>>I want the Tree View to display the directory structure of the PC
Did you look at listbox with the LB_DIR flag?
To scan the HD you will need to use FindFirstFile(), FindNextFile() and FindClose()
-
Thanks I solved the problem with the tree view (well, not quite yet, it just won't show the images from the imagelist I made for it... but I'm looking at that now)
Thanks for your advices, tho :D