[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 850
  • Last Modified:

how to use lParam member variable in CTreeCtrl

 I'm creating an application which contains a CTreeCtrl such as Workspace in
DEVSTUDIO,  there is one member variable - lParam in each tree item, but i don't know
how to use it. I'm trying to store a CObList or a pointer to linklist into this lParam, but
how ? can anybody tell me please.
0
eugeneng
Asked:
eugeneng
  • 4
  • 3
1 Solution
 
RONSLOWCommented:
The lParam is used to hold extra data.  If you use callbacks for tree node labels, then you'd usually make use of the data in the lParam from within the OnGetDispInfo callback.

To store a pointer in the lParam, use a cast eg.
  lParam = reinterpret_cast<LPARAM>(mypointer);
and likewise, to get the pointer back out of the lparam, use
  mypointer = reinterpret_cast<CObList*>(lParam);

Do you really want an entire list associated with a tree item, of just a single element of the list.  If only a single element is required, then you could store a pointer to that element in the lParam, or if you used an array, you could store the index into the array.

0
 
eugenengAuthor Commented:
Dear Ronslow :
 can you show me how to store in  and get out a CObject, or struct, frome a lparam then assign it to tree item. thank
0
 
RONSLOWCommented:
If you have CMyClass (maybe derived from CObject)
and you have a pointer to an object CMyClass* pMyObjectpMyObject,
then do this when creating the tree item...

treeitem.lParam = reinterpret_cast<LPARAM>(pMyObject);

Whenever you refer to a tre item (eg. use clicks on it an you want to know what is selected).  The get the corresponding item and do this...

CMyClass* pMyObject = reinterpret_cast<CMyClass*>(lParam);

You don't need to 'get out <something> from a lParam then assign it to tree item'.  You simply fill in the lParam when you create the tree item (along with label etc).  When you need to get at the object, you ask for (or are given) the tree item struct and look at lParam and cast it (as shown above).



0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
eugenengAuthor Commented:
Dear RONSLOW:
  Regarding the question of storing CObject pointer into TV_ITEM.lParam member variable of a tree control item, after i've new my a CObject pointer and attached it to lParam of TV_ITEM(by using the way you tough me), would the tree control clear the memory (that allocated for CObject) when tree item has been deleted?
0
 
RONSLOWCommented:
No - you manage the data yourself.

The tree (or list) control simply looks at it.

Of course, you could write you own code that also killed off the data.
0
 
eugenengAuthor Commented:
is the example below correctly remove the memory ? :

   CMyObjec* pMyStruct;
   pMyStruct = reinterpret_cast<CMyStruc*>(GetItemData(hTree));
   delete pMyStruct;


0
 
RONSLOWCommented:
It all depends on how you are creating and managing your data.

There code would delete the data associated - but you'd want to ensure that that doesn't result in any pointers getting stuffer. eg. if your CMyStruc objects are in a linked list (eg CList), and you just delete one of them, then that could stuff up your list.

But assuming that the tree is the only place that stores the data, then that would do it (assuming the GetItemData call and args are all OK).


0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now